]>
Commit | Line | Data |
---|---|---|
0a7c9364 | 1 | #!/usr/bin/env sh |
bfdf1f48 | 2 | |
a0c2d312 | 3 | VER=2.8.9 |
a7b7355d | 4 | |
6cc11ffb | 5 | PROJECT_NAME="acme.sh" |
a7b7355d | 6 | |
6cc11ffb | 7 | PROJECT_ENTRY="acme.sh" |
8 | ||
09f74a9a | 9 | PROJECT="https://github.com/acmesh-official/$PROJECT_NAME" |
4c3b3608 | 10 | |
f3e4cea3 | 11 | DEFAULT_INSTALL_HOME="$HOME/.$PROJECT_NAME" |
77f96b38 | 12 | |
13 | _WINDOWS_SCHEDULER_NAME="$PROJECT_NAME.cron" | |
14 | ||
f3e4cea3 | 15 | _SCRIPT_="$0" |
16 | ||
b50e701c | 17 | _SUB_FOLDER_NOTIFY="notify" |
18 | _SUB_FOLDER_DNSAPI="dnsapi" | |
19 | _SUB_FOLDER_DEPLOY="deploy" | |
20 | ||
21 | _SUB_FOLDERS="$_SUB_FOLDER_DNSAPI $_SUB_FOLDER_DEPLOY $_SUB_FOLDER_NOTIFY" | |
f3e4cea3 | 22 | |
c1151b0d | 23 | LETSENCRYPT_CA_V1="https://acme-v01.api.letsencrypt.org/directory" |
24 | LETSENCRYPT_STAGING_CA_V1="https://acme-staging.api.letsencrypt.org/directory" | |
25 | ||
737e9e48 | 26 | CA_LETSENCRYPT_V2="https://acme-v02.api.letsencrypt.org/directory" |
27 | CA_LETSENCRYPT_V2_TEST="https://acme-staging-v02.api.letsencrypt.org/directory" | |
c1151b0d | 28 | |
737e9e48 | 29 | CA_BUYPASS="https://api.buypass.com/acme/directory" |
30 | CA_BUYPASS_TEST="https://api.test4.buypass.no/acme/directory" | |
31 | ||
32 | CA_ZEROSSL="https://acme.zerossl.com/v2/DV90" | |
389518e1 | 33 | _ZERO_EAB_ENDPOINT="http://api.zerossl.com/acme/eab-credentials-email" |
34 | ||
737e9e48 | 35 | DEFAULT_CA=$CA_LETSENCRYPT_V2 |
36 | DEFAULT_STAGING_CA=$CA_LETSENCRYPT_V2_TEST | |
37 | ||
38 | CA_NAMES=" | |
50fefc3b | 39 | LetsEncrypt.org,letsencrypt |
40 | LetsEncrypt.org_test,letsencrypt_test,letsencrypttest | |
269847d1 | 41 | BuyPass.com,buypass |
42 | BuyPass.com_test,buypass_test,buypasstest | |
43 | ZeroSSL.com,zerossl | |
737e9e48 | 44 | " |
45 | ||
46 | CA_SERVERS="$CA_LETSENCRYPT_V2,$CA_LETSENCRYPT_V2_TEST,$CA_BUYPASS,$CA_BUYPASS_TEST,$CA_ZEROSSL" | |
f87890cb | 47 | |
07af4247 | 48 | DEFAULT_USER_AGENT="$PROJECT_NAME/$VER ($PROJECT)" |
bbbdcb09 | 49 | |
57e58ce7 | 50 | DEFAULT_ACCOUNT_KEY_LENGTH=2048 |
51 | DEFAULT_DOMAIN_KEY_LENGTH=2048 | |
52 | ||
a746139c | 53 | DEFAULT_OPENSSL_BIN="openssl" |
54 | ||
4c3b3608 | 55 | VTYPE_HTTP="http-01" |
56 | VTYPE_DNS="dns-01" | |
08681f4a | 57 | VTYPE_ALPN="tls-alpn-01" |
e22bcf7c | 58 | |
0463b5d6 | 59 | LOCAL_ANY_ADDRESS="0.0.0.0" |
60 | ||
ec67a1b2 | 61 | DEFAULT_RENEW=60 |
523c7682 | 62 | |
3f4513b3 | 63 | NO_VALUE="no" |
64 | ||
3881f221 | 65 | W_DNS="dns" |
08681f4a | 66 | W_ALPN="alpn" |
64821ad4 | 67 | DNS_ALIAS_PREFIX="=" |
4c3b3608 | 68 | |
0e44f587 | 69 | MODE_STATELESS="stateless" |
70 | ||
ec603bee | 71 | STATE_VERIFIED="verified_ok" |
72 | ||
9d725af6 | 73 | NGINX="nginx:" |
03f8d6e9 | 74 | NGINX_START="#ACME_NGINX_START" |
75 | NGINX_END="#ACME_NGINX_END" | |
9d725af6 | 76 | |
88fab7d6 | 77 | BEGIN_CSR="-----BEGIN CERTIFICATE REQUEST-----" |
78 | END_CSR="-----END CERTIFICATE REQUEST-----" | |
79 | ||
80 | BEGIN_CERT="-----BEGIN CERTIFICATE-----" | |
81 | END_CERT="-----END CERTIFICATE-----" | |
82 | ||
45e386b2 | 83 | CONTENT_TYPE_JSON="application/jose+json" |
cc179731 | 84 | RENEW_SKIP=2 |
85 | ||
7690f73e | 86 | B64CONF_START="__ACME_BASE64__START_" |
87 | B64CONF_END="__ACME_BASE64__END_" | |
88 | ||
43822d37 | 89 | ECC_SEP="_" |
90 | ECC_SUFFIX="${ECC_SEP}ecc" | |
91 | ||
a73c5b33 | 92 | LOG_LEVEL_1=1 |
93 | LOG_LEVEL_2=2 | |
94 | LOG_LEVEL_3=3 | |
95 | DEFAULT_LOG_LEVEL="$LOG_LEVEL_1" | |
96 | ||
fc6cf4d9 | 97 | DEBUG_LEVEL_1=1 |
98 | DEBUG_LEVEL_2=2 | |
99 | DEBUG_LEVEL_3=3 | |
100 | DEBUG_LEVEL_DEFAULT=$DEBUG_LEVEL_1 | |
101 | DEBUG_LEVEL_NONE=0 | |
102 | ||
10eec7d4 | 103 | DOH_CLOUDFLARE=1 |
104 | DOH_GOOGLE=2 | |
105 | ||
e6e85b0c | 106 | HIDDEN_VALUE="[hidden](please add '--output-insecure' to see this value)" |
107 | ||
e2edf208 | 108 | SYSLOG_ERROR="user.error" |
fc6cf4d9 | 109 | SYSLOG_INFO="user.info" |
e2edf208 | 110 | SYSLOG_DEBUG="user.debug" |
111 | ||
fc6cf4d9 | 112 | #error |
113089be | 113 | SYSLOG_LEVEL_ERROR=3 |
fc6cf4d9 | 114 | #info |
113089be | 115 | SYSLOG_LEVEL_INFO=6 |
fc6cf4d9 | 116 | #debug |
113089be | 117 | SYSLOG_LEVEL_DEBUG=7 |
fc6cf4d9 | 118 | #debug2 |
113089be | 119 | SYSLOG_LEVEL_DEBUG_2=8 |
fc6cf4d9 | 120 | #debug3 |
113089be | 121 | SYSLOG_LEVEL_DEBUG_3=9 |
fc6cf4d9 | 122 | |
113089be | 123 | SYSLOG_LEVEL_DEFAULT=$SYSLOG_LEVEL_ERROR |
fc6cf4d9 | 124 | #none |
125 | SYSLOG_LEVEL_NONE=0 | |
126 | ||
b50e701c | 127 | NOTIFY_LEVEL_DISABLE=0 |
128 | NOTIFY_LEVEL_ERROR=1 | |
129 | NOTIFY_LEVEL_RENEW=2 | |
130 | NOTIFY_LEVEL_SKIP=3 | |
131 | ||
132 | NOTIFY_LEVEL_DEFAULT=$NOTIFY_LEVEL_RENEW | |
133 | ||
134 | NOTIFY_MODE_BULK=0 | |
135 | NOTIFY_MODE_CERT=1 | |
136 | ||
137 | NOTIFY_MODE_DEFAULT=$NOTIFY_MODE_BULK | |
138 | ||
09f74a9a | 139 | _DEBUG_WIKI="https://github.com/acmesh-official/acme.sh/wiki/How-to-debug-acme.sh" |
4c3b3608 | 140 | |
09f74a9a | 141 | _PREPARE_LINK="https://github.com/acmesh-official/acme.sh/wiki/Install-preparations" |
562a4c05 | 142 | |
09f74a9a | 143 | _STATELESS_WIKI="https://github.com/acmesh-official/acme.sh/wiki/Stateless-Mode" |
0e44f587 | 144 | |
09f74a9a | 145 | _DNS_ALIAS_WIKI="https://github.com/acmesh-official/acme.sh/wiki/DNS-alias-mode" |
875625b1 | 146 | |
09f74a9a | 147 | _DNS_MANUAL_WIKI="https://github.com/acmesh-official/acme.sh/wiki/dns-manual-mode" |
a0923622 | 148 | |
e3ebd582 | 149 | _DNS_API_WIKI="https://github.com/acmesh-official/acme.sh/wiki/dnsapi" |
150 | ||
09f74a9a | 151 | _NOTIFY_WIKI="https://github.com/acmesh-official/acme.sh/wiki/notify" |
b50e701c | 152 | |
09f74a9a | 153 | _SUDO_WIKI="https://github.com/acmesh-official/acme.sh/wiki/sudo" |
5bdfdfef | 154 | |
1041c9f9 | 155 | _REVOKE_WIKI="https://github.com/acmesh-official/acme.sh/wiki/revokecert" |
156 | ||
737e9e48 | 157 | _ZEROSSL_WIKI="https://github.com/acmesh-official/acme.sh/wiki/ZeroSSL.com-CA" |
158 | ||
d83d8552 | 159 | _SERVER_WIKI="https://github.com/acmesh-official/acme.sh/wiki/Server" |
160 | ||
e3ebd582 | 161 | _PREFERRED_CHAIN_WIKI="https://github.com/acmesh-official/acme.sh/wiki/Preferred-Chain" |
162 | ||
8a24275b | 163 | _DNSCHECK_WIKI="https://github.com/acmesh-official/acme.sh/wiki/dnscheck" |
164 | ||
309bec47 | 165 | _DNS_MANUAL_ERR="The dns manual mode can not renew automatically, you must issue it again manually. You'd better use the other modes instead." |
166 | ||
167 | _DNS_MANUAL_WARN="It seems that you are using dns manual mode. please take care: $_DNS_MANUAL_ERR" | |
168 | ||
a0923622 | 169 | _DNS_MANUAL_ERROR="It seems that you are using dns manual mode. Read this link first: $_DNS_MANUAL_WIKI" |
170 | ||
08ee072f | 171 | __INTERACTIVE="" |
4c2a3841 | 172 | if [ -t 1 ]; then |
08ee072f | 173 | __INTERACTIVE="1" |
174 | fi | |
00a50605 | 175 | |
43822d37 | 176 | __green() { |
137dc1ea | 177 | if [ "${__INTERACTIVE}${ACME_NO_COLOR:-0}" = "10" -o "${ACME_FORCE_COLOR}" = "1" ]; then |
671edc33 | 178 | printf '\33[1;32m%b\33[0m' "$1" |
137dc1ea | 179 | return |
2d12b689 | 180 | fi |
3576754c | 181 | printf -- "%b" "$1" |
43822d37 | 182 | } |
183 | ||
184 | __red() { | |
137dc1ea | 185 | if [ "${__INTERACTIVE}${ACME_NO_COLOR:-0}" = "10" -o "${ACME_FORCE_COLOR}" = "1" ]; then |
671edc33 | 186 | printf '\33[1;31m%b\33[0m' "$1" |
137dc1ea | 187 | return |
2d12b689 | 188 | fi |
3576754c | 189 | printf -- "%b" "$1" |
43822d37 | 190 | } |
00a50605 | 191 | |
a73c5b33 | 192 | _printargs() { |
65a7d569 | 193 | _exitstatus="$?" |
569d6c55 | 194 | if [ -z "$NO_TIMESTAMP" ] || [ "$NO_TIMESTAMP" = "0" ]; then |
195 | printf -- "%s" "[$(date)] " | |
196 | fi | |
4c2a3841 | 197 | if [ -z "$2" ]; then |
569d6c55 | 198 | printf -- "%s" "$1" |
43822d37 | 199 | else |
569d6c55 | 200 | printf -- "%s" "$1='$2'" |
43822d37 | 201 | fi |
a73c5b33 | 202 | printf "\n" |
ac9f6e3a | 203 | # return the saved exit status |
52351d7d | 204 | return "$_exitstatus" |
43822d37 | 205 | } |
206 | ||
9d548d81 | 207 | _dlg_versions() { |
208 | echo "Diagnosis versions: " | |
851fedf7 | 209 | echo "openssl:$ACME_OPENSSL_BIN" |
d8ba26e6 | 210 | if _exists "${ACME_OPENSSL_BIN:-openssl}"; then |
211 | ${ACME_OPENSSL_BIN:-openssl} version 2>&1 | |
9d548d81 | 212 | else |
eca57bee | 213 | echo "$ACME_OPENSSL_BIN doesn't exist." |
9d548d81 | 214 | fi |
4c2a3841 | 215 | |
9d548d81 | 216 | echo "apache:" |
4c2a3841 | 217 | if [ "$_APACHECTL" ] && _exists "$_APACHECTL"; then |
e735d8d4 | 218 | $_APACHECTL -V 2>&1 |
9d548d81 | 219 | else |
eca57bee | 220 | echo "apache doesn't exist." |
9d548d81 | 221 | fi |
4c2a3841 | 222 | |
326c386b | 223 | echo "nginx:" |
224 | if _exists "nginx"; then | |
225 | nginx -V 2>&1 | |
226 | else | |
eca57bee | 227 | echo "nginx doesn't exist." |
326c386b | 228 | fi |
229 | ||
3794b5cb | 230 | echo "socat:" |
231 | if _exists "socat"; then | |
463df9e4 | 232 | socat -V 2>&1 |
9d548d81 | 233 | else |
eca57bee | 234 | _debug "socat doesn't exist." |
9d548d81 | 235 | fi |
236 | } | |
43822d37 | 237 | |
e2edf208 | 238 | #class |
239 | _syslog() { | |
65a7d569 | 240 | _exitstatus="$?" |
fc6cf4d9 | 241 | if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" = "$SYSLOG_LEVEL_NONE" ]; then |
e2edf208 | 242 | return |
243 | fi | |
244 | _logclass="$1" | |
245 | shift | |
5b3e3d9c | 246 | if [ -z "$__logger_i" ]; then |
247 | if _contains "$(logger --help 2>&1)" "-i"; then | |
248 | __logger_i="logger -i" | |
249 | else | |
250 | __logger_i="logger" | |
251 | fi | |
252 | fi | |
253 | $__logger_i -t "$PROJECT_NAME" -p "$_logclass" "$(_printargs "$@")" >/dev/null 2>&1 | |
65a7d569 | 254 | return "$_exitstatus" |
e2edf208 | 255 | } |
256 | ||
a73c5b33 | 257 | _log() { |
258 | [ -z "$LOG_FILE" ] && return | |
95e06de5 | 259 | _printargs "$@" >>"$LOG_FILE" |
a73c5b33 | 260 | } |
261 | ||
262 | _info() { | |
fc6cf4d9 | 263 | _log "$@" |
113089be | 264 | if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_INFO" ]; then |
fc6cf4d9 | 265 | _syslog "$SYSLOG_INFO" "$@" |
266 | fi | |
a73c5b33 | 267 | _printargs "$@" |
4c3b3608 | 268 | } |
269 | ||
270 | _err() { | |
fc6cf4d9 | 271 | _syslog "$SYSLOG_ERROR" "$@" |
272 | _log "$@" | |
569d6c55 | 273 | if [ -z "$NO_TIMESTAMP" ] || [ "$NO_TIMESTAMP" = "0" ]; then |
274 | printf -- "%s" "[$(date)] " >&2 | |
275 | fi | |
4c2a3841 | 276 | if [ -z "$2" ]; then |
65de3110 | 277 | __red "$1" >&2 |
278 | else | |
279 | __red "$1='$2'" >&2 | |
280 | fi | |
b19ba13a | 281 | printf "\n" >&2 |
4c3b3608 | 282 | return 1 |
283 | } | |
284 | ||
43822d37 | 285 | _usage() { |
4c2a3841 | 286 | __red "$@" >&2 |
65de3110 | 287 | printf "\n" >&2 |
43822d37 | 288 | } |
289 | ||
bba5376a JV |
290 | __debug_bash_helper() { |
291 | # At this point only do for --debug 3 | |
292 | if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -lt "$DEBUG_LEVEL_3" ]; then | |
bba5376a JV |
293 | return |
294 | fi | |
295 | # Return extra debug info when running with bash, otherwise return empty | |
296 | # string. | |
297 | if [ -z "${BASH_VERSION}" ]; then | |
bba5376a JV |
298 | return |
299 | fi | |
300 | # We are a bash shell at this point, return the filename, function name, and | |
301 | # line number as a string | |
302 | _dbh_saveIFS=$IFS | |
303 | IFS=" " | |
adce8f52 JV |
304 | # Must use eval or syntax error happens under dash. The eval should use |
305 | # single quotes as older versions of busybox had a bug with double quotes and | |
306 | # eval. | |
bba5376a JV |
307 | # Use 'caller 1' as we want one level up the stack as we should be called |
308 | # by one of the _debug* functions | |
adce8f52 | 309 | eval '_dbh_called=($(caller 1))' |
bba5376a | 310 | IFS=$_dbh_saveIFS |
adce8f52 | 311 | eval '_dbh_file=${_dbh_called[2]}' |
bba5376a JV |
312 | if [ -n "${_script_home}" ]; then |
313 | # Trim off the _script_home directory name | |
adce8f52 | 314 | eval '_dbh_file=${_dbh_file#$_script_home/}' |
bba5376a | 315 | fi |
adce8f52 JV |
316 | eval '_dbh_function=${_dbh_called[1]}' |
317 | eval '_dbh_lineno=${_dbh_called[0]}' | |
bba5376a JV |
318 | printf "%-40s " "$_dbh_file:${_dbh_function}:${_dbh_lineno}" |
319 | } | |
320 | ||
c60883ef | 321 | _debug() { |
fc6cf4d9 | 322 | if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_1" ]; then |
323 | _log "$@" | |
a73c5b33 | 324 | fi |
113089be | 325 | if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG" ]; then |
fc6cf4d9 | 326 | _syslog "$SYSLOG_DEBUG" "$@" |
327 | fi | |
328 | if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_1" ]; then | |
bba5376a JV |
329 | _bash_debug=$(__debug_bash_helper) |
330 | _printargs "${_bash_debug}$@" >&2 | |
c60883ef | 331 | fi |
c60883ef | 332 | } |
333 | ||
e6e85b0c | 334 | #output the sensitive messages |
335 | _secure_debug() { | |
336 | if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_1" ]; then | |
337 | if [ "$OUTPUT_INSECURE" = "1" ]; then | |
338 | _log "$@" | |
339 | else | |
340 | _log "$1" "$HIDDEN_VALUE" | |
341 | fi | |
342 | fi | |
343 | if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG" ]; then | |
344 | _syslog "$SYSLOG_DEBUG" "$1" "$HIDDEN_VALUE" | |
345 | fi | |
346 | if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_1" ]; then | |
347 | if [ "$OUTPUT_INSECURE" = "1" ]; then | |
348 | _printargs "$@" >&2 | |
349 | else | |
350 | _printargs "$1" "$HIDDEN_VALUE" >&2 | |
351 | fi | |
352 | fi | |
353 | } | |
354 | ||
a63b05a9 | 355 | _debug2() { |
fc6cf4d9 | 356 | if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_2" ]; then |
357 | _log "$@" | |
a73c5b33 | 358 | fi |
113089be | 359 | if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG_2" ]; then |
fc6cf4d9 | 360 | _syslog "$SYSLOG_DEBUG" "$@" |
361 | fi | |
362 | if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_2" ]; then | |
bba5376a JV |
363 | _bash_debug=$(__debug_bash_helper) |
364 | _printargs "${_bash_debug}$@" >&2 | |
a63b05a9 | 365 | fi |
a63b05a9 | 366 | } |
367 | ||
e6e85b0c | 368 | _secure_debug2() { |
369 | if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_2" ]; then | |
370 | if [ "$OUTPUT_INSECURE" = "1" ]; then | |
371 | _log "$@" | |
372 | else | |
373 | _log "$1" "$HIDDEN_VALUE" | |
374 | fi | |
375 | fi | |
376 | if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG_2" ]; then | |
377 | _syslog "$SYSLOG_DEBUG" "$1" "$HIDDEN_VALUE" | |
378 | fi | |
379 | if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_2" ]; then | |
380 | if [ "$OUTPUT_INSECURE" = "1" ]; then | |
381 | _printargs "$@" >&2 | |
382 | else | |
383 | _printargs "$1" "$HIDDEN_VALUE" >&2 | |
384 | fi | |
385 | fi | |
386 | } | |
387 | ||
22ea4004 | 388 | _debug3() { |
fc6cf4d9 | 389 | if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_3" ]; then |
390 | _log "$@" | |
391 | fi | |
113089be | 392 | if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG_3" ]; then |
fc6cf4d9 | 393 | _syslog "$SYSLOG_DEBUG" "$@" |
a73c5b33 | 394 | fi |
fc6cf4d9 | 395 | if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_3" ]; then |
bba5376a JV |
396 | _bash_debug=$(__debug_bash_helper) |
397 | _printargs "${_bash_debug}$@" >&2 | |
22ea4004 | 398 | fi |
22ea4004 | 399 | } |
400 | ||
e6e85b0c | 401 | _secure_debug3() { |
402 | if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_3" ]; then | |
403 | if [ "$OUTPUT_INSECURE" = "1" ]; then | |
404 | _log "$@" | |
405 | else | |
406 | _log "$1" "$HIDDEN_VALUE" | |
407 | fi | |
408 | fi | |
409 | if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG_3" ]; then | |
410 | _syslog "$SYSLOG_DEBUG" "$1" "$HIDDEN_VALUE" | |
411 | fi | |
412 | if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_3" ]; then | |
413 | if [ "$OUTPUT_INSECURE" = "1" ]; then | |
414 | _printargs "$@" >&2 | |
415 | else | |
416 | _printargs "$1" "$HIDDEN_VALUE" >&2 | |
417 | fi | |
418 | fi | |
419 | } | |
420 | ||
c4bf5eef | 421 | _upper_case() { |
422 | # shellcheck disable=SC2018,SC2019 | |
423 | tr 'a-z' 'A-Z' | |
424 | } | |
425 | ||
426 | _lower_case() { | |
427 | # shellcheck disable=SC2018,SC2019 | |
428 | tr 'A-Z' 'a-z' | |
429 | } | |
430 | ||
4c2a3841 | 431 | _startswith() { |
dceb3aca | 432 | _str="$1" |
433 | _sub="$2" | |
19539575 | 434 | echo "$_str" | grep "^$_sub" >/dev/null 2>&1 |
dceb3aca | 435 | } |
436 | ||
4c2a3841 | 437 | _endswith() { |
43822d37 | 438 | _str="$1" |
439 | _sub="$2" | |
440 | echo "$_str" | grep -- "$_sub\$" >/dev/null 2>&1 | |
441 | } | |
442 | ||
4c2a3841 | 443 | _contains() { |
dceb3aca | 444 | _str="$1" |
445 | _sub="$2" | |
43822d37 | 446 | echo "$_str" | grep -- "$_sub" >/dev/null 2>&1 |
dceb3aca | 447 | } |
448 | ||
c53da1ef | 449 | _hasfield() { |
450 | _str="$1" | |
451 | _field="$2" | |
452 | _sep="$3" | |
4c2a3841 | 453 | if [ -z "$_field" ]; then |
43822d37 | 454 | _usage "Usage: str field [sep]" |
c53da1ef | 455 | return 1 |
456 | fi | |
4c2a3841 | 457 | |
458 | if [ -z "$_sep" ]; then | |
c53da1ef | 459 | _sep="," |
460 | fi | |
4c2a3841 | 461 | |
6cf7be4b | 462 | for f in $(echo "$_str" | tr "$_sep" ' '); do |
4c2a3841 | 463 | if [ "$f" = "$_field" ]; then |
0c9546cc | 464 | _debug2 "'$_str' contains '$_field'" |
c53da1ef | 465 | return 0 #contains ok |
466 | fi | |
467 | done | |
0c9546cc | 468 | _debug2 "'$_str' does not contain '$_field'" |
3c07f57a | 469 | return 1 #not contains |
c53da1ef | 470 | } |
471 | ||
422dd1fa | 472 | # str index [sep] |
4c2a3841 | 473 | _getfield() { |
0463b5d6 | 474 | _str="$1" |
475 | _findex="$2" | |
476 | _sep="$3" | |
4c2a3841 | 477 | |
478 | if [ -z "$_findex" ]; then | |
0463b5d6 | 479 | _usage "Usage: str field [sep]" |
480 | return 1 | |
481 | fi | |
4c2a3841 | 482 | |
483 | if [ -z "$_sep" ]; then | |
0463b5d6 | 484 | _sep="," |
485 | fi | |
486 | ||
201aa244 | 487 | _ffi="$_findex" |
4c2a3841 | 488 | while [ "$_ffi" -gt "0" ]; do |
201aa244 | 489 | _fv="$(echo "$_str" | cut -d "$_sep" -f "$_ffi")" |
4c2a3841 | 490 | if [ "$_fv" ]; then |
0463b5d6 | 491 | printf -- "%s" "$_fv" |
492 | return 0 | |
493 | fi | |
95e06de5 | 494 | _ffi="$(_math "$_ffi" - 1)" |
0463b5d6 | 495 | done |
4c2a3841 | 496 | |
0463b5d6 | 497 | printf -- "%s" "$_str" |
498 | ||
499 | } | |
500 | ||
4c2a3841 | 501 | _exists() { |
c60883ef | 502 | cmd="$1" |
4c2a3841 | 503 | if [ -z "$cmd" ]; then |
43822d37 | 504 | _usage "Usage: _exists cmd" |
c60883ef | 505 | return 1 |
506 | fi | |
82dc2244 | 507 | |
508 | if eval type type >/dev/null 2>&1; then | |
509 | eval type "$cmd" >/dev/null 2>&1 | |
510 | elif command >/dev/null 2>&1; then | |
19539575 | 511 | command -v "$cmd" >/dev/null 2>&1 |
ce4be4e9 | 512 | else |
e591d5cf | 513 | which "$cmd" >/dev/null 2>&1 |
eac18b1c | 514 | fi |
c60883ef | 515 | ret="$?" |
690a5e20 | 516 | _debug3 "$cmd exists=$ret" |
c60883ef | 517 | return $ret |
518 | } | |
519 | ||
00a50605 | 520 | #a + b |
4c2a3841 | 521 | _math() { |
be68fbd4 | 522 | _m_opts="$@" |
523 | printf "%s" "$(($_m_opts))" | |
00a50605 | 524 | } |
525 | ||
526 | _h_char_2_dec() { | |
527 | _ch=$1 | |
528 | case "${_ch}" in | |
19c43451 | 529 | a | A) |
530 | printf "10" | |
531 | ;; | |
532 | b | B) | |
533 | printf "11" | |
534 | ;; | |
535 | c | C) | |
536 | printf "12" | |
537 | ;; | |
538 | d | D) | |
539 | printf "13" | |
540 | ;; | |
541 | e | E) | |
542 | printf "14" | |
543 | ;; | |
544 | f | F) | |
545 | printf "15" | |
546 | ;; | |
547 | *) | |
548 | printf "%s" "$_ch" | |
549 | ;; | |
19539575 | 550 | esac |
00a50605 | 551 | |
552 | } | |
553 | ||
fac1e367 | 554 | _URGLY_PRINTF="" |
4c2a3841 | 555 | if [ "$(printf '\x41')" != 'A' ]; then |
fac1e367 | 556 | _URGLY_PRINTF=1 |
557 | fi | |
558 | ||
f8bcfeb2 | 559 | _ESCAPE_XARGS="" |
841b7627 | 560 | if _exists xargs && [ "$(printf %s '\\x41' | xargs printf)" = 'A' ]; then |
f8bcfeb2 | 561 | _ESCAPE_XARGS=1 |
562 | fi | |
563 | ||
4c3b3608 | 564 | _h2b() { |
c384ed96 AK |
565 | if _exists xxd; then |
566 | if _contains "$(xxd --help 2>&1)" "assumes -c30"; then | |
567 | if xxd -r -p -c 9999 2>/dev/null; then | |
568 | return | |
569 | fi | |
570 | else | |
571 | if xxd -r -p 2>/dev/null; then | |
572 | return | |
573 | fi | |
574 | fi | |
b420ec6c | 575 | fi |
576 | ||
4c3b3608 | 577 | hex=$(cat) |
fa93d68b | 578 | ic="" |
579 | jc="" | |
b420ec6c | 580 | _debug2 _URGLY_PRINTF "$_URGLY_PRINTF" |
581 | if [ -z "$_URGLY_PRINTF" ]; then | |
f8bcfeb2 | 582 | if [ "$_ESCAPE_XARGS" ] && _exists xargs; then |
fa93d68b | 583 | _debug2 "xargs" |
ded4469e | 584 | echo "$hex" | _upper_case | sed 's/\([0-9A-F]\{2\}\)/\\\\\\x\1/g' | xargs printf |
fa93d68b | 585 | else |
ded4469e | 586 | for h in $(echo "$hex" | _upper_case | sed 's/\([0-9A-F]\{2\}\)/ \1/g'); do |
fa93d68b | 587 | if [ -z "$h" ]; then |
588 | break | |
589 | fi | |
590 | printf "\x$h%s" | |
591 | done | |
592 | fi | |
b420ec6c | 593 | else |
ded4469e | 594 | for c in $(echo "$hex" | _upper_case | sed 's/\([0-9A-F]\)/ \1/g'); do |
fa93d68b | 595 | if [ -z "$ic" ]; then |
596 | ic=$c | |
597 | continue | |
00a50605 | 598 | fi |
fa93d68b | 599 | jc=$c |
19539575 | 600 | ic="$(_h_char_2_dec "$ic")" |
601 | jc="$(_h_char_2_dec "$jc")" | |
e51bef6d | 602 | printf '\'"$(printf "%o" "$(_math "$ic" \* 16 + $jc)")""%s" |
fa93d68b | 603 | ic="" |
604 | jc="" | |
b420ec6c | 605 | done |
606 | fi | |
e591d5cf | 607 | |
4c3b3608 | 608 | } |
609 | ||
542d7977 | 610 | _is_solaris() { |
611 | _contains "${__OS__:=$(uname -a)}" "solaris" || _contains "${__OS__:=$(uname -a)}" "SunOS" | |
612 | } | |
613 | ||
9bdb799b | 614 | #_ascii_hex str |
615 | #this can only process ascii chars, should only be used when od command is missing as a backup way. | |
616 | _ascii_hex() { | |
617 | _debug2 "Using _ascii_hex" | |
618 | _str="$1" | |
619 | _str_len=${#_str} | |
620 | _h_i=1 | |
621 | while [ "$_h_i" -le "$_str_len" ]; do | |
622 | _str_c="$(printf "%s" "$_str" | cut -c "$_h_i")" | |
623 | printf " %02x" "'$_str_c" | |
624 | _h_i="$(_math "$_h_i" + 1)" | |
625 | done | |
626 | } | |
627 | ||
542d7977 | 628 | #stdin output hexstr splited by one space |
629 | #input:"abc" | |
630 | #output: " 61 62 63" | |
631 | _hex_dump() { | |
4e4a6d83 | 632 | if _exists od; then |
633 | od -A n -v -t x1 | tr -s " " | sed 's/ $//' | tr -d "\r\t\n" | |
634 | elif _exists hexdump; then | |
635 | _debug3 "using hexdump" | |
636 | hexdump -v -e '/1 ""' -e '/1 " %02x" ""' | |
637 | elif _exists xxd; then | |
638 | _debug3 "using xxd" | |
639 | xxd -ps -c 20 -i | sed "s/ 0x/ /g" | tr -d ",\n" | tr -s " " | |
640 | else | |
641 | _debug3 "using _ascii_hex" | |
9bdb799b | 642 | str=$(cat) |
643 | _ascii_hex "$str" | |
644 | fi | |
542d7977 | 645 | } |
646 | ||
647 | #url encode, no-preserved chars | |
648 | #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 | |
649 | #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 | |
650 | ||
651 | #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 | |
652 | #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 | |
653 | ||
654 | #0 1 2 3 4 5 6 7 8 9 - _ . ~ | |
655 | #30 31 32 33 34 35 36 37 38 39 2d 5f 2e 7e | |
656 | ||
657 | #stdin stdout | |
658 | _url_encode() { | |
659 | _hex_str=$(_hex_dump) | |
660 | _debug3 "_url_encode" | |
661 | _debug3 "_hex_str" "$_hex_str" | |
662 | for _hex_code in $_hex_str; do | |
663 | #upper case | |
664 | case "${_hex_code}" in | |
19c43451 | 665 | "41") |
666 | printf "%s" "A" | |
667 | ;; | |
668 | "42") | |
669 | printf "%s" "B" | |
670 | ;; | |
671 | "43") | |
672 | printf "%s" "C" | |
673 | ;; | |
674 | "44") | |
675 | printf "%s" "D" | |
676 | ;; | |
677 | "45") | |
678 | printf "%s" "E" | |
679 | ;; | |
680 | "46") | |
681 | printf "%s" "F" | |
682 | ;; | |
683 | "47") | |
684 | printf "%s" "G" | |
685 | ;; | |
686 | "48") | |
687 | printf "%s" "H" | |
688 | ;; | |
689 | "49") | |
690 | printf "%s" "I" | |
691 | ;; | |
692 | "4a") | |
693 | printf "%s" "J" | |
694 | ;; | |
695 | "4b") | |
696 | printf "%s" "K" | |
697 | ;; | |
698 | "4c") | |
699 | printf "%s" "L" | |
700 | ;; | |
701 | "4d") | |
702 | printf "%s" "M" | |
703 | ;; | |
704 | "4e") | |
705 | printf "%s" "N" | |
706 | ;; | |
707 | "4f") | |
708 | printf "%s" "O" | |
709 | ;; | |
710 | "50") | |
711 | printf "%s" "P" | |
712 | ;; | |
713 | "51") | |
714 | printf "%s" "Q" | |
715 | ;; | |
716 | "52") | |
717 | printf "%s" "R" | |
718 | ;; | |
719 | "53") | |
720 | printf "%s" "S" | |
721 | ;; | |
722 | "54") | |
723 | printf "%s" "T" | |
724 | ;; | |
725 | "55") | |
726 | printf "%s" "U" | |
727 | ;; | |
728 | "56") | |
729 | printf "%s" "V" | |
730 | ;; | |
731 | "57") | |
732 | printf "%s" "W" | |
733 | ;; | |
734 | "58") | |
735 | printf "%s" "X" | |
736 | ;; | |
737 | "59") | |
738 | printf "%s" "Y" | |
739 | ;; | |
740 | "5a") | |
741 | printf "%s" "Z" | |
742 | ;; | |
c3b1eb08 | 743 | |
744 | #lower case | |
19c43451 | 745 | "61") |
746 | printf "%s" "a" | |
747 | ;; | |
748 | "62") | |
749 | printf "%s" "b" | |
750 | ;; | |
751 | "63") | |
752 | printf "%s" "c" | |
753 | ;; | |
754 | "64") | |
755 | printf "%s" "d" | |
756 | ;; | |
757 | "65") | |
758 | printf "%s" "e" | |
759 | ;; | |
760 | "66") | |
761 | printf "%s" "f" | |
762 | ;; | |
763 | "67") | |
764 | printf "%s" "g" | |
765 | ;; | |
766 | "68") | |
767 | printf "%s" "h" | |
768 | ;; | |
769 | "69") | |
770 | printf "%s" "i" | |
771 | ;; | |
772 | "6a") | |
773 | printf "%s" "j" | |
774 | ;; | |
775 | "6b") | |
776 | printf "%s" "k" | |
777 | ;; | |
778 | "6c") | |
779 | printf "%s" "l" | |
780 | ;; | |
781 | "6d") | |
782 | printf "%s" "m" | |
783 | ;; | |
784 | "6e") | |
785 | printf "%s" "n" | |
786 | ;; | |
787 | "6f") | |
788 | printf "%s" "o" | |
789 | ;; | |
790 | "70") | |
791 | printf "%s" "p" | |
792 | ;; | |
793 | "71") | |
794 | printf "%s" "q" | |
795 | ;; | |
796 | "72") | |
797 | printf "%s" "r" | |
798 | ;; | |
799 | "73") | |
800 | printf "%s" "s" | |
801 | ;; | |
802 | "74") | |
803 | printf "%s" "t" | |
804 | ;; | |
805 | "75") | |
806 | printf "%s" "u" | |
807 | ;; | |
808 | "76") | |
809 | printf "%s" "v" | |
810 | ;; | |
811 | "77") | |
812 | printf "%s" "w" | |
813 | ;; | |
814 | "78") | |
815 | printf "%s" "x" | |
816 | ;; | |
817 | "79") | |
818 | printf "%s" "y" | |
819 | ;; | |
820 | "7a") | |
821 | printf "%s" "z" | |
822 | ;; | |
c3b1eb08 | 823 | #numbers |
19c43451 | 824 | "30") |
825 | printf "%s" "0" | |
826 | ;; | |
827 | "31") | |
828 | printf "%s" "1" | |
829 | ;; | |
830 | "32") | |
831 | printf "%s" "2" | |
832 | ;; | |
833 | "33") | |
834 | printf "%s" "3" | |
835 | ;; | |
836 | "34") | |
837 | printf "%s" "4" | |
838 | ;; | |
839 | "35") | |
840 | printf "%s" "5" | |
841 | ;; | |
842 | "36") | |
843 | printf "%s" "6" | |
844 | ;; | |
845 | "37") | |
846 | printf "%s" "7" | |
847 | ;; | |
848 | "38") | |
849 | printf "%s" "8" | |
850 | ;; | |
851 | "39") | |
852 | printf "%s" "9" | |
853 | ;; | |
854 | "2d") | |
855 | printf "%s" "-" | |
856 | ;; | |
857 | "5f") | |
858 | printf "%s" "_" | |
859 | ;; | |
860 | "2e") | |
861 | printf "%s" "." | |
862 | ;; | |
863 | "7e") | |
864 | printf "%s" "~" | |
865 | ;; | |
866 | #other hex | |
867 | *) | |
868 | printf '%%%s' "$_hex_code" | |
869 | ;; | |
542d7977 | 870 | esac |
e009ec8b | 871 | done |
872 | } | |
873 | ||
b50e701c | 874 | _json_encode() { |
875 | _j_str="$(sed 's/"/\\"/g' | sed "s/\r/\\r/g")" | |
876 | _debug3 "_json_encode" | |
877 | _debug3 "_j_str" "$_j_str" | |
878 | echo "$_j_str" | _hex_dump | _lower_case | sed 's/0a/5c 6e/g' | tr -d ' ' | _h2b | tr -d "\r\n" | |
879 | } | |
880 | ||
93de1e49 | 881 | #from: http:\/\/ to http:// |
882 | _json_decode() { | |
883 | _j_str="$(sed 's#\\/#/#g')" | |
884 | _debug3 "_json_decode" | |
885 | _debug3 "_j_str" "$_j_str" | |
886 | echo "$_j_str" | |
887 | } | |
888 | ||
c60883ef | 889 | #options file |
890 | _sed_i() { | |
891 | options="$1" | |
892 | filename="$2" | |
4c2a3841 | 893 | if [ -z "$filename" ]; then |
43822d37 | 894 | _usage "Usage:_sed_i options filename" |
c60883ef | 895 | return 1 |
896 | fi | |
14f3dbb7 | 897 | _debug2 options "$options" |
898 | if sed -h 2>&1 | grep "\-i\[SUFFIX]" >/dev/null 2>&1; then | |
c60883ef | 899 | _debug "Using sed -i" |
14f3dbb7 | 900 | sed -i "$options" "$filename" |
c60883ef | 901 | else |
902 | _debug "No -i support in sed" | |
19539575 | 903 | text="$(cat "$filename")" |
4c2a3841 | 904 | echo "$text" | sed "$options" >"$filename" |
c60883ef | 905 | fi |
906 | } | |
907 | ||
22ea4004 | 908 | _egrep_o() { |
a3c0c754 | 909 | if ! egrep -o "$1" 2>/dev/null; then |
22ea4004 | 910 | sed -n 's/.*\('"$1"'\).*/\1/p' |
22ea4004 | 911 | fi |
912 | } | |
913 | ||
88fab7d6 | 914 | #Usage: file startline endline |
915 | _getfile() { | |
916 | filename="$1" | |
917 | startline="$2" | |
918 | endline="$3" | |
4c2a3841 | 919 | if [ -z "$endline" ]; then |
43822d37 | 920 | _usage "Usage: file startline endline" |
88fab7d6 | 921 | return 1 |
922 | fi | |
4c2a3841 | 923 | |
924 | i="$(grep -n -- "$startline" "$filename" | cut -d : -f 1)" | |
925 | if [ -z "$i" ]; then | |
88fab7d6 | 926 | _err "Can not find start line: $startline" |
927 | return 1 | |
928 | fi | |
19539575 | 929 | i="$(_math "$i" + 1)" |
930 | _debug i "$i" | |
4c2a3841 | 931 | |
932 | j="$(grep -n -- "$endline" "$filename" | cut -d : -f 1)" | |
933 | if [ -z "$j" ]; then | |
88fab7d6 | 934 | _err "Can not find end line: $endline" |
935 | return 1 | |
936 | fi | |
19539575 | 937 | j="$(_math "$j" - 1)" |
938 | _debug j "$j" | |
4c2a3841 | 939 | |
940 | sed -n "$i,${j}p" "$filename" | |
88fab7d6 | 941 | |
942 | } | |
943 | ||
944 | #Usage: multiline | |
4c3b3608 | 945 | _base64() { |
ec9975c3 | 946 | [ "" ] #urgly |
4c2a3841 | 947 | if [ "$1" ]; then |
24d2a8b9 | 948 | _debug3 "base64 multiline:'$1'" |
d8ba26e6 | 949 | ${ACME_OPENSSL_BIN:-openssl} base64 -e |
88fab7d6 | 950 | else |
4d8b99a3 | 951 | _debug3 "base64 single line." |
d8ba26e6 | 952 | ${ACME_OPENSSL_BIN:-openssl} base64 -e | tr -d '\r\n' |
88fab7d6 | 953 | fi |
954 | } | |
955 | ||
956 | #Usage: multiline | |
957 | _dbase64() { | |
4c2a3841 | 958 | if [ "$1" ]; then |
d8ba26e6 | 959 | ${ACME_OPENSSL_BIN:-openssl} base64 -d -A |
88fab7d6 | 960 | else |
d8ba26e6 | 961 | ${ACME_OPENSSL_BIN:-openssl} base64 -d |
88fab7d6 | 962 | fi |
963 | } | |
964 | ||
183063a2 | 965 | #file |
966 | _checkcert() { | |
967 | _cf="$1" | |
968 | if [ "$DEBUG" ]; then | |
2c7d2230 | 969 | ${ACME_OPENSSL_BIN:-openssl} x509 -noout -text -in "$_cf" |
183063a2 | 970 | else |
2c7d2230 | 971 | ${ACME_OPENSSL_BIN:-openssl} x509 -noout -text -in "$_cf" >/dev/null 2>&1 |
183063a2 | 972 | fi |
973 | } | |
974 | ||
e22bcf7c | 975 | #Usage: hashalg [outputhex] |
88fab7d6 | 976 | #Output Base64-encoded digest |
977 | _digest() { | |
978 | alg="$1" | |
4c2a3841 | 979 | if [ -z "$alg" ]; then |
43822d37 | 980 | _usage "Usage: _digest hashalg" |
88fab7d6 | 981 | return 1 |
982 | fi | |
4c2a3841 | 983 | |
e22bcf7c | 984 | outputhex="$2" |
4c2a3841 | 985 | |
c7b16249 | 986 | if [ "$alg" = "sha256" ] || [ "$alg" = "sha1" ] || [ "$alg" = "md5" ]; then |
4c2a3841 | 987 | if [ "$outputhex" ]; then |
d8ba26e6 | 988 | ${ACME_OPENSSL_BIN:-openssl} dgst -"$alg" -hex | cut -d = -f 2 | tr -d ' ' |
e22bcf7c | 989 | else |
d8ba26e6 | 990 | ${ACME_OPENSSL_BIN:-openssl} dgst -"$alg" -binary | _base64 |
b001840d | 991 | fi |
992 | else | |
993 | _err "$alg is not supported yet" | |
994 | return 1 | |
995 | fi | |
996 | ||
997 | } | |
998 | ||
e009ec8b | 999 | #Usage: hashalg secret_hex [outputhex] |
1000 | #Output binary hmac | |
b001840d | 1001 | _hmac() { |
1002 | alg="$1" | |
e009ec8b | 1003 | secret_hex="$2" |
b001840d | 1004 | outputhex="$3" |
4c2a3841 | 1005 | |
e009ec8b | 1006 | if [ -z "$secret_hex" ]; then |
4c2a3841 | 1007 | _usage "Usage: _hmac hashalg secret [outputhex]" |
b001840d | 1008 | return 1 |
1009 | fi | |
1010 | ||
a6014bf0 | 1011 | if [ "$alg" = "sha256" ] || [ "$alg" = "sha1" ]; then |
4c2a3841 | 1012 | if [ "$outputhex" ]; then |
d8ba26e6 | 1013 | (${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 ' ' |
e22bcf7c | 1014 | else |
d8ba26e6 | 1015 | ${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 |
e22bcf7c | 1016 | fi |
88fab7d6 | 1017 | else |
1018 | _err "$alg is not supported yet" | |
1019 | return 1 | |
1020 | fi | |
1021 | ||
1022 | } | |
1023 | ||
1024 | #Usage: keyfile hashalg | |
1025 | #Output: Base64-encoded signature value | |
1026 | _sign() { | |
1027 | keyfile="$1" | |
1028 | alg="$2" | |
4c2a3841 | 1029 | if [ -z "$alg" ]; then |
43822d37 | 1030 | _usage "Usage: _sign keyfile hashalg" |
88fab7d6 | 1031 | return 1 |
1032 | fi | |
4c2a3841 | 1033 | |
d8ba26e6 | 1034 | _sign_openssl="${ACME_OPENSSL_BIN:-openssl} dgst -sign $keyfile " |
4c2a3841 | 1035 | |
63031fb2 | 1036 | if grep "BEGIN RSA PRIVATE KEY" "$keyfile" >/dev/null 2>&1 || grep "BEGIN PRIVATE KEY" "$keyfile" >/dev/null 2>&1; then |
f87890cb | 1037 | $_sign_openssl -$alg | _base64 |
4c2a3841 | 1038 | elif grep "BEGIN EC PRIVATE KEY" "$keyfile" >/dev/null 2>&1; then |
f87890cb | 1039 | if ! _signedECText="$($_sign_openssl -sha$__ECC_KEY_LEN | ${ACME_OPENSSL_BIN:-openssl} asn1parse -inform DER)"; then |
67184d7b | 1040 | _err "Sign failed: $_sign_openssl" |
1041 | _err "Key file: $keyfile" | |
357b514b | 1042 | _err "Key content:$(wc -l <"$keyfile") lines" |
67184d7b | 1043 | return 1 |
1044 | fi | |
998783eb | 1045 | _debug3 "_signedECText" "$_signedECText" |
1046 | _ec_r="$(echo "$_signedECText" | _head_n 2 | _tail_n 1 | cut -d : -f 4 | tr -d "\r\n")" | |
998783eb | 1047 | _ec_s="$(echo "$_signedECText" | _head_n 3 | _tail_n 1 | cut -d : -f 4 | tr -d "\r\n")" |
a3295476 | 1048 | if [ "$__ECC_KEY_LEN" -eq "256" ]; then |
1049 | while [ "${#_ec_r}" -lt "64" ]; do | |
f31debc0 | 1050 | _ec_r="0${_ec_r}" |
6654d7a9 | 1051 | done |
a3295476 | 1052 | while [ "${#_ec_s}" -lt "64" ]; do |
f31debc0 | 1053 | _ec_s="0${_ec_s}" |
a3295476 | 1054 | done |
1055 | fi | |
1056 | if [ "$__ECC_KEY_LEN" -eq "384" ]; then | |
1057 | while [ "${#_ec_r}" -lt "96" ]; do | |
f31debc0 | 1058 | _ec_r="0${_ec_r}" |
6654d7a9 | 1059 | done |
a3295476 | 1060 | while [ "${#_ec_s}" -lt "96" ]; do |
f31debc0 | 1061 | _ec_s="0${_ec_s}" |
a3295476 | 1062 | done |
1063 | fi | |
1064 | if [ "$__ECC_KEY_LEN" -eq "512" ]; then | |
1065 | while [ "${#_ec_r}" -lt "132" ]; do | |
f31debc0 | 1066 | _ec_r="0${_ec_r}" |
6654d7a9 | 1067 | done |
a3295476 | 1068 | while [ "${#_ec_s}" -lt "132" ]; do |
f31debc0 | 1069 | _ec_s="0${_ec_s}" |
a3295476 | 1070 | done |
1071 | fi | |
e2a5af1c | 1072 | _debug3 "_ec_r" "$_ec_r" |
998783eb | 1073 | _debug3 "_ec_s" "$_ec_s" |
1074 | printf "%s" "$_ec_r$_ec_s" | _h2b | _base64 | |
1075 | else | |
1076 | _err "Unknown key file format." | |
1077 | return 1 | |
1078 | fi | |
4c2a3841 | 1079 | |
4c3b3608 | 1080 | } |
1081 | ||
3281043e | 1082 | #keylength or isEcc flag (empty str => not ecc) |
43822d37 | 1083 | _isEccKey() { |
1084 | _length="$1" | |
1085 | ||
4c2a3841 | 1086 | if [ -z "$_length" ]; then |
43822d37 | 1087 | return 1 |
1088 | fi | |
1089 | ||
19c43451 | 1090 | [ "$_length" != "1024" ] && |
1091 | [ "$_length" != "2048" ] && | |
1092 | [ "$_length" != "3072" ] && | |
1093 | [ "$_length" != "4096" ] && | |
1094 | [ "$_length" != "8192" ] | |
43822d37 | 1095 | } |
1096 | ||
e22bcf7c | 1097 | # _createkey 2048|ec-256 file |
1098 | _createkey() { | |
1099 | length="$1" | |
1100 | f="$2" | |
c4236e58 | 1101 | _debug2 "_createkey for file:$f" |
43822d37 | 1102 | eccname="$length" |
4c2a3841 | 1103 | if _startswith "$length" "ec-"; then |
f9a6988e | 1104 | length=$(printf "%s" "$length" | cut -d '-' -f 2-100) |
e22bcf7c | 1105 | |
4c2a3841 | 1106 | if [ "$length" = "256" ]; then |
e22bcf7c | 1107 | eccname="prime256v1" |
1108 | fi | |
4c2a3841 | 1109 | if [ "$length" = "384" ]; then |
e22bcf7c | 1110 | eccname="secp384r1" |
1111 | fi | |
4c2a3841 | 1112 | if [ "$length" = "521" ]; then |
e22bcf7c | 1113 | eccname="secp521r1" |
1114 | fi | |
43822d37 | 1115 | |
e22bcf7c | 1116 | fi |
1117 | ||
4c2a3841 | 1118 | if [ -z "$length" ]; then |
1119 | length=2048 | |
43822d37 | 1120 | fi |
4c2a3841 | 1121 | |
cbcd7e0f | 1122 | _debug "Use length $length" |
43822d37 | 1123 | |
81532f37 | 1124 | if ! touch "$f" >/dev/null 2>&1; then |
1125 | _f_path="$(dirname "$f")" | |
1126 | _debug _f_path "$_f_path" | |
1127 | if ! mkdir -p "$_f_path"; then | |
1128 | _err "Can not create path: $_f_path" | |
1129 | return 1 | |
1130 | fi | |
1131 | fi | |
1132 | ||
4c2a3841 | 1133 | if _isEccKey "$length"; then |
cbcd7e0f | 1134 | _debug "Using ec name: $eccname" |
c384ed96 | 1135 | if _opkey="$(${ACME_OPENSSL_BIN:-openssl} ecparam -name "$eccname" -noout -genkey 2>/dev/null)"; then |
e6df1828 | 1136 | echo "$_opkey" >"$f" |
1137 | else | |
1138 | _err "error ecc key name: $eccname" | |
1139 | return 1 | |
1140 | fi | |
e22bcf7c | 1141 | else |
cbcd7e0f | 1142 | _debug "Using RSA: $length" |
c384ed96 AK |
1143 | __traditional="" |
1144 | if _contains "$(${ACME_OPENSSL_BIN:-openssl} help genrsa 2>&1)" "-traditional"; then | |
1145 | __traditional="-traditional" | |
1146 | fi | |
1147 | if _opkey="$(${ACME_OPENSSL_BIN:-openssl} genrsa $__traditional "$length" 2>/dev/null)"; then | |
e6df1828 | 1148 | echo "$_opkey" >"$f" |
1149 | else | |
1150 | _err "error rsa key: $length" | |
1151 | return 1 | |
1152 | fi | |
e22bcf7c | 1153 | fi |
43822d37 | 1154 | |
4c2a3841 | 1155 | if [ "$?" != "0" ]; then |
43822d37 | 1156 | _err "Create key error." |
1157 | return 1 | |
1158 | fi | |
e22bcf7c | 1159 | } |
1160 | ||
9774b01b | 1161 | #domain |
1162 | _is_idn() { | |
1163 | _is_idn_d="$1" | |
049be104 | 1164 | _debug2 _is_idn_d "$_is_idn_d" |
d10f40f1 | 1165 | _idn_temp=$(printf "%s" "$_is_idn_d" | tr -d '0-9' | tr -d 'a-z' | tr -d 'A-Z' | tr -d '*.,-_') |
049be104 | 1166 | _debug2 _idn_temp "$_idn_temp" |
1167 | [ "$_idn_temp" ] | |
9774b01b | 1168 | } |
1169 | ||
1170 | #aa.com | |
1171 | #aa.com,bb.com,cc.com | |
1172 | _idn() { | |
1173 | __idn_d="$1" | |
4c2a3841 | 1174 | if ! _is_idn "$__idn_d"; then |
9774b01b | 1175 | printf "%s" "$__idn_d" |
1176 | return 0 | |
1177 | fi | |
4c2a3841 | 1178 | |
1179 | if _exists idn; then | |
1180 | if _contains "$__idn_d" ','; then | |
9774b01b | 1181 | _i_first="1" |
4c2a3841 | 1182 | for f in $(echo "$__idn_d" | tr ',' ' '); do |
9774b01b | 1183 | [ -z "$f" ] && continue |
4c2a3841 | 1184 | if [ -z "$_i_first" ]; then |
9774b01b | 1185 | printf "%s" "," |
1186 | else | |
1187 | _i_first="" | |
1188 | fi | |
2a1e06f8 | 1189 | idn --quiet "$f" | tr -d "\r\n" |
9774b01b | 1190 | done |
1191 | else | |
1192 | idn "$__idn_d" | tr -d "\r\n" | |
1193 | fi | |
1194 | else | |
1195 | _err "Please install idn to process IDN names." | |
1196 | fi | |
1197 | } | |
1198 | ||
08681f4a | 1199 | #_createcsr cn san_list keyfile csrfile conf acmeValidationv1 |
e22bcf7c | 1200 | _createcsr() { |
1201 | _debug _createcsr | |
1202 | domain="$1" | |
1203 | domainlist="$2" | |
0c9546cc | 1204 | csrkey="$3" |
e22bcf7c | 1205 | csr="$4" |
1206 | csrconf="$5" | |
08681f4a | 1207 | acmeValidationv1="$6" |
e22bcf7c | 1208 | _debug2 domain "$domain" |
1209 | _debug2 domainlist "$domainlist" | |
0c9546cc | 1210 | _debug2 csrkey "$csrkey" |
1211 | _debug2 csr "$csr" | |
1212 | _debug2 csrconf "$csrconf" | |
4c2a3841 | 1213 | |
1214 | printf "[ req_distinguished_name ]\n[ req ]\ndistinguished_name = req_distinguished_name\nreq_extensions = v3_req\n[ v3_req ]\n\nkeyUsage = nonRepudiation, digitalSignature, keyEncipherment" >"$csrconf" | |
1215 | ||
08681f4a | 1216 | if [ "$acmeValidationv1" ]; then |
4962cc3d | 1217 | domainlist="$(_idn "$domainlist")" |
08681f4a | 1218 | printf -- "\nsubjectAltName=DNS:$domainlist" >>"$csrconf" |
1219 | elif [ -z "$domainlist" ] || [ "$domainlist" = "$NO_VALUE" ]; then | |
e22bcf7c | 1220 | #single domain |
1221 | _info "Single domain" "$domain" | |
0093dc3d | 1222 | printf -- "\nsubjectAltName=DNS:$(_idn "$domain")" >>"$csrconf" |
e22bcf7c | 1223 | else |
f9a6988e | 1224 | domainlist="$(_idn "$domainlist")" |
9774b01b | 1225 | _debug2 domainlist "$domainlist" |
4c2a3841 | 1226 | if _contains "$domainlist" ","; then |
0093dc3d | 1227 | alt="DNS:$(_idn "$domain"),DNS:$(echo "$domainlist" | sed "s/,,/,/g" | sed "s/,/,DNS:/g")" |
e22bcf7c | 1228 | else |
0093dc3d | 1229 | alt="DNS:$(_idn "$domain"),DNS:$domainlist" |
e22bcf7c | 1230 | fi |
3c07f57a | 1231 | #multi |
e22bcf7c | 1232 | _info "Multi domain" "$alt" |
4c2a3841 | 1233 | printf -- "\nsubjectAltName=$alt" >>"$csrconf" |
0c9546cc | 1234 | fi |
6ba1eda9 | 1235 | if [ "$Le_OCSP_Staple" = "1" ]; then |
96db9362 | 1236 | _savedomainconf Le_OCSP_Staple "$Le_OCSP_Staple" |
4c2a3841 | 1237 | printf -- "\nbasicConstraints = CA:FALSE\n1.3.6.1.5.5.7.1.24=DER:30:03:02:01:05" >>"$csrconf" |
e22bcf7c | 1238 | fi |
4c2a3841 | 1239 | |
08681f4a | 1240 | if [ "$acmeValidationv1" ]; then |
ad613e24 | 1241 | printf "\n1.3.6.1.5.5.7.1.31=critical,DER:04:20:${acmeValidationv1}" >>"${csrconf}" |
08681f4a | 1242 | fi |
1243 | ||
9774b01b | 1244 | _csr_cn="$(_idn "$domain")" |
1245 | _debug2 _csr_cn "$_csr_cn" | |
34f25fa5 | 1246 | if _contains "$(uname -a)" "MINGW"; then |
d8ba26e6 | 1247 | ${ACME_OPENSSL_BIN:-openssl} req -new -sha256 -key "$csrkey" -subj "//CN=$_csr_cn" -config "$csrconf" -out "$csr" |
34f25fa5 | 1248 | else |
d8ba26e6 | 1249 | ${ACME_OPENSSL_BIN:-openssl} req -new -sha256 -key "$csrkey" -subj "/CN=$_csr_cn" -config "$csrconf" -out "$csr" |
34f25fa5 | 1250 | fi |
e22bcf7c | 1251 | } |
1252 | ||
1253 | #_signcsr key csr conf cert | |
1254 | _signcsr() { | |
1255 | key="$1" | |
1256 | csr="$2" | |
1257 | conf="$3" | |
1258 | cert="$4" | |
5aa146a5 | 1259 | _debug "_signcsr" |
4c2a3841 | 1260 | |
d8ba26e6 | 1261 | _msg="$(${ACME_OPENSSL_BIN:-openssl} x509 -req -days 365 -in "$csr" -signkey "$key" -extensions v3_req -extfile "$conf" -out "$cert" 2>&1)" |
5aa146a5 | 1262 | _ret="$?" |
1263 | _debug "$_msg" | |
1264 | return $_ret | |
e22bcf7c | 1265 | } |
1266 | ||
10afcaca | 1267 | #_csrfile |
1268 | _readSubjectFromCSR() { | |
1269 | _csrfile="$1" | |
4c2a3841 | 1270 | if [ -z "$_csrfile" ]; then |
10afcaca | 1271 | _usage "_readSubjectFromCSR mycsr.csr" |
1272 | return 1 | |
1273 | fi | |
b963dadc | 1274 | ${ACME_OPENSSL_BIN:-openssl} req -noout -in "$_csrfile" -subject | tr ',' "\n" | _egrep_o "CN *=.*" | cut -d = -f 2 | cut -d / -f 1 | tr -d ' \n' |
10afcaca | 1275 | } |
1276 | ||
1277 | #_csrfile | |
1278 | #echo comma separated domain list | |
1279 | _readSubjectAltNamesFromCSR() { | |
1280 | _csrfile="$1" | |
4c2a3841 | 1281 | if [ -z "$_csrfile" ]; then |
10afcaca | 1282 | _usage "_readSubjectAltNamesFromCSR mycsr.csr" |
1283 | return 1 | |
1284 | fi | |
4c2a3841 | 1285 | |
10afcaca | 1286 | _csrsubj="$(_readSubjectFromCSR "$_csrfile")" |
1287 | _debug _csrsubj "$_csrsubj" | |
4c2a3841 | 1288 | |
d8ba26e6 | 1289 | _dnsAltnames="$(${ACME_OPENSSL_BIN:-openssl} req -noout -text -in "$_csrfile" | grep "^ *DNS:.*" | tr -d ' \n')" |
10afcaca | 1290 | _debug _dnsAltnames "$_dnsAltnames" |
4c2a3841 | 1291 | |
1292 | if _contains "$_dnsAltnames," "DNS:$_csrsubj,"; then | |
10afcaca | 1293 | _debug "AltNames contains subject" |
2b9ebd66 | 1294 | _excapedAlgnames="$(echo "$_dnsAltnames" | tr '*' '#')" |
1295 | _debug _excapedAlgnames "$_excapedAlgnames" | |
1296 | _escapedSubject="$(echo "$_csrsubj" | tr '*' '#')" | |
1297 | _debug _escapedSubject "$_escapedSubject" | |
1298 | _dnsAltnames="$(echo "$_excapedAlgnames," | sed "s/DNS:$_escapedSubject,//g" | tr '#' '*' | sed "s/,\$//g")" | |
1299 | _debug _dnsAltnames "$_dnsAltnames" | |
10afcaca | 1300 | else |
1301 | _debug "AltNames doesn't contain subject" | |
1302 | fi | |
4c2a3841 | 1303 | |
2b9ebd66 | 1304 | echo "$_dnsAltnames" | sed "s/DNS://g" |
10afcaca | 1305 | } |
1306 | ||
3c07f57a | 1307 | #_csrfile |
10afcaca | 1308 | _readKeyLengthFromCSR() { |
1309 | _csrfile="$1" | |
4c2a3841 | 1310 | if [ -z "$_csrfile" ]; then |
1643b476 | 1311 | _usage "_readKeyLengthFromCSR mycsr.csr" |
10afcaca | 1312 | return 1 |
1313 | fi | |
4c2a3841 | 1314 | |
d8ba26e6 | 1315 | _outcsr="$(${ACME_OPENSSL_BIN:-openssl} req -noout -text -in "$_csrfile")" |
7df062b7 | 1316 | _debug2 _outcsr "$_outcsr" |
4c2a3841 | 1317 | if _contains "$_outcsr" "Public Key Algorithm: id-ecPublicKey"; then |
10afcaca | 1318 | _debug "ECC CSR" |
482cb737 | 1319 | echo "$_outcsr" | tr "\t" " " | _egrep_o "^ *ASN1 OID:.*" | cut -d ':' -f 2 | tr -d ' ' |
10afcaca | 1320 | else |
1321 | _debug "RSA CSR" | |
eb0ef6bd | 1322 | _rkl="$(echo "$_outcsr" | tr "\t" " " | _egrep_o "^ *Public.Key:.*" | cut -d '(' -f 2 | cut -d ' ' -f 1)" |
1323 | if [ "$_rkl" ]; then | |
1324 | echo "$_rkl" | |
1325 | else | |
1326 | echo "$_outcsr" | tr "\t" " " | _egrep_o "RSA Public.Key:.*" | cut -d '(' -f 2 | cut -d ' ' -f 1 | |
1327 | fi | |
10afcaca | 1328 | fi |
1329 | } | |
1330 | ||
34c27e09 | 1331 | _ss() { |
1332 | _port="$1" | |
4c2a3841 | 1333 | |
1334 | if _exists "ss"; then | |
edf08da6 | 1335 | _debug "Using: ss" |
14d7bfda | 1336 | ss -ntpl 2>/dev/null | grep ":$_port " |
edf08da6 | 1337 | return 0 |
1338 | fi | |
1339 | ||
4c2a3841 | 1340 | if _exists "netstat"; then |
251fc37c | 1341 | _debug "Using: netstat" |
97147b59 | 1342 | if netstat -help 2>&1 | grep "\-p proto" >/dev/null; then |
ccb96535 | 1343 | #for windows version netstat tool |
0463b5d6 | 1344 | netstat -an -p tcp | grep "LISTENING" | grep ":$_port " |
ccb96535 | 1345 | else |
4c2a3841 | 1346 | if netstat -help 2>&1 | grep "\-p protocol" >/dev/null; then |
19539575 | 1347 | netstat -an -p tcp | grep LISTEN | grep ":$_port " |
4c2a3841 | 1348 | elif netstat -help 2>&1 | grep -- '-P protocol' >/dev/null; then |
22ea4004 | 1349 | #for solaris |
e3c66532 | 1350 | netstat -an -P tcp | grep "\.$_port " | grep "LISTEN" |
f19f2100 | 1351 | elif netstat -help 2>&1 | grep "\-p" >/dev/null; then |
f21dd911 | 1352 | #for full linux |
19539575 | 1353 | netstat -ntpl | grep ":$_port " |
f21dd911 | 1354 | else |
1355 | #for busybox (embedded linux; no pid support) | |
1356 | netstat -ntl 2>/dev/null | grep ":$_port " | |
edf08da6 | 1357 | fi |
ccb96535 | 1358 | fi |
34c27e09 | 1359 | return 0 |
1360 | fi | |
edf08da6 | 1361 | |
34c27e09 | 1362 | return 1 |
1363 | } | |
1364 | ||
8eab77f3 BC |
1365 | #outfile key cert cacert [password [name [caname]]] |
1366 | _toPkcs() { | |
1367 | _cpfx="$1" | |
1368 | _ckey="$2" | |
1369 | _ccert="$3" | |
1370 | _cca="$4" | |
1371 | pfxPassword="$5" | |
1372 | pfxName="$6" | |
1373 | pfxCaname="$7" | |
1374 | ||
1375 | if [ "$pfxCaname" ]; then | |
1376 | ${ACME_OPENSSL_BIN:-openssl} pkcs12 -export -out "$_cpfx" -inkey "$_ckey" -in "$_ccert" -certfile "$_cca" -password "pass:$pfxPassword" -name "$pfxName" -caname "$pfxCaname" | |
1377 | elif [ "$pfxName" ]; then | |
1378 | ${ACME_OPENSSL_BIN:-openssl} pkcs12 -export -out "$_cpfx" -inkey "$_ckey" -in "$_ccert" -certfile "$_cca" -password "pass:$pfxPassword" -name "$pfxName" | |
1379 | elif [ "$pfxPassword" ]; then | |
1380 | ${ACME_OPENSSL_BIN:-openssl} pkcs12 -export -out "$_cpfx" -inkey "$_ckey" -in "$_ccert" -certfile "$_cca" -password "pass:$pfxPassword" | |
1381 | else | |
1382 | ${ACME_OPENSSL_BIN:-openssl} pkcs12 -export -out "$_cpfx" -inkey "$_ckey" -in "$_ccert" -certfile "$_cca" | |
1383 | fi | |
1384 | ||
1385 | } | |
1386 | ||
43822d37 | 1387 | #domain [password] [isEcc] |
ac2d5123 | 1388 | toPkcs() { |
1389 | domain="$1" | |
1390 | pfxPassword="$2" | |
4c2a3841 | 1391 | if [ -z "$domain" ]; then |
2e87e64b | 1392 | _usage "Usage: $PROJECT_ENTRY --to-pkcs12 --domain <domain.tld> [--password <password>] [--ecc]" |
ac2d5123 | 1393 | return 1 |
1394 | fi | |
1395 | ||
43822d37 | 1396 | _isEcc="$3" |
4c2a3841 | 1397 | |
43822d37 | 1398 | _initpath "$domain" "$_isEcc" |
1399 | ||
8eab77f3 | 1400 | _toPkcs "$CERT_PFX_PATH" "$CERT_KEY_PATH" "$CERT_PATH" "$CA_CERT_PATH" "$pfxPassword" |
4c2a3841 | 1401 | |
1402 | if [ "$?" = "0" ]; then | |
ac2d5123 | 1403 | _info "Success, Pfx is exported to: $CERT_PFX_PATH" |
1404 | fi | |
1405 | ||
1406 | } | |
1407 | ||
4410226d | 1408 | #domain [isEcc] |
1409 | toPkcs8() { | |
1410 | domain="$1" | |
1411 | ||
1412 | if [ -z "$domain" ]; then | |
2e87e64b | 1413 | _usage "Usage: $PROJECT_ENTRY --to-pkcs8 --domain <domain.tld> [--ecc]" |
4410226d | 1414 | return 1 |
1415 | fi | |
1416 | ||
1417 | _isEcc="$2" | |
1418 | ||
1419 | _initpath "$domain" "$_isEcc" | |
1420 | ||
d8ba26e6 | 1421 | ${ACME_OPENSSL_BIN:-openssl} pkcs8 -topk8 -inform PEM -outform PEM -nocrypt -in "$CERT_KEY_PATH" -out "$CERT_PKCS8_PATH" |
4410226d | 1422 | |
1423 | if [ "$?" = "0" ]; then | |
1424 | _info "Success, $CERT_PKCS8_PATH" | |
1425 | fi | |
1426 | ||
1427 | } | |
1428 | ||
3c07f57a | 1429 | #[2048] |
4c3b3608 | 1430 | createAccountKey() { |
1431 | _info "Creating account key" | |
4c2a3841 | 1432 | if [ -z "$1" ]; then |
2e87e64b | 1433 | _usage "Usage: $PROJECT_ENTRY --create-account-key [--accountkeylength <bits>]" |
4c3b3608 | 1434 | return |
1435 | fi | |
4c2a3841 | 1436 | |
5fbc47eb | 1437 | length=$1 |
57e58ce7 | 1438 | _create_account_key "$length" |
1439 | ||
1440 | } | |
1441 | ||
1442 | _create_account_key() { | |
1443 | ||
5fbc47eb | 1444 | length=$1 |
4c2a3841 | 1445 | |
1446 | if [ -z "$length" ] || [ "$length" = "$NO_VALUE" ]; then | |
57e58ce7 | 1447 | _debug "Use default length $DEFAULT_ACCOUNT_KEY_LENGTH" |
1448 | length="$DEFAULT_ACCOUNT_KEY_LENGTH" | |
4c3b3608 | 1449 | fi |
4c2a3841 | 1450 | |
5fbc47eb | 1451 | _debug length "$length" |
4c3b3608 | 1452 | _initpath |
5fbc47eb | 1453 | |
57e58ce7 | 1454 | mkdir -p "$CA_DIR" |
4f1888d2 | 1455 | if [ -s "$ACCOUNT_KEY_PATH" ]; then |
4c3b3608 | 1456 | _info "Account key exists, skip" |
4f1888d2 | 1457 | return 0 |
4c3b3608 | 1458 | else |
1459 | #generate account key | |
4f1888d2 | 1460 | if _createkey "$length" "$ACCOUNT_KEY_PATH"; then |
1461 | chmod 600 "$ACCOUNT_KEY_PATH" | |
1462 | _info "Create account key ok." | |
1463 | return 0 | |
1464 | else | |
1465 | _err "Create account key error." | |
1466 | return 1 | |
1467 | fi | |
4c3b3608 | 1468 | fi |
1469 | ||
1470 | } | |
1471 | ||
43822d37 | 1472 | #domain [length] |
4c3b3608 | 1473 | createDomainKey() { |
1474 | _info "Creating domain key" | |
4c2a3841 | 1475 | if [ -z "$1" ]; then |
2e87e64b | 1476 | _usage "Usage: $PROJECT_ENTRY --create-domain-key --domain <domain.tld> [--keylength <bits>]" |
4c3b3608 | 1477 | return |
1478 | fi | |
4c2a3841 | 1479 | |
4c3b3608 | 1480 | domain=$1 |
2844d73d | 1481 | _cdl=$2 |
e22bcf7c | 1482 | |
2844d73d | 1483 | if [ -z "$_cdl" ]; then |
57e58ce7 | 1484 | _debug "Use DEFAULT_DOMAIN_KEY_LENGTH=$DEFAULT_DOMAIN_KEY_LENGTH" |
2844d73d | 1485 | _cdl="$DEFAULT_DOMAIN_KEY_LENGTH" |
57e58ce7 | 1486 | fi |
e22bcf7c | 1487 | |
2844d73d | 1488 | _initpath "$domain" "$_cdl" |
4c2a3841 | 1489 | |
bd04638d | 1490 | if [ ! -f "$CERT_KEY_PATH" ] || [ ! -s "$CERT_KEY_PATH" ] || ([ "$FORCE" ] && ! [ "$_ACME_IS_RENEW" ]) || [ "$Le_ForceNewDomainKey" = "1" ]; then |
2844d73d | 1491 | if _createkey "$_cdl" "$CERT_KEY_PATH"; then |
1492 | _savedomainconf Le_Keylength "$_cdl" | |
1493 | _info "The domain key is here: $(__green $CERT_KEY_PATH)" | |
7aeb113c | 1494 | return 0 |
4f1888d2 | 1495 | else |
a7420ca3 | 1496 | _err "Can not create domain key" |
4f1888d2 | 1497 | return 1 |
2844d73d | 1498 | fi |
4c3b3608 | 1499 | else |
bd04638d | 1500 | if [ "$_ACME_IS_RENEW" ]; then |
4c3b3608 | 1501 | _info "Domain key exists, skip" |
1502 | return 0 | |
1503 | else | |
1504 | _err "Domain key exists, do you want to overwrite the key?" | |
41e3eafa | 1505 | _err "Add '--force', and try again." |
4c3b3608 | 1506 | return 1 |
1507 | fi | |
1508 | fi | |
1509 | ||
1510 | } | |
1511 | ||
43822d37 | 1512 | # domain domainlist isEcc |
4c3b3608 | 1513 | createCSR() { |
1514 | _info "Creating csr" | |
4c2a3841 | 1515 | if [ -z "$1" ]; then |
2e87e64b | 1516 | _usage "Usage: $PROJECT_ENTRY --create-csr --domain <domain.tld> [--domain <domain2.tld> ...]" |
4c3b3608 | 1517 | return |
1518 | fi | |
4c2a3841 | 1519 | |
43822d37 | 1520 | domain="$1" |
1521 | domainlist="$2" | |
1522 | _isEcc="$3" | |
4c2a3841 | 1523 | |
43822d37 | 1524 | _initpath "$domain" "$_isEcc" |
4c2a3841 | 1525 | |
bd04638d | 1526 | if [ -f "$CSR_PATH" ] && [ "$_ACME_IS_RENEW" ] && [ -z "$FORCE" ]; then |
4c3b3608 | 1527 | _info "CSR exists, skip" |
1528 | return | |
1529 | fi | |
4c2a3841 | 1530 | |
1531 | if [ ! -f "$CERT_KEY_PATH" ]; then | |
43822d37 | 1532 | _err "The key file is not found: $CERT_KEY_PATH" |
1533 | _err "Please create the key file first." | |
1534 | return 1 | |
1535 | fi | |
e22bcf7c | 1536 | _createcsr "$domain" "$domainlist" "$CERT_KEY_PATH" "$CSR_PATH" "$DOMAIN_SSL_CONF" |
4c2a3841 | 1537 | |
4c3b3608 | 1538 | } |
1539 | ||
11927a76 | 1540 | _url_replace() { |
f9a6988e | 1541 | tr '/+' '_-' | tr -d '= ' |
4c3b3608 | 1542 | } |
1543 | ||
f96d91cb | 1544 | #base64 string |
1545 | _durl_replace_base64() { | |
1546 | _l=$((${#1} % 4)) | |
1547 | if [ $_l -eq 2 ]; then | |
1548 | _s="$1"'==' | |
1549 | elif [ $_l -eq 3 ]; then | |
1550 | _s="$1"'=' | |
1551 | else | |
1552 | _s="$1" | |
1553 | fi | |
1554 | echo "$_s" | tr '_-' '/+' | |
1555 | } | |
1556 | ||
4c3b3608 | 1557 | _time2str() { |
cb115809 JM |
1558 | #BSD |
1559 | if date -u -r "$1" 2>/dev/null; then | |
4c3b3608 | 1560 | return |
1561 | fi | |
4c2a3841 | 1562 | |
cb115809 JM |
1563 | #Linux |
1564 | if date -u -d@"$1" 2>/dev/null; then | |
4c3b3608 | 1565 | return |
1566 | fi | |
4c2a3841 | 1567 | |
cb115809 | 1568 | #Solaris |
4c2a3841 | 1569 | if _exists adb; then |
031e885e | 1570 | _t_s_a=$(echo "0t${1}=Y" | adb) |
1571 | echo "$_t_s_a" | |
22ea4004 | 1572 | fi |
4c2a3841 | 1573 | |
a07395fb M |
1574 | #Busybox |
1575 | if echo "$1" | awk '{ print strftime("%c", $0); }' 2>/dev/null; then | |
1576 | return | |
1577 | fi | |
4c3b3608 | 1578 | } |
1579 | ||
eae29099 | 1580 | _normalizeJson() { |
1581 | sed "s/\" *: *\([\"{\[]\)/\":\1/g" | sed "s/^ *\([^ ]\)/\1/" | tr -d "\r\n" | |
1582 | } | |
1583 | ||
44df2967 | 1584 | _stat() { |
1585 | #Linux | |
4c2a3841 | 1586 | if stat -c '%U:%G' "$1" 2>/dev/null; then |
44df2967 | 1587 | return |
1588 | fi | |
4c2a3841 | 1589 | |
44df2967 | 1590 | #BSD |
4c2a3841 | 1591 | if stat -f '%Su:%Sg' "$1" 2>/dev/null; then |
44df2967 | 1592 | return |
1593 | fi | |
4c2a3841 | 1594 | |
1595 | return 1 #error, 'stat' not found | |
44df2967 | 1596 | } |
1597 | ||
166096dc | 1598 | #keyfile |
1599 | _calcjwk() { | |
1600 | keyfile="$1" | |
4c2a3841 | 1601 | if [ -z "$keyfile" ]; then |
43822d37 | 1602 | _usage "Usage: _calcjwk keyfile" |
166096dc | 1603 | return 1 |
1604 | fi | |
4c2a3841 | 1605 | |
1606 | if [ "$JWK_HEADER" ] && [ "$__CACHED_JWK_KEY_FILE" = "$keyfile" ]; then | |
ae2db62f | 1607 | _debug2 "Use cached jwk for file: $__CACHED_JWK_KEY_FILE" |
1608 | return 0 | |
1609 | fi | |
4c2a3841 | 1610 | |
4c2a3841 | 1611 | if grep "BEGIN RSA PRIVATE KEY" "$keyfile" >/dev/null 2>&1; then |
166096dc | 1612 | _debug "RSA key" |
d8ba26e6 | 1613 | 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) |
4c2a3841 | 1614 | if [ "${#pub_exp}" = "5" ]; then |
166096dc | 1615 | pub_exp=0$pub_exp |
1616 | fi | |
22ea4004 | 1617 | _debug3 pub_exp "$pub_exp" |
4c2a3841 | 1618 | |
f9a6988e | 1619 | e=$(echo "$pub_exp" | _h2b | _base64) |
22ea4004 | 1620 | _debug3 e "$e" |
4c2a3841 | 1621 | |
d8ba26e6 | 1622 | modulus=$(${ACME_OPENSSL_BIN:-openssl} rsa -in "$keyfile" -modulus -noout | cut -d '=' -f 2) |
22ea4004 | 1623 | _debug3 modulus "$modulus" |
11927a76 | 1624 | n="$(printf "%s" "$modulus" | _h2b | _base64 | _url_replace)" |
4d8b99a3 | 1625 | _debug3 n "$n" |
1626 | ||
166096dc | 1627 | jwk='{"e": "'$e'", "kty": "RSA", "n": "'$n'"}' |
22ea4004 | 1628 | _debug3 jwk "$jwk" |
4c2a3841 | 1629 | |
5982f4bc | 1630 | JWK_HEADER='{"alg": "RS256", "jwk": '$jwk'}' |
1631 | JWK_HEADERPLACE_PART1='{"nonce": "' | |
c1151b0d | 1632 | JWK_HEADERPLACE_PART2='", "alg": "RS256"' |
4c2a3841 | 1633 | elif grep "BEGIN EC PRIVATE KEY" "$keyfile" >/dev/null 2>&1; then |
166096dc | 1634 | _debug "EC key" |
d8ba26e6 | 1635 | crv="$(${ACME_OPENSSL_BIN:-openssl} ec -in "$keyfile" -noout -text 2>/dev/null | grep "^NIST CURVE:" | cut -d ":" -f 2 | tr -d " \r\n")" |
22ea4004 | 1636 | _debug3 crv "$crv" |
f87890cb | 1637 | __ECC_KEY_LEN=$(echo "$crv" | cut -d "-" -f 2) |
1638 | if [ "$__ECC_KEY_LEN" = "521" ]; then | |
1639 | __ECC_KEY_LEN=512 | |
1640 | fi | |
1641 | _debug3 __ECC_KEY_LEN "$__ECC_KEY_LEN" | |
4c2a3841 | 1642 | if [ -z "$crv" ]; then |
d22b7938 | 1643 | _debug "Let's try ASN1 OID" |
d8ba26e6 | 1644 | 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")" |
cae9cee2 | 1645 | _debug3 crv_oid "$crv_oid" |
d22b7938 | 1646 | case "${crv_oid}" in |
19c43451 | 1647 | "prime256v1") |
1648 | crv="P-256" | |
1649 | __ECC_KEY_LEN=256 | |
1650 | ;; | |
1651 | "secp384r1") | |
1652 | crv="P-384" | |
1653 | __ECC_KEY_LEN=384 | |
1654 | ;; | |
1655 | "secp521r1") | |
1656 | crv="P-521" | |
1657 | __ECC_KEY_LEN=512 | |
1658 | ;; | |
1659 | *) | |
1660 | _err "ECC oid : $crv_oid" | |
1661 | return 1 | |
1662 | ;; | |
067d586c | 1663 | esac |
d22b7938 | 1664 | _debug3 crv "$crv" |
1665 | fi | |
4c2a3841 | 1666 | |
d8ba26e6 | 1667 | pubi="$(${ACME_OPENSSL_BIN:-openssl} ec -in "$keyfile" -noout -text 2>/dev/null | grep -n pub: | cut -d : -f 1)" |
79a267ab | 1668 | pubi=$(_math "$pubi" + 1) |
22ea4004 | 1669 | _debug3 pubi "$pubi" |
4c2a3841 | 1670 | |
d8ba26e6 | 1671 | pubj="$(${ACME_OPENSSL_BIN:-openssl} ec -in "$keyfile" -noout -text 2>/dev/null | grep -n "ASN1 OID:" | cut -d : -f 1)" |
79a267ab | 1672 | pubj=$(_math "$pubj" - 1) |
22ea4004 | 1673 | _debug3 pubj "$pubj" |
4c2a3841 | 1674 | |
d8ba26e6 | 1675 | pubtext="$(${ACME_OPENSSL_BIN:-openssl} ec -in "$keyfile" -noout -text 2>/dev/null | sed -n "$pubi,${pubj}p" | tr -d " \n\r")" |
22ea4004 | 1676 | _debug3 pubtext "$pubtext" |
4c2a3841 | 1677 | |
95e06de5 | 1678 | xlen="$(printf "%s" "$pubtext" | tr -d ':' | wc -c)" |
79a267ab | 1679 | xlen=$(_math "$xlen" / 4) |
22ea4004 | 1680 | _debug3 xlen "$xlen" |
00a50605 | 1681 | |
998783eb | 1682 | xend=$(_math "$xlen" + 1) |
f9a6988e | 1683 | x="$(printf "%s" "$pubtext" | cut -d : -f 2-"$xend")" |
22ea4004 | 1684 | _debug3 x "$x" |
4c2a3841 | 1685 | |
11927a76 | 1686 | x64="$(printf "%s" "$x" | tr -d : | _h2b | _base64 | _url_replace)" |
22ea4004 | 1687 | _debug3 x64 "$x64" |
00a50605 | 1688 | |
19539575 | 1689 | xend=$(_math "$xend" + 1) |
f9a6988e | 1690 | y="$(printf "%s" "$pubtext" | cut -d : -f "$xend"-10000)" |
22ea4004 | 1691 | _debug3 y "$y" |
4c2a3841 | 1692 | |
11927a76 | 1693 | y64="$(printf "%s" "$y" | tr -d : | _h2b | _base64 | _url_replace)" |
22ea4004 | 1694 | _debug3 y64 "$y64" |
4c2a3841 | 1695 | |
ae2db62f | 1696 | jwk='{"crv": "'$crv'", "kty": "EC", "x": "'$x64'", "y": "'$y64'"}' |
22ea4004 | 1697 | _debug3 jwk "$jwk" |
4c2a3841 | 1698 | |
f87890cb | 1699 | JWK_HEADER='{"alg": "ES'$__ECC_KEY_LEN'", "jwk": '$jwk'}' |
5982f4bc | 1700 | JWK_HEADERPLACE_PART1='{"nonce": "' |
c1151b0d | 1701 | JWK_HEADERPLACE_PART2='", "alg": "ES'$__ECC_KEY_LEN'"' |
166096dc | 1702 | else |
238990a2 | 1703 | _err "Only RSA or EC key is supported. keyfile=$keyfile" |
1704 | _debug2 "$(cat "$keyfile")" | |
166096dc | 1705 | return 1 |
1706 | fi | |
1707 | ||
5982f4bc | 1708 | _debug3 JWK_HEADER "$JWK_HEADER" |
ae2db62f | 1709 | __CACHED_JWK_KEY_FILE="$keyfile" |
166096dc | 1710 | } |
fac1e367 | 1711 | |
3aae1ae3 | 1712 | _time() { |
1713 | date -u "+%s" | |
1714 | } | |
fac1e367 | 1715 | |
5d2c5b01 | 1716 | _utc_date() { |
1717 | date -u "+%Y-%m-%d %H:%M:%S" | |
1718 | } | |
1719 | ||
fac1e367 | 1720 | _mktemp() { |
4c2a3841 | 1721 | if _exists mktemp; then |
1722 | if mktemp 2>/dev/null; then | |
610e0f21 | 1723 | return 0 |
4c2a3841 | 1724 | elif _contains "$(mktemp 2>&1)" "-t prefix" && mktemp -t "$PROJECT_NAME" 2>/dev/null; then |
5c48e139 | 1725 | #for Mac osx |
610e0f21 | 1726 | return 0 |
b19ba13a | 1727 | fi |
fac1e367 | 1728 | fi |
4c2a3841 | 1729 | if [ -d "/tmp" ]; then |
3aae1ae3 | 1730 | echo "/tmp/${PROJECT_NAME}wefADf24sf.$(_time).tmp" |
1731 | return 0 | |
4c2a3841 | 1732 | elif [ "$LE_TEMP_DIR" ] && mkdir -p "$LE_TEMP_DIR"; then |
610e0f21 | 1733 | echo "/$LE_TEMP_DIR/wefADf24sf.$(_time).tmp" |
1734 | return 0 | |
3aae1ae3 | 1735 | fi |
1736 | _err "Can not create temp file." | |
fac1e367 | 1737 | } |
1738 | ||
effa7fd5 | 1739 | #clear all the https envs to cause _inithttp() to run next time. |
1740 | _resethttp() { | |
1741 | __HTTP_INITIALIZED="" | |
1742 | _ACME_CURL="" | |
1743 | _ACME_WGET="" | |
1744 | ACME_HTTP_NO_REDIRECTS="" | |
1745 | } | |
1746 | ||
fac1e367 | 1747 | _inithttp() { |
1748 | ||
4c2a3841 | 1749 | if [ -z "$HTTP_HEADER" ] || ! touch "$HTTP_HEADER"; then |
fac1e367 | 1750 | HTTP_HEADER="$(_mktemp)" |
1751 | _debug2 HTTP_HEADER "$HTTP_HEADER" | |
1752 | fi | |
1753 | ||
4c2a3841 | 1754 | if [ "$__HTTP_INITIALIZED" ]; then |
1755 | if [ "$_ACME_CURL$_ACME_WGET" ]; then | |
1befee5a | 1756 | _debug2 "Http already initialized." |
1757 | return 0 | |
1758 | fi | |
1759 | fi | |
4c2a3841 | 1760 | |
1761 | if [ -z "$_ACME_CURL" ] && _exists "curl"; then | |
effa7fd5 | 1762 | _ACME_CURL="curl --silent --dump-header $HTTP_HEADER " |
1763 | if [ -z "$ACME_HTTP_NO_REDIRECTS" ]; then | |
1764 | _ACME_CURL="$_ACME_CURL -L " | |
1765 | fi | |
4c2a3841 | 1766 | if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then |
fac1e367 | 1767 | _CURL_DUMP="$(_mktemp)" |
1befee5a | 1768 | _ACME_CURL="$_ACME_CURL --trace-ascii $_CURL_DUMP " |
fac1e367 | 1769 | fi |
1770 | ||
2aa75f03 | 1771 | if [ "$CA_PATH" ]; then |
1772 | _ACME_CURL="$_ACME_CURL --capath $CA_PATH " | |
1773 | elif [ "$CA_BUNDLE" ]; then | |
1befee5a | 1774 | _ACME_CURL="$_ACME_CURL --cacert $CA_BUNDLE " |
78009539 PS |
1775 | fi |
1776 | ||
6ca5f3d8 | 1777 | if _contains "$(curl --help 2>&1)" "--globoff"; then |
1778 | _ACME_CURL="$_ACME_CURL -g " | |
1779 | fi | |
fac1e367 | 1780 | fi |
4c2a3841 | 1781 | |
1befee5a | 1782 | if [ -z "$_ACME_WGET" ] && _exists "wget"; then |
1783 | _ACME_WGET="wget -q" | |
effa7fd5 | 1784 | if [ "$ACME_HTTP_NO_REDIRECTS" ]; then |
1785 | _ACME_WGET="$_ACME_WGET --max-redirect 0 " | |
1786 | fi | |
4c2a3841 | 1787 | if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then |
1befee5a | 1788 | _ACME_WGET="$_ACME_WGET -d " |
fac1e367 | 1789 | fi |
2aa75f03 | 1790 | if [ "$CA_PATH" ]; then |
1791 | _ACME_WGET="$_ACME_WGET --ca-directory=$CA_PATH " | |
1792 | elif [ "$CA_BUNDLE" ]; then | |
1793 | _ACME_WGET="$_ACME_WGET --ca-certificate=$CA_BUNDLE " | |
78009539 | 1794 | fi |
fac1e367 | 1795 | fi |
1796 | ||
177b57e1 | 1797 | #from wget 1.14: do not skip body on 404 error |
58ef6d83 | 1798 | if [ "$_ACME_WGET" ] && _contains "$($_ACME_WGET --help 2>&1)" "--content-on-error"; then |
177b57e1 | 1799 | _ACME_WGET="$_ACME_WGET --content-on-error " |
1800 | fi | |
1801 | ||
1befee5a | 1802 | __HTTP_INITIALIZED=1 |
fac1e367 | 1803 | |
fac1e367 | 1804 | } |
fac1e367 | 1805 | |
9cecd525 | 1806 | # body url [needbase64] [POST|PUT|DELETE] [ContentType] |
c60883ef | 1807 | _post() { |
1808 | body="$1" | |
c1151b0d | 1809 | _post_url="$2" |
c60883ef | 1810 | needbase64="$3" |
a4270efa | 1811 | httpmethod="$4" |
45e386b2 | 1812 | _postContentType="$5" |
c60883ef | 1813 | |
4c2a3841 | 1814 | if [ -z "$httpmethod" ]; then |
a4270efa | 1815 | httpmethod="POST" |
1816 | fi | |
1817 | _debug $httpmethod | |
c1151b0d | 1818 | _debug "_post_url" "$_post_url" |
30de13b4 | 1819 | _debug2 "body" "$body" |
6a66ba8a | 1820 | _debug2 "_postContentType" "$_postContentType" |
4c2a3841 | 1821 | |
fac1e367 | 1822 | _inithttp |
4c2a3841 | 1823 | |
9b124070 | 1824 | if [ "$_ACME_CURL" ] && [ "${ACME_USE_WGET:-0}" = "0" ]; then |
1befee5a | 1825 | _CURL="$_ACME_CURL" |
7834c252 | 1826 | if [ "$HTTPS_INSECURE" ]; then |
1827 | _CURL="$_CURL --insecure " | |
1828 | fi | |
5723fd11 | 1829 | if [ "$httpmethod" = "HEAD" ]; then |
1830 | _CURL="$_CURL -I " | |
1831 | fi | |
ec9fc8cb | 1832 | _debug "_CURL" "$_CURL" |
4c2a3841 | 1833 | if [ "$needbase64" ]; then |
1ba4ab2b | 1834 | if [ "$body" ]; then |
1835 | if [ "$_postContentType" ]; then | |
1836 | response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "Content-Type: $_postContentType" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$_post_url" | _base64)" | |
1837 | else | |
1838 | response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$_post_url" | _base64)" | |
1839 | fi | |
39852662 | 1840 | else |
1ba4ab2b | 1841 | if [ "$_postContentType" ]; then |
1842 | response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "Content-Type: $_postContentType" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$_post_url" | _base64)" | |
1843 | else | |
1844 | response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$_post_url" | _base64)" | |
1845 | fi | |
39852662 | 1846 | fi |
c60883ef | 1847 | else |
1ba4ab2b | 1848 | if [ "$body" ]; then |
1849 | if [ "$_postContentType" ]; then | |
1850 | response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "Content-Type: $_postContentType" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$_post_url")" | |
1851 | else | |
1852 | response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$_post_url")" | |
1853 | fi | |
39852662 | 1854 | else |
1ba4ab2b | 1855 | if [ "$_postContentType" ]; then |
1856 | response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "Content-Type: $_postContentType" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$_post_url")" | |
1857 | else | |
1858 | response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$_post_url")" | |
1859 | fi | |
39852662 | 1860 | fi |
c60883ef | 1861 | fi |
16679b57 | 1862 | _ret="$?" |
4c2a3841 | 1863 | if [ "$_ret" != "0" ]; then |
87ab2d90 | 1864 | _err "Please refer to https://curl.haxx.se/libcurl/c/libcurl-errors.html for error code: $_ret" |
4c2a3841 | 1865 | if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then |
87ab2d90 | 1866 | _err "Here is the curl dump log:" |
1867 | _err "$(cat "$_CURL_DUMP")" | |
1868 | fi | |
687cfcc2 | 1869 | fi |
4c2a3841 | 1870 | elif [ "$_ACME_WGET" ]; then |
7834c252 | 1871 | _WGET="$_ACME_WGET" |
1872 | if [ "$HTTPS_INSECURE" ]; then | |
1873 | _WGET="$_WGET --no-check-certificate " | |
1874 | fi | |
51b4a9e3 | 1875 | if [ "$httpmethod" = "HEAD" ]; then |
1876 | _WGET="$_WGET --read-timeout=3.0 --tries=2 " | |
1877 | fi | |
7834c252 | 1878 | _debug "_WGET" "$_WGET" |
4c2a3841 | 1879 | if [ "$needbase64" ]; then |
1880 | if [ "$httpmethod" = "POST" ]; then | |
ef871775 | 1881 | if [ "$_postContentType" ]; then |
1882 | response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --header "Content-Type: $_postContentType" --post-data="$body" "$_post_url" 2>"$HTTP_HEADER" | _base64)" | |
1883 | else | |
1884 | response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --post-data="$body" "$_post_url" 2>"$HTTP_HEADER" | _base64)" | |
1885 | fi | |
8fb9a709 | 1886 | else |
ef871775 | 1887 | if [ "$_postContentType" ]; then |
1888 | response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --header "Content-Type: $_postContentType" --method $httpmethod --body-data="$body" "$_post_url" 2>"$HTTP_HEADER" | _base64)" | |
1889 | else | |
1890 | response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --method $httpmethod --body-data="$body" "$_post_url" 2>"$HTTP_HEADER" | _base64)" | |
1891 | fi | |
8fb9a709 | 1892 | fi |
c60883ef | 1893 | else |
4c2a3841 | 1894 | if [ "$httpmethod" = "POST" ]; then |
ef871775 | 1895 | if [ "$_postContentType" ]; then |
1896 | response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --header "Content-Type: $_postContentType" --post-data="$body" "$_post_url" 2>"$HTTP_HEADER")" | |
1897 | else | |
1898 | response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --post-data="$body" "$_post_url" 2>"$HTTP_HEADER")" | |
1899 | fi | |
51b4a9e3 | 1900 | elif [ "$httpmethod" = "HEAD" ]; then |
1901 | if [ "$_postContentType" ]; then | |
1902 | response="$($_WGET --spider -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --header "Content-Type: $_postContentType" --post-data="$body" "$_post_url" 2>"$HTTP_HEADER")" | |
1903 | else | |
1904 | response="$($_WGET --spider -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --post-data="$body" "$_post_url" 2>"$HTTP_HEADER")" | |
1905 | fi | |
8fb9a709 | 1906 | else |
ef871775 | 1907 | if [ "$_postContentType" ]; then |
1908 | response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --header "Content-Type: $_postContentType" --method $httpmethod --body-data="$body" "$_post_url" 2>"$HTTP_HEADER")" | |
1909 | else | |
1910 | response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --method $httpmethod --body-data="$body" "$_post_url" 2>"$HTTP_HEADER")" | |
1911 | fi | |
8fb9a709 | 1912 | fi |
c60883ef | 1913 | fi |
16679b57 | 1914 | _ret="$?" |
4c2a3841 | 1915 | if [ "$_ret" = "8" ]; then |
9f43c270 | 1916 | _ret=0 |
810c129c | 1917 | _debug "wget returns 8, the server returns a 'Bad request' response, lets process the response later." |
9f43c270 | 1918 | fi |
4c2a3841 | 1919 | if [ "$_ret" != "0" ]; then |
1920 | _err "Please refer to https://www.gnu.org/software/wget/manual/html_node/Exit-Status.html for error code: $_ret" | |
687cfcc2 | 1921 | fi |
c60883ef | 1922 | _sed_i "s/^ *//g" "$HTTP_HEADER" |
d0b748a4 | 1923 | else |
1924 | _ret="$?" | |
1925 | _err "Neither curl nor wget is found, can not do $httpmethod." | |
c60883ef | 1926 | fi |
16679b57 | 1927 | _debug "_ret" "$_ret" |
19539575 | 1928 | printf "%s" "$response" |
16679b57 | 1929 | return $_ret |
c60883ef | 1930 | } |
1931 | ||
75da0713 | 1932 | # url getheader timeout |
c60883ef | 1933 | _get() { |
a4270efa | 1934 | _debug GET |
c60883ef | 1935 | url="$1" |
1936 | onlyheader="$2" | |
75da0713 | 1937 | t="$3" |
79a267ab | 1938 | _debug url "$url" |
72f54ca6 | 1939 | _debug "timeout=$t" |
fac1e367 | 1940 | |
1941 | _inithttp | |
1942 | ||
9b124070 | 1943 | if [ "$_ACME_CURL" ] && [ "${ACME_USE_WGET:-0}" = "0" ]; then |
1befee5a | 1944 | _CURL="$_ACME_CURL" |
7834c252 | 1945 | if [ "$HTTPS_INSECURE" ]; then |
1946 | _CURL="$_CURL --insecure " | |
1947 | fi | |
4c2a3841 | 1948 | if [ "$t" ]; then |
75da0713 | 1949 | _CURL="$_CURL --connect-timeout $t" |
1950 | fi | |
1951 | _debug "_CURL" "$_CURL" | |
4c2a3841 | 1952 | if [ "$onlyheader" ]; then |
f9a6988e | 1953 | $_CURL -I --user-agent "$USER_AGENT" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$url" |
c60883ef | 1954 | else |
f9a6988e | 1955 | $_CURL --user-agent "$USER_AGENT" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$url" |
c60883ef | 1956 | fi |
9aaf36cd | 1957 | ret=$? |
4c2a3841 | 1958 | if [ "$ret" != "0" ]; then |
d529eb6d | 1959 | _err "Please refer to https://curl.haxx.se/libcurl/c/libcurl-errors.html for error code: $ret" |
4c2a3841 | 1960 | if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then |
fac1e367 | 1961 | _err "Here is the curl dump log:" |
1962 | _err "$(cat "$_CURL_DUMP")" | |
1963 | fi | |
1964 | fi | |
4c2a3841 | 1965 | elif [ "$_ACME_WGET" ]; then |
1befee5a | 1966 | _WGET="$_ACME_WGET" |
7834c252 | 1967 | if [ "$HTTPS_INSECURE" ]; then |
1968 | _WGET="$_WGET --no-check-certificate " | |
1969 | fi | |
4c2a3841 | 1970 | if [ "$t" ]; then |
75da0713 | 1971 | _WGET="$_WGET --timeout=$t" |
1972 | fi | |
1973 | _debug "_WGET" "$_WGET" | |
4c2a3841 | 1974 | if [ "$onlyheader" ]; then |
f9a6988e | 1975 | $_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' |
c60883ef | 1976 | else |
f9a6988e | 1977 | $_WGET --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" -O - "$url" |
c60883ef | 1978 | fi |
9aaf36cd | 1979 | ret=$? |
f731a4c7 | 1980 | if [ "$ret" = "8" ]; then |
39a1f1ef | 1981 | ret=0 |
810c129c | 1982 | _debug "wget returns 8, the server returns a 'Bad request' response, lets process the response later." |
9f43c270 | 1983 | fi |
4c2a3841 | 1984 | if [ "$ret" != "0" ]; then |
1985 | _err "Please refer to https://www.gnu.org/software/wget/manual/html_node/Exit-Status.html for error code: $ret" | |
fac1e367 | 1986 | fi |
d0b748a4 | 1987 | else |
1988 | ret=$? | |
1989 | _err "Neither curl nor wget is found, can not do GET." | |
9aaf36cd | 1990 | fi |
ec9fc8cb | 1991 | _debug "ret" "$ret" |
c60883ef | 1992 | return $ret |
1993 | } | |
166096dc | 1994 | |
c2c8f320 | 1995 | _head_n() { |
79a267ab | 1996 | head -n "$1" |
c2c8f320 | 1997 | } |
1998 | ||
1999 | _tail_n() { | |
f9a6988e | 2000 | if ! tail -n "$1" 2>/dev/null; then |
19ab2a29 | 2001 | #fix for solaris |
f9a6988e | 2002 | tail -"$1" |
19ab2a29 | 2003 | fi |
c2c8f320 | 2004 | } |
fac1e367 | 2005 | |
166096dc | 2006 | # url payload needbase64 keyfile |
4c3b3608 | 2007 | _send_signed_request() { |
2008 | url=$1 | |
2009 | payload=$2 | |
2010 | needbase64=$3 | |
166096dc | 2011 | keyfile=$4 |
4c2a3841 | 2012 | if [ -z "$keyfile" ]; then |
166096dc | 2013 | keyfile="$ACCOUNT_KEY_PATH" |
2014 | fi | |
f9a6988e | 2015 | _debug url "$url" |
4c3b3608 | 2016 | _debug payload "$payload" |
4c2a3841 | 2017 | |
2018 | if ! _calcjwk "$keyfile"; then | |
166096dc | 2019 | return 1 |
2020 | fi | |
c60883ef | 2021 | |
cc2d5946 | 2022 | __request_conent_type="$CONTENT_TYPE_JSON" |
2023 | ||
11927a76 | 2024 | payload64=$(printf "%s" "$payload" | _base64 | _url_replace) |
f9a6988e | 2025 | _debug3 payload64 "$payload64" |
4c2a3841 | 2026 | |
709a3fb0 | 2027 | MAX_REQUEST_RETRY_TIMES=20 |
2028 | _sleep_retry_sec=1 | |
0bc745f6 | 2029 | _request_retry_times=0 |
2030 | while [ "${_request_retry_times}" -lt "$MAX_REQUEST_RETRY_TIMES" ]; do | |
aad309ee | 2031 | _request_retry_times=$(_math "$_request_retry_times" + 1) |
b7924ce5 | 2032 | _debug3 _request_retry_times "$_request_retry_times" |
0bc745f6 | 2033 | if [ -z "$_CACHED_NONCE" ]; then |
8f01919f | 2034 | _headers="" |
8bd12ed0 K |
2035 | if [ "$ACME_NEW_NONCE" ]; then |
2036 | _debug2 "Get nonce with HEAD. ACME_NEW_NONCE" "$ACME_NEW_NONCE" | |
cae50e16 | 2037 | nonceurl="$ACME_NEW_NONCE" |
1ba4ab2b | 2038 | if _post "" "$nonceurl" "" "HEAD" "$__request_conent_type" >/dev/null; then |
cae50e16 | 2039 | _headers="$(cat "$HTTP_HEADER")" |
e7f7e96d | 2040 | _debug2 _headers "$_headers" |
2041 | _CACHED_NONCE="$(echo "$_headers" | grep -i "Replay-Nonce:" | _head_n 1 | tr -d "\r\n " | cut -d ':' -f 2)" | |
cae50e16 | 2042 | fi |
2043 | fi | |
e7f7e96d | 2044 | if [ -z "$_CACHED_NONCE" ]; then |
33226307 | 2045 | _debug2 "Get nonce with GET. ACME_DIRECTORY" "$ACME_DIRECTORY" |
cae50e16 | 2046 | nonceurl="$ACME_DIRECTORY" |
2047 | _headers="$(_get "$nonceurl" "onlyheader")" | |
e7f7e96d | 2048 | _debug2 _headers "$_headers" |
2049 | _CACHED_NONCE="$(echo "$_headers" | grep -i "Replay-Nonce:" | _head_n 1 | tr -d "\r\n " | cut -d ':' -f 2)" | |
cae50e16 | 2050 | fi |
e7f7e96d | 2051 | if [ -z "$_CACHED_NONCE" ] && [ "$ACME_NEW_NONCE" ]; then |
2052 | _debug2 "Get nonce with GET. ACME_NEW_NONCE" "$ACME_NEW_NONCE" | |
2053 | nonceurl="$ACME_NEW_NONCE" | |
2054 | _headers="$(_get "$nonceurl" "onlyheader")" | |
2055 | _debug2 _headers "$_headers" | |
2056 | _CACHED_NONCE="$(echo "$_headers" | grep -i "Replay-Nonce:" | _head_n 1 | tr -d "\r\n " | cut -d ':' -f 2)" | |
2057 | fi | |
2058 | _debug2 _CACHED_NONCE "$_CACHED_NONCE" | |
0bc745f6 | 2059 | if [ "$?" != "0" ]; then |
2060 | _err "Can not connect to $nonceurl to get nonce." | |
2061 | return 1 | |
2062 | fi | |
0bc745f6 | 2063 | else |
2064 | _debug2 "Use _CACHED_NONCE" "$_CACHED_NONCE" | |
2065 | fi | |
2066 | nonce="$_CACHED_NONCE" | |
2067 | _debug2 nonce "$nonce" | |
aad309ee | 2068 | if [ -z "$nonce" ]; then |
2069 | _info "Could not get nonce, let's try again." | |
2070 | _sleep 2 | |
2071 | continue | |
2072 | fi | |
c1151b0d | 2073 | if [ "$ACME_VERSION" = "2" ]; then |
114f2a14 | 2074 | if [ "$url" = "$ACME_NEW_ACCOUNT" ]; then |
2075 | protected="$JWK_HEADERPLACE_PART1$nonce\", \"url\": \"${url}$JWK_HEADERPLACE_PART2, \"jwk\": $jwk"'}' | |
2076 | elif [ "$url" = "$ACME_REVOKE_CERT" ] && [ "$keyfile" != "$ACCOUNT_KEY_PATH" ]; then | |
c1151b0d | 2077 | protected="$JWK_HEADERPLACE_PART1$nonce\", \"url\": \"${url}$JWK_HEADERPLACE_PART2, \"jwk\": $jwk"'}' |
2078 | else | |
6b798b01 | 2079 | protected="$JWK_HEADERPLACE_PART1$nonce\", \"url\": \"${url}$JWK_HEADERPLACE_PART2, \"kid\": \"${ACCOUNT_URL}\""'}' |
c1151b0d | 2080 | fi |
2081 | else | |
2082 | protected="$JWK_HEADERPLACE_PART1$nonce\", \"url\": \"${url}$JWK_HEADERPLACE_PART2, \"jwk\": $jwk"'}' | |
2083 | fi | |
0bc745f6 | 2084 | _debug3 protected "$protected" |
a272ee4f | 2085 | |
0bc745f6 | 2086 | protected64="$(printf "%s" "$protected" | _base64 | _url_replace)" |
2087 | _debug3 protected64 "$protected64" | |
4c2a3841 | 2088 | |
0bc745f6 | 2089 | if ! _sig_t="$(printf "%s" "$protected64.$payload64" | _sign "$keyfile" "sha256")"; then |
2090 | _err "Sign request failed." | |
2091 | return 1 | |
2092 | fi | |
2093 | _debug3 _sig_t "$_sig_t" | |
166096dc | 2094 | |
0bc745f6 | 2095 | sig="$(printf "%s" "$_sig_t" | _url_replace)" |
2096 | _debug3 sig "$sig" | |
4c2a3841 | 2097 | |
8bd12ed0 | 2098 | body="{\"protected\": \"$protected64\", \"payload\": \"$payload64\", \"signature\": \"$sig\"}" |
0bc745f6 | 2099 | _debug3 body "$body" |
4c2a3841 | 2100 | |
6a66ba8a | 2101 | response="$(_post "$body" "$url" "$needbase64" "POST" "$__request_conent_type")" |
0bc745f6 | 2102 | _CACHED_NONCE="" |
bbbdcb09 | 2103 | |
0bc745f6 | 2104 | if [ "$?" != "0" ]; then |
2105 | _err "Can not post to $url" | |
2106 | return 1 | |
2107 | fi | |
4c3b3608 | 2108 | |
64802502 | 2109 | responseHeaders="$(cat "$HTTP_HEADER")" |
0bc745f6 | 2110 | _debug2 responseHeaders "$responseHeaders" |
f2acdd27 | 2111 | |
0bc745f6 | 2112 | code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\r\n")" |
2113 | _debug code "$code" | |
4c2a3841 | 2114 | |
f2acdd27 | 2115 | _debug2 original "$response" |
0712e989 | 2116 | if echo "$responseHeaders" | grep -i "Content-Type: *application/json" >/dev/null 2>&1; then |
cc8f2afc | 2117 | response="$(echo "$response" | _json_decode | _normalizeJson)" |
8148bfea | 2118 | fi |
f2acdd27 | 2119 | _debug2 response "$response" |
36309e6d | 2120 | |
f2acdd27 | 2121 | _CACHED_NONCE="$(echo "$responseHeaders" | grep -i "Replay-Nonce:" | _head_n 1 | tr -d "\r\n " | cut -d ':' -f 2)" |
2122 | ||
a0ec5b18 | 2123 | if ! _startswith "$code" "2"; then |
f2acdd27 | 2124 | _body="$response" |
2125 | if [ "$needbase64" ]; then | |
2126 | _body="$(echo "$_body" | _dbase64 multiline)" | |
2127 | _debug3 _body "$_body" | |
2128 | fi | |
2129 | ||
2130 | if _contains "$_body" "JWS has invalid anti-replay nonce" || _contains "$_body" "JWS has an invalid anti-replay nonce"; then | |
2131 | _info "It seems the CA server is busy now, let's wait and retry. Sleeping $_sleep_retry_sec seconds." | |
2132 | _CACHED_NONCE="" | |
2133 | _sleep $_sleep_retry_sec | |
2134 | continue | |
2135 | fi | |
c384ed96 AK |
2136 | if _contains "$_body" "The Replay Nonce is not recognized"; then |
2137 | _info "The replay Nonce is not valid, let's get a new one, Sleeping $_sleep_retry_sec seconds." | |
2138 | _CACHED_NONCE="" | |
2139 | _sleep $_sleep_retry_sec | |
2140 | continue | |
2141 | fi | |
0bc745f6 | 2142 | fi |
a44ea0dd | 2143 | return 0 |
0bc745f6 | 2144 | done |
a44ea0dd | 2145 | _info "Giving up sending to CA server after $MAX_REQUEST_RETRY_TIMES retries." |
2146 | return 1 | |
4c3b3608 | 2147 | |
4c3b3608 | 2148 | } |
4c3b3608 | 2149 | |
2150 | #setopt "file" "opt" "=" "value" [";"] | |
2151 | _setopt() { | |
2152 | __conf="$1" | |
2153 | __opt="$2" | |
2154 | __sep="$3" | |
2155 | __val="$4" | |
2156 | __end="$5" | |
4c2a3841 | 2157 | if [ -z "$__opt" ]; then |
2158 | _usage usage: _setopt '"file" "opt" "=" "value" [";"]' | |
4c3b3608 | 2159 | return |
2160 | fi | |
4c2a3841 | 2161 | if [ ! -f "$__conf" ]; then |
4c3b3608 | 2162 | touch "$__conf" |
2163 | fi | |
2164 | ||
4c2a3841 | 2165 | if grep -n "^$__opt$__sep" "$__conf" >/dev/null; then |
22ea4004 | 2166 | _debug3 OK |
4c2a3841 | 2167 | if _contains "$__val" "&"; then |
79a267ab | 2168 | __val="$(echo "$__val" | sed 's/&/\\&/g')" |
4c3b3608 | 2169 | fi |
79a267ab | 2170 | text="$(cat "$__conf")" |
52f8b787 | 2171 | printf -- "%s\n" "$text" | sed "s|^$__opt$__sep.*$|$__opt$__sep$__val$__end|" >"$__conf" |
4c3b3608 | 2172 | |
4c2a3841 | 2173 | elif grep -n "^#$__opt$__sep" "$__conf" >/dev/null; then |
2174 | if _contains "$__val" "&"; then | |
79a267ab | 2175 | __val="$(echo "$__val" | sed 's/&/\\&/g')" |
4c3b3608 | 2176 | fi |
79a267ab | 2177 | text="$(cat "$__conf")" |
52f8b787 | 2178 | printf -- "%s\n" "$text" | sed "s|^#$__opt$__sep.*$|$__opt$__sep$__val$__end|" >"$__conf" |
4c3b3608 | 2179 | |
2180 | else | |
22ea4004 | 2181 | _debug3 APP |
4c2a3841 | 2182 | echo "$__opt$__sep$__val$__end" >>"$__conf" |
4c3b3608 | 2183 | fi |
1efb2085 | 2184 | _debug3 "$(grep -n "^$__opt$__sep" "$__conf")" |
4c3b3608 | 2185 | } |
2186 | ||
7690f73e | 2187 | #_save_conf file key value base64encode |
8a29fbc8 | 2188 | #save to conf |
2189 | _save_conf() { | |
2190 | _s_c_f="$1" | |
2191 | _sdkey="$2" | |
2192 | _sdvalue="$3" | |
7690f73e | 2193 | _b64encode="$4" |
53c01882 | 2194 | if [ "$_sdvalue" ] && [ "$_b64encode" ]; then |
7690f73e | 2195 | _sdvalue="${B64CONF_START}$(printf "%s" "${_sdvalue}" | _base64)${B64CONF_END}" |
2196 | fi | |
4c2a3841 | 2197 | if [ "$_s_c_f" ]; then |
8a29fbc8 | 2198 | _setopt "$_s_c_f" "$_sdkey" "=" "'$_sdvalue'" |
4d2f38b0 | 2199 | else |
8a29fbc8 | 2200 | _err "config file is empty, can not save $_sdkey=$_sdvalue" |
4d2f38b0 | 2201 | fi |
2202 | } | |
2203 | ||
8a29fbc8 | 2204 | #_clear_conf file key |
2205 | _clear_conf() { | |
2206 | _c_c_f="$1" | |
2207 | _sdkey="$2" | |
4c2a3841 | 2208 | if [ "$_c_c_f" ]; then |
20ea8591 | 2209 | _conf_data="$(cat "$_c_c_f")" |
fa574fe8 | 2210 | echo "$_conf_data" | sed "s/^$_sdkey *=.*$//" >"$_c_c_f" |
4c3b3608 | 2211 | else |
8a29fbc8 | 2212 | _err "config file is empty, can not clear" |
4c3b3608 | 2213 | fi |
2214 | } | |
2215 | ||
8a29fbc8 | 2216 | #_read_conf file key |
2217 | _read_conf() { | |
2218 | _r_c_f="$1" | |
2219 | _sdkey="$2" | |
4c2a3841 | 2220 | if [ -f "$_r_c_f" ]; then |
0cfeee4d | 2221 | _sdv="$( |
2222 | eval "$(grep "^$_sdkey *=" "$_r_c_f")" | |
2223 | eval "printf \"%s\" \"\$$_sdkey\"" | |
c97e43dc | 2224 | )" |
7690f73e | 2225 | if _startswith "$_sdv" "${B64CONF_START}" && _endswith "$_sdv" "${B64CONF_END}"; then |
2226 | _sdv="$(echo "$_sdv" | sed "s/${B64CONF_START}//" | sed "s/${B64CONF_END}//" | _dbase64)" | |
2227 | fi | |
2228 | printf "%s" "$_sdv" | |
61623d22 | 2229 | else |
57e58ce7 | 2230 | _debug "config file is empty, can not read $_sdkey" |
4c3b3608 | 2231 | fi |
4c3b3608 | 2232 | } |
2233 | ||
7690f73e | 2234 | #_savedomainconf key value base64encode |
4c3b3608 | 2235 | #save to domain.conf |
2236 | _savedomainconf() { | |
7690f73e | 2237 | _save_conf "$DOMAIN_CONF" "$@" |
4d2f38b0 | 2238 | } |
2239 | ||
2240 | #_cleardomainconf key | |
2241 | _cleardomainconf() { | |
8a29fbc8 | 2242 | _clear_conf "$DOMAIN_CONF" "$1" |
4c3b3608 | 2243 | } |
2244 | ||
61623d22 | 2245 | #_readdomainconf key |
2246 | _readdomainconf() { | |
8a29fbc8 | 2247 | _read_conf "$DOMAIN_CONF" "$1" |
61623d22 | 2248 | } |
2249 | ||
aec66362 | 2250 | #key value base64encode |
2251 | _savedeployconf() { | |
2252 | _savedomainconf "SAVED_$1" "$2" "$3" | |
2253 | #remove later | |
dc5eda7e | 2254 | _cleardomainconf "$1" |
aec66362 | 2255 | } |
2256 | ||
2257 | #key | |
2258 | _getdeployconf() { | |
2259 | _rac_key="$1" | |
2e3ddd3a | 2260 | _rac_value="$(eval echo \$"$_rac_key")" |
2261 | if [ "$_rac_value" ]; then | |
2262 | if _startswith "$_rac_value" '"' && _endswith "$_rac_value" '"'; then | |
2263 | _debug2 "trim quotation marks" | |
465ece5d | 2264 | eval "export $_rac_key=$_rac_value" |
2e3ddd3a | 2265 | fi |
aec66362 | 2266 | return 0 # do nothing |
2267 | fi | |
2268 | _saved=$(_readdomainconf "SAVED_$_rac_key") | |
22f9a3b4 | 2269 | eval "export $_rac_key=\"$_saved\"" |
aec66362 | 2270 | } |
2271 | ||
7690f73e | 2272 | #_saveaccountconf key value base64encode |
4c3b3608 | 2273 | _saveaccountconf() { |
7690f73e | 2274 | _save_conf "$ACCOUNT_CONF_PATH" "$@" |
4c3b3608 | 2275 | } |
2276 | ||
7690f73e | 2277 | #key value base64encode |
fcdf41ba | 2278 | _saveaccountconf_mutable() { |
7690f73e | 2279 | _save_conf "$ACCOUNT_CONF_PATH" "SAVED_$1" "$2" "$3" |
fcdf41ba | 2280 | #remove later |
2281 | _clearaccountconf "$1" | |
2282 | } | |
2283 | ||
2284 | #key | |
2285 | _readaccountconf() { | |
2286 | _read_conf "$ACCOUNT_CONF_PATH" "$1" | |
2287 | } | |
2288 | ||
2289 | #key | |
2290 | _readaccountconf_mutable() { | |
2291 | _rac_key="$1" | |
2292 | _readaccountconf "SAVED_$_rac_key" | |
2293 | } | |
2294 | ||
fac1e367 | 2295 | #_clearaccountconf key |
2296 | _clearaccountconf() { | |
8a29fbc8 | 2297 | _clear_conf "$ACCOUNT_CONF_PATH" "$1" |
2298 | } | |
2299 | ||
c384ed96 AK |
2300 | #key |
2301 | _clearaccountconf_mutable() { | |
2302 | _clearaccountconf "SAVED_$1" | |
2303 | #remove later | |
2304 | _clearaccountconf "$1" | |
2305 | } | |
2306 | ||
8a29fbc8 | 2307 | #_savecaconf key value |
2308 | _savecaconf() { | |
2309 | _save_conf "$CA_CONF" "$1" "$2" | |
2310 | } | |
2311 | ||
2312 | #_readcaconf key | |
2313 | _readcaconf() { | |
2314 | _read_conf "$CA_CONF" "$1" | |
2315 | } | |
2316 | ||
2317 | #_clearaccountconf key | |
2318 | _clearcaconf() { | |
2319 | _clear_conf "$CA_CONF" "$1" | |
fac1e367 | 2320 | } |
2321 | ||
0463b5d6 | 2322 | # content localaddress |
4c3b3608 | 2323 | _startserver() { |
2324 | content="$1" | |
0463b5d6 | 2325 | ncaddr="$2" |
e7f7e96d | 2326 | _debug "content" "$content" |
0463b5d6 | 2327 | _debug "ncaddr" "$ncaddr" |
2328 | ||
6fc1447f | 2329 | _debug "startserver: $$" |
4c2a3841 | 2330 | |
39c8f79f | 2331 | _debug Le_HTTPPort "$Le_HTTPPort" |
6ae0f7f5 | 2332 | _debug Le_Listen_V4 "$Le_Listen_V4" |
2333 | _debug Le_Listen_V6 "$Le_Listen_V6" | |
4c2a3841 | 2334 | |
3794b5cb | 2335 | _NC="socat" |
4c2a3841 | 2336 | if [ "$Le_Listen_V4" ]; then |
6ae0f7f5 | 2337 | _NC="$_NC -4" |
4c2a3841 | 2338 | elif [ "$Le_Listen_V6" ]; then |
6ae0f7f5 | 2339 | _NC="$_NC -6" |
2340 | fi | |
4c2a3841 | 2341 | |
9ad7ac63 | 2342 | if [ "$DEBUG" ] && [ "$DEBUG" -gt "1" ]; then |
5c568d69 | 2343 | _NC="$_NC -d -d -v" |
2344 | fi | |
2345 | ||
9134b6ea HC |
2346 | SOCAT_OPTIONS=TCP-LISTEN:$Le_HTTPPort,crlf,reuseaddr,fork |
2347 | ||
2348 | #Adding bind to local-address | |
d84665cb | 2349 | if [ "$ncaddr" ]; then |
9ad7ac63 | 2350 | SOCAT_OPTIONS="$SOCAT_OPTIONS,bind=${ncaddr}" |
9134b6ea HC |
2351 | fi |
2352 | ||
e7f7e96d | 2353 | _content_len="$(printf "%s" "$content" | wc -c)" |
2354 | _debug _content_len "$_content_len" | |
5c568d69 | 2355 | _debug "_NC" "$_NC $SOCAT_OPTIONS" |
e7f7e96d | 2356 | $_NC $SOCAT_OPTIONS SYSTEM:"sleep 1; \ |
2357 | echo 'HTTP/1.0 200 OK'; \ | |
2358 | echo 'Content-Length\: $_content_len'; \ | |
2359 | echo ''; \ | |
2ffd8637 | 2360 | printf -- '$content';" & |
3794b5cb | 2361 | serverproc="$!" |
4c3b3608 | 2362 | } |
2363 | ||
4c2a3841 | 2364 | _stopserver() { |
4c3b3608 | 2365 | pid="$1" |
6fc1447f | 2366 | _debug "pid" "$pid" |
4c2a3841 | 2367 | if [ -z "$pid" ]; then |
6fc1447f | 2368 | return |
2369 | fi | |
e22bcf7c | 2370 | |
3794b5cb | 2371 | kill $pid |
2372 | ||
4c3b3608 | 2373 | } |
2374 | ||
fdcb6b72 | 2375 | # sleep sec |
2376 | _sleep() { | |
2377 | _sleep_sec="$1" | |
4c2a3841 | 2378 | if [ "$__INTERACTIVE" ]; then |
fdcb6b72 | 2379 | _sleep_c="$_sleep_sec" |
4c2a3841 | 2380 | while [ "$_sleep_c" -ge "0" ]; do |
c583d6bb | 2381 | printf "\r \r" |
fdcb6b72 | 2382 | __green "$_sleep_c" |
79a267ab | 2383 | _sleep_c="$(_math "$_sleep_c" - 1)" |
fdcb6b72 | 2384 | sleep 1 |
2385 | done | |
c583d6bb | 2386 | printf "\r" |
fdcb6b72 | 2387 | else |
2388 | sleep "$_sleep_sec" | |
2389 | fi | |
2390 | } | |
e22bcf7c | 2391 | |
08681f4a | 2392 | # _starttlsserver san_a san_b port content _ncaddr acmeValidationv1 |
e22bcf7c | 2393 | _starttlsserver() { |
2394 | _info "Starting tls server." | |
2395 | san_a="$1" | |
2396 | san_b="$2" | |
2397 | port="$3" | |
2398 | content="$4" | |
6ae0f7f5 | 2399 | opaddr="$5" |
08681f4a | 2400 | acmeValidationv1="$6" |
4c2a3841 | 2401 | |
e22bcf7c | 2402 | _debug san_a "$san_a" |
2403 | _debug san_b "$san_b" | |
2404 | _debug port "$port" | |
08681f4a | 2405 | _debug acmeValidationv1 "$acmeValidationv1" |
4c2a3841 | 2406 | |
e22bcf7c | 2407 | #create key TLS_KEY |
4c2a3841 | 2408 | if ! _createkey "2048" "$TLS_KEY"; then |
e22bcf7c | 2409 | _err "Create tls validation key error." |
2410 | return 1 | |
2411 | fi | |
4c2a3841 | 2412 | |
e22bcf7c | 2413 | #create csr |
2414 | alt="$san_a" | |
4c2a3841 | 2415 | if [ "$san_b" ]; then |
e22bcf7c | 2416 | alt="$alt,$san_b" |
2417 | fi | |
08681f4a | 2418 | if ! _createcsr "tls.acme.sh" "$alt" "$TLS_KEY" "$TLS_CSR" "$TLS_CONF" "$acmeValidationv1"; then |
e22bcf7c | 2419 | _err "Create tls validation csr error." |
2420 | return 1 | |
2421 | fi | |
4c2a3841 | 2422 | |
e22bcf7c | 2423 | #self signed |
4c2a3841 | 2424 | if ! _signcsr "$TLS_KEY" "$TLS_CSR" "$TLS_CONF" "$TLS_CERT"; then |
e22bcf7c | 2425 | _err "Create tls validation cert error." |
2426 | return 1 | |
2427 | fi | |
4c2a3841 | 2428 | |
5f6e3da7 | 2429 | __S_OPENSSL="${ACME_OPENSSL_BIN:-openssl} s_server -www -cert $TLS_CERT -key $TLS_KEY " |
2430 | if [ "$opaddr" ]; then | |
2431 | __S_OPENSSL="$__S_OPENSSL -accept $opaddr:$port" | |
2432 | else | |
2433 | __S_OPENSSL="$__S_OPENSSL -accept $port" | |
2434 | fi | |
6ae0f7f5 | 2435 | |
2436 | _debug Le_Listen_V4 "$Le_Listen_V4" | |
2437 | _debug Le_Listen_V6 "$Le_Listen_V6" | |
4c2a3841 | 2438 | if [ "$Le_Listen_V4" ]; then |
6ae0f7f5 | 2439 | __S_OPENSSL="$__S_OPENSSL -4" |
4c2a3841 | 2440 | elif [ "$Le_Listen_V6" ]; then |
6ae0f7f5 | 2441 | __S_OPENSSL="$__S_OPENSSL -6" |
2442 | fi | |
4c2a3841 | 2443 | |
08681f4a | 2444 | if [ "$acmeValidationv1" ]; then |
2445 | __S_OPENSSL="$__S_OPENSSL -alpn acme-tls/1" | |
2446 | fi | |
2447 | ||
6ae0f7f5 | 2448 | _debug "$__S_OPENSSL" |
5f6e3da7 | 2449 | if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then |
2450 | $__S_OPENSSL -tlsextdebug & | |
2451 | else | |
2452 | $__S_OPENSSL >/dev/null 2>&1 & | |
2453 | fi | |
331c4bb6 | 2454 | |
e22bcf7c | 2455 | serverproc="$!" |
5dbf664a | 2456 | sleep 1 |
d5ec5f80 | 2457 | _debug serverproc "$serverproc" |
e22bcf7c | 2458 | } |
2459 | ||
18e46962 | 2460 | #file |
2461 | _readlink() { | |
2462 | _rf="$1" | |
2463 | if ! readlink -f "$_rf" 2>/dev/null; then | |
6c4cc357 | 2464 | if _startswith "$_rf" "/"; then |
2465 | echo "$_rf" | |
7da50703 | 2466 | return 0 |
2467 | fi | |
6c4cc357 | 2468 | echo "$(pwd)/$_rf" | _conapath |
18e46962 | 2469 | fi |
2470 | } | |
2471 | ||
6c4cc357 | 2472 | _conapath() { |
2473 | sed "s#/\./#/#g" | |
2474 | } | |
2475 | ||
5ea6e9c9 | 2476 | __initHome() { |
4c2a3841 | 2477 | if [ -z "$_SCRIPT_HOME" ]; then |
2478 | if _exists readlink && _exists dirname; then | |
66990cf8 | 2479 | _debug "Lets find script dir." |
f3e4cea3 | 2480 | _debug "_SCRIPT_" "$_SCRIPT_" |
18e46962 | 2481 | _script="$(_readlink "$_SCRIPT_")" |
f3e4cea3 | 2482 | _debug "_script" "$_script" |
2483 | _script_home="$(dirname "$_script")" | |
2484 | _debug "_script_home" "$_script_home" | |
4c2a3841 | 2485 | if [ -d "$_script_home" ]; then |
f3e4cea3 | 2486 | _SCRIPT_HOME="$_script_home" |
2487 | else | |
2488 | _err "It seems the script home is not correct:$_script_home" | |
2489 | fi | |
2490 | fi | |
2491 | fi | |
2492 | ||
219e9115 | 2493 | # if [ -z "$LE_WORKING_DIR" ]; then |
2494 | # if [ -f "$DEFAULT_INSTALL_HOME/account.conf" ]; then | |
2495 | # _debug "It seems that $PROJECT_NAME is already installed in $DEFAULT_INSTALL_HOME" | |
2496 | # LE_WORKING_DIR="$DEFAULT_INSTALL_HOME" | |
2497 | # else | |
2498 | # LE_WORKING_DIR="$_SCRIPT_HOME" | |
2499 | # fi | |
2500 | # fi | |
4c2a3841 | 2501 | |
2502 | if [ -z "$LE_WORKING_DIR" ]; then | |
f3e4cea3 | 2503 | _debug "Using default home:$DEFAULT_INSTALL_HOME" |
2504 | LE_WORKING_DIR="$DEFAULT_INSTALL_HOME" | |
2505 | fi | |
7da50703 | 2506 | export LE_WORKING_DIR |
f3e4cea3 | 2507 | |
f5b546b3 | 2508 | if [ -z "$LE_CONFIG_HOME" ]; then |
2509 | LE_CONFIG_HOME="$LE_WORKING_DIR" | |
27dbe77f | 2510 | fi |
f5b546b3 | 2511 | _debug "Using config home:$LE_CONFIG_HOME" |
2512 | export LE_CONFIG_HOME | |
27dbe77f | 2513 | |
f5b546b3 | 2514 | _DEFAULT_ACCOUNT_CONF_PATH="$LE_CONFIG_HOME/account.conf" |
d53289d7 | 2515 | |
4c2a3841 | 2516 | if [ -z "$ACCOUNT_CONF_PATH" ]; then |
2517 | if [ -f "$_DEFAULT_ACCOUNT_CONF_PATH" ]; then | |
8663fb7e | 2518 | . "$_DEFAULT_ACCOUNT_CONF_PATH" |
635695ec | 2519 | fi |
d53289d7 | 2520 | fi |
4c2a3841 | 2521 | |
2522 | if [ -z "$ACCOUNT_CONF_PATH" ]; then | |
d53289d7 | 2523 | ACCOUNT_CONF_PATH="$_DEFAULT_ACCOUNT_CONF_PATH" |
4c3b3608 | 2524 | fi |
f8f53a6b | 2525 | _debug3 ACCOUNT_CONF_PATH "$ACCOUNT_CONF_PATH" |
f5b546b3 | 2526 | DEFAULT_LOG_FILE="$LE_CONFIG_HOME/$PROJECT_NAME.log" |
4c2a3841 | 2527 | |
f5b546b3 | 2528 | DEFAULT_CA_HOME="$LE_CONFIG_HOME/ca" |
4c2a3841 | 2529 | |
2530 | if [ -z "$LE_TEMP_DIR" ]; then | |
f5b546b3 | 2531 | LE_TEMP_DIR="$LE_CONFIG_HOME/tmp" |
610e0f21 | 2532 | fi |
5ea6e9c9 | 2533 | } |
2534 | ||
48d9a8c1 | 2535 | #server |
2536 | _initAPI() { | |
2537 | _api_server="${1:-$ACME_DIRECTORY}" | |
2538 | _debug "_init api for server: $_api_server" | |
4cee14f3 | 2539 | |
cae50e16 | 2540 | if [ -z "$ACME_NEW_ACCOUNT" ]; then |
48d9a8c1 | 2541 | response=$(_get "$_api_server") |
2542 | if [ "$?" != "0" ]; then | |
2543 | _debug2 "response" "$response" | |
2544 | _err "Can not init api." | |
2545 | return 1 | |
2546 | fi | |
d8bd45c2 | 2547 | response=$(echo "$response" | _json_decode) |
48d9a8c1 | 2548 | _debug2 "response" "$response" |
2549 | ||
2550 | ACME_KEY_CHANGE=$(echo "$response" | _egrep_o 'key-change" *: *"[^"]*"' | cut -d '"' -f 3) | |
c1151b0d | 2551 | if [ -z "$ACME_KEY_CHANGE" ]; then |
2552 | ACME_KEY_CHANGE=$(echo "$response" | _egrep_o 'keyChange" *: *"[^"]*"' | cut -d '"' -f 3) | |
2553 | fi | |
48d9a8c1 | 2554 | export ACME_KEY_CHANGE |
2555 | ||
2556 | ACME_NEW_AUTHZ=$(echo "$response" | _egrep_o 'new-authz" *: *"[^"]*"' | cut -d '"' -f 3) | |
c1151b0d | 2557 | if [ -z "$ACME_NEW_AUTHZ" ]; then |
2558 | ACME_NEW_AUTHZ=$(echo "$response" | _egrep_o 'newAuthz" *: *"[^"]*"' | cut -d '"' -f 3) | |
2559 | fi | |
48d9a8c1 | 2560 | export ACME_NEW_AUTHZ |
2561 | ||
cae50e16 | 2562 | ACME_NEW_ORDER=$(echo "$response" | _egrep_o 'new-cert" *: *"[^"]*"' | cut -d '"' -f 3) |
a71eba07 | 2563 | ACME_NEW_ORDER_RES="new-cert" |
cae50e16 | 2564 | if [ -z "$ACME_NEW_ORDER" ]; then |
2565 | ACME_NEW_ORDER=$(echo "$response" | _egrep_o 'new-order" *: *"[^"]*"' | cut -d '"' -f 3) | |
a71eba07 | 2566 | ACME_NEW_ORDER_RES="new-order" |
c1151b0d | 2567 | if [ -z "$ACME_NEW_ORDER" ]; then |
2568 | ACME_NEW_ORDER=$(echo "$response" | _egrep_o 'newOrder" *: *"[^"]*"' | cut -d '"' -f 3) | |
2569 | fi | |
cae50e16 | 2570 | fi |
2571 | export ACME_NEW_ORDER | |
a71eba07 | 2572 | export ACME_NEW_ORDER_RES |
48d9a8c1 | 2573 | |
cae50e16 | 2574 | ACME_NEW_ACCOUNT=$(echo "$response" | _egrep_o 'new-reg" *: *"[^"]*"' | cut -d '"' -f 3) |
a71eba07 | 2575 | ACME_NEW_ACCOUNT_RES="new-reg" |
cae50e16 | 2576 | if [ -z "$ACME_NEW_ACCOUNT" ]; then |
2577 | ACME_NEW_ACCOUNT=$(echo "$response" | _egrep_o 'new-account" *: *"[^"]*"' | cut -d '"' -f 3) | |
a71eba07 | 2578 | ACME_NEW_ACCOUNT_RES="new-account" |
c1151b0d | 2579 | if [ -z "$ACME_NEW_ACCOUNT" ]; then |
2580 | ACME_NEW_ACCOUNT=$(echo "$response" | _egrep_o 'newAccount" *: *"[^"]*"' | cut -d '"' -f 3) | |
2581 | if [ "$ACME_NEW_ACCOUNT" ]; then | |
2582 | export ACME_VERSION=2 | |
2583 | fi | |
2584 | fi | |
cae50e16 | 2585 | fi |
2586 | export ACME_NEW_ACCOUNT | |
a71eba07 | 2587 | export ACME_NEW_ACCOUNT_RES |
48d9a8c1 | 2588 | |
2589 | ACME_REVOKE_CERT=$(echo "$response" | _egrep_o 'revoke-cert" *: *"[^"]*"' | cut -d '"' -f 3) | |
c1151b0d | 2590 | if [ -z "$ACME_REVOKE_CERT" ]; then |
2591 | ACME_REVOKE_CERT=$(echo "$response" | _egrep_o 'revokeCert" *: *"[^"]*"' | cut -d '"' -f 3) | |
2592 | fi | |
48d9a8c1 | 2593 | export ACME_REVOKE_CERT |
2594 | ||
cae50e16 | 2595 | ACME_NEW_NONCE=$(echo "$response" | _egrep_o 'new-nonce" *: *"[^"]*"' | cut -d '"' -f 3) |
c1151b0d | 2596 | if [ -z "$ACME_NEW_NONCE" ]; then |
2597 | ACME_NEW_NONCE=$(echo "$response" | _egrep_o 'newNonce" *: *"[^"]*"' | cut -d '"' -f 3) | |
2598 | fi | |
cae50e16 | 2599 | export ACME_NEW_NONCE |
4249e13e | 2600 | |
f87890cb | 2601 | ACME_AGREEMENT=$(echo "$response" | _egrep_o 'terms-of-service" *: *"[^"]*"' | cut -d '"' -f 3) |
c1151b0d | 2602 | if [ -z "$ACME_AGREEMENT" ]; then |
2603 | ACME_AGREEMENT=$(echo "$response" | _egrep_o 'termsOfService" *: *"[^"]*"' | cut -d '"' -f 3) | |
2604 | fi | |
f87890cb | 2605 | export ACME_AGREEMENT |
cae50e16 | 2606 | |
f87890cb | 2607 | _debug "ACME_KEY_CHANGE" "$ACME_KEY_CHANGE" |
2608 | _debug "ACME_NEW_AUTHZ" "$ACME_NEW_AUTHZ" | |
2609 | _debug "ACME_NEW_ORDER" "$ACME_NEW_ORDER" | |
2610 | _debug "ACME_NEW_ACCOUNT" "$ACME_NEW_ACCOUNT" | |
2611 | _debug "ACME_REVOKE_CERT" "$ACME_REVOKE_CERT" | |
2612 | _debug "ACME_AGREEMENT" "$ACME_AGREEMENT" | |
c1151b0d | 2613 | _debug "ACME_NEW_NONCE" "$ACME_NEW_NONCE" |
2614 | _debug "ACME_VERSION" "$ACME_VERSION" | |
48d9a8c1 | 2615 | |
f87890cb | 2616 | fi |
48d9a8c1 | 2617 | } |
2618 | ||
3281043e | 2619 | #[domain] [keylength or isEcc flag] |
5ea6e9c9 | 2620 | _initpath() { |
cd9fb3b6 | 2621 | domain="$1" |
2622 | _ilength="$2" | |
5ea6e9c9 | 2623 | |
2624 | __initHome | |
2625 | ||
4c2a3841 | 2626 | if [ -f "$ACCOUNT_CONF_PATH" ]; then |
8663fb7e | 2627 | . "$ACCOUNT_CONF_PATH" |
4c3b3608 | 2628 | fi |
2629 | ||
bd04638d | 2630 | if [ "$_ACME_IN_CRON" ]; then |
4c2a3841 | 2631 | if [ ! "$_USER_PATH_EXPORTED" ]; then |
281aa349 | 2632 | _USER_PATH_EXPORTED=1 |
2633 | export PATH="$USER_PATH:$PATH" | |
2634 | fi | |
2635 | fi | |
4c2a3841 | 2636 | |
2637 | if [ -z "$CA_HOME" ]; then | |
5c48e139 | 2638 | CA_HOME="$DEFAULT_CA_HOME" |
2639 | fi | |
281aa349 | 2640 | |
48d9a8c1 | 2641 | if [ -z "$ACME_DIRECTORY" ]; then |
b3a801df | 2642 | if [ "$STAGE" ]; then |
2643 | ACME_DIRECTORY="$DEFAULT_STAGING_CA" | |
bd04638d | 2644 | _info "Using ACME_DIRECTORY: $ACME_DIRECTORY" |
4c3b3608 | 2645 | else |
b3a801df | 2646 | default_acme_server=$(_readaccountconf "DEFAULT_ACME_SERVER") |
2647 | _debug default_acme_server "$default_acme_server" | |
2648 | if [ "$default_acme_server" ]; then | |
2649 | ACME_DIRECTORY="$default_acme_server" | |
737e9e48 | 2650 | else |
b3a801df | 2651 | ACME_DIRECTORY="$DEFAULT_CA" |
737e9e48 | 2652 | fi |
4c2a3841 | 2653 | fi |
4c3b3608 | 2654 | fi |
4c2a3841 | 2655 | |
66444663 | 2656 | _debug ACME_DIRECTORY "$ACME_DIRECTORY" |
98394f99 | 2657 | _ACME_SERVER_HOST="$(echo "$ACME_DIRECTORY" | cut -d : -f 2 | tr -s / | cut -d / -f 2)" |
48d9a8c1 | 2658 | _debug2 "_ACME_SERVER_HOST" "$_ACME_SERVER_HOST" |
2659 | ||
2660 | CA_DIR="$CA_HOME/$_ACME_SERVER_HOST" | |
4c2a3841 | 2661 | |
5c48e139 | 2662 | _DEFAULT_CA_CONF="$CA_DIR/ca.conf" |
4c2a3841 | 2663 | |
2664 | if [ -z "$CA_CONF" ]; then | |
5c48e139 | 2665 | CA_CONF="$_DEFAULT_CA_CONF" |
2666 | fi | |
c4236e58 | 2667 | _debug3 CA_CONF "$CA_CONF" |
4c2a3841 | 2668 | |
2669 | if [ -f "$CA_CONF" ]; then | |
5c48e139 | 2670 | . "$CA_CONF" |
2671 | fi | |
2672 | ||
4c2a3841 | 2673 | if [ -z "$ACME_DIR" ]; then |
4c3b3608 | 2674 | ACME_DIR="/home/.acme" |
2675 | fi | |
4c2a3841 | 2676 | |
2677 | if [ -z "$APACHE_CONF_BACKUP_DIR" ]; then | |
f5b546b3 | 2678 | APACHE_CONF_BACKUP_DIR="$LE_CONFIG_HOME" |
4c3b3608 | 2679 | fi |
4c2a3841 | 2680 | |
2681 | if [ -z "$USER_AGENT" ]; then | |
bbbdcb09 | 2682 | USER_AGENT="$DEFAULT_USER_AGENT" |
2683 | fi | |
4c2a3841 | 2684 | |
2685 | if [ -z "$HTTP_HEADER" ]; then | |
f5b546b3 | 2686 | HTTP_HEADER="$LE_CONFIG_HOME/http.header" |
933c169d | 2687 | fi |
b2817897 | 2688 | |
5c48e139 | 2689 | _OLD_ACCOUNT_KEY="$LE_WORKING_DIR/account.key" |
2690 | _OLD_ACCOUNT_JSON="$LE_WORKING_DIR/account.json" | |
4c2a3841 | 2691 | |
5c48e139 | 2692 | _DEFAULT_ACCOUNT_KEY_PATH="$CA_DIR/account.key" |
2693 | _DEFAULT_ACCOUNT_JSON_PATH="$CA_DIR/account.json" | |
4c2a3841 | 2694 | if [ -z "$ACCOUNT_KEY_PATH" ]; then |
b2817897 | 2695 | ACCOUNT_KEY_PATH="$_DEFAULT_ACCOUNT_KEY_PATH" |
4c3b3608 | 2696 | fi |
4c2a3841 | 2697 | |
2698 | if [ -z "$ACCOUNT_JSON_PATH" ]; then | |
5c48e139 | 2699 | ACCOUNT_JSON_PATH="$_DEFAULT_ACCOUNT_JSON_PATH" |
2700 | fi | |
4c2a3841 | 2701 | |
f5b546b3 | 2702 | _DEFAULT_CERT_HOME="$LE_CONFIG_HOME" |
4c2a3841 | 2703 | if [ -z "$CERT_HOME" ]; then |
a79b26af RD |
2704 | CERT_HOME="$_DEFAULT_CERT_HOME" |
2705 | fi | |
2706 | ||
77f1ea40 | 2707 | if [ -z "$ACME_OPENSSL_BIN" ] || [ ! -f "$ACME_OPENSSL_BIN" ] || [ ! -x "$ACME_OPENSSL_BIN" ]; then |
851fedf7 | 2708 | ACME_OPENSSL_BIN="$DEFAULT_OPENSSL_BIN" |
a746139c | 2709 | fi |
2710 | ||
cd9fb3b6 | 2711 | if [ -z "$domain" ]; then |
4c3b3608 | 2712 | return 0 |
2713 | fi | |
4c2a3841 | 2714 | |
4c2a3841 | 2715 | if [ -z "$DOMAIN_PATH" ]; then |
43822d37 | 2716 | domainhome="$CERT_HOME/$domain" |
2717 | domainhomeecc="$CERT_HOME/$domain$ECC_SUFFIX" | |
4c2a3841 | 2718 | |
4c3b3608 | 2719 | DOMAIN_PATH="$domainhome" |
4c2a3841 | 2720 | |
2721 | if _isEccKey "$_ilength"; then | |
43822d37 | 2722 | DOMAIN_PATH="$domainhomeecc" |
2723 | else | |
4c2a3841 | 2724 | if [ ! -d "$domainhome" ] && [ -d "$domainhomeecc" ]; then |
6d4e903b | 2725 | _info "The domain '$domain' seems to have a ECC cert already, please add '$(__red "--ecc")' parameter if you want to use that cert." |
43822d37 | 2726 | fi |
2727 | fi | |
2728 | _debug DOMAIN_PATH "$DOMAIN_PATH" | |
4c3b3608 | 2729 | fi |
4c2a3841 | 2730 | |
fd72cced | 2731 | if [ -z "$DOMAIN_BACKUP_PATH" ]; then |
d88f8e86 | 2732 | DOMAIN_BACKUP_PATH="$DOMAIN_PATH/backup" |
fd72cced | 2733 | fi |
2734 | ||
4c2a3841 | 2735 | if [ -z "$DOMAIN_CONF" ]; then |
43822d37 | 2736 | DOMAIN_CONF="$DOMAIN_PATH/$domain.conf" |
4c3b3608 | 2737 | fi |
4c2a3841 | 2738 | |
2739 | if [ -z "$DOMAIN_SSL_CONF" ]; then | |
0c9546cc | 2740 | DOMAIN_SSL_CONF="$DOMAIN_PATH/$domain.csr.conf" |
4c3b3608 | 2741 | fi |
4c2a3841 | 2742 | |
2743 | if [ -z "$CSR_PATH" ]; then | |
43822d37 | 2744 | CSR_PATH="$DOMAIN_PATH/$domain.csr" |
4c3b3608 | 2745 | fi |
4c2a3841 | 2746 | if [ -z "$CERT_KEY_PATH" ]; then |
43822d37 | 2747 | CERT_KEY_PATH="$DOMAIN_PATH/$domain.key" |
4c3b3608 | 2748 | fi |
4c2a3841 | 2749 | if [ -z "$CERT_PATH" ]; then |
43822d37 | 2750 | CERT_PATH="$DOMAIN_PATH/$domain.cer" |
4c3b3608 | 2751 | fi |
4c2a3841 | 2752 | if [ -z "$CA_CERT_PATH" ]; then |
43822d37 | 2753 | CA_CERT_PATH="$DOMAIN_PATH/ca.cer" |
4c3b3608 | 2754 | fi |
4c2a3841 | 2755 | if [ -z "$CERT_FULLCHAIN_PATH" ]; then |
43822d37 | 2756 | CERT_FULLCHAIN_PATH="$DOMAIN_PATH/fullchain.cer" |
caf1fc10 | 2757 | fi |
4c2a3841 | 2758 | if [ -z "$CERT_PFX_PATH" ]; then |
43822d37 | 2759 | CERT_PFX_PATH="$DOMAIN_PATH/$domain.pfx" |
ac2d5123 | 2760 | fi |
4410226d | 2761 | if [ -z "$CERT_PKCS8_PATH" ]; then |
2762 | CERT_PKCS8_PATH="$DOMAIN_PATH/$domain.pkcs8" | |
2763 | fi | |
4c2a3841 | 2764 | |
2765 | if [ -z "$TLS_CONF" ]; then | |
f94433e5 | 2766 | TLS_CONF="$DOMAIN_PATH/tls.validation.conf" |
e22bcf7c | 2767 | fi |
4c2a3841 | 2768 | if [ -z "$TLS_CERT" ]; then |
f94433e5 | 2769 | TLS_CERT="$DOMAIN_PATH/tls.validation.cert" |
e22bcf7c | 2770 | fi |
4c2a3841 | 2771 | if [ -z "$TLS_KEY" ]; then |
f94433e5 | 2772 | TLS_KEY="$DOMAIN_PATH/tls.validation.key" |
e22bcf7c | 2773 | fi |
4c2a3841 | 2774 | if [ -z "$TLS_CSR" ]; then |
f94433e5 | 2775 | TLS_CSR="$DOMAIN_PATH/tls.validation.csr" |
e22bcf7c | 2776 | fi |
4c2a3841 | 2777 | |
4c3b3608 | 2778 | } |
2779 | ||
610e0f21 | 2780 | _exec() { |
4c2a3841 | 2781 | if [ -z "$_EXEC_TEMP_ERR" ]; then |
610e0f21 | 2782 | _EXEC_TEMP_ERR="$(_mktemp)" |
2783 | fi | |
2784 | ||
4c2a3841 | 2785 | if [ "$_EXEC_TEMP_ERR" ]; then |
3e5b1024 | 2786 | eval "$@ 2>>$_EXEC_TEMP_ERR" |
610e0f21 | 2787 | else |
3e5b1024 | 2788 | eval "$@" |
610e0f21 | 2789 | fi |
2790 | } | |
2791 | ||
2792 | _exec_err() { | |
3e5b1024 | 2793 | [ "$_EXEC_TEMP_ERR" ] && _err "$(cat "$_EXEC_TEMP_ERR")" && echo "" >"$_EXEC_TEMP_ERR" |
610e0f21 | 2794 | } |
4c3b3608 | 2795 | |
2796 | _apachePath() { | |
c3dd3ef0 | 2797 | _APACHECTL="apachectl" |
4c2a3841 | 2798 | if ! _exists apachectl; then |
2799 | if _exists apache2ctl; then | |
2800 | _APACHECTL="apache2ctl" | |
e4a19585 | 2801 | else |
bc96082f | 2802 | _err "'apachectl not found. It seems that apache is not installed, or you are not root user.'" |
e4a19585 | 2803 | _err "Please use webroot mode to try again." |
2804 | return 1 | |
2805 | fi | |
80a0a7b5 | 2806 | fi |
4c2a3841 | 2807 | |
2808 | if ! _exec $_APACHECTL -V >/dev/null; then | |
610e0f21 | 2809 | _exec_err |
2810 | return 1 | |
2811 | fi | |
4c2a3841 | 2812 | |
2813 | if [ "$APACHE_HTTPD_CONF" ]; then | |
5be1449d | 2814 | _saveaccountconf APACHE_HTTPD_CONF "$APACHE_HTTPD_CONF" |
2815 | httpdconf="$APACHE_HTTPD_CONF" | |
79a267ab | 2816 | httpdconfname="$(basename "$httpdconfname")" |
d62ee940 | 2817 | else |
4c2a3841 | 2818 | httpdconfname="$($_APACHECTL -V | grep SERVER_CONFIG_FILE= | cut -d = -f 2 | tr -d '"')" |
5be1449d | 2819 | _debug httpdconfname "$httpdconfname" |
4c2a3841 | 2820 | |
2821 | if [ -z "$httpdconfname" ]; then | |
5be1449d | 2822 | _err "Can not read apache config file." |
2823 | return 1 | |
2824 | fi | |
4c2a3841 | 2825 | |
2826 | if _startswith "$httpdconfname" '/'; then | |
5be1449d | 2827 | httpdconf="$httpdconfname" |
79a267ab | 2828 | httpdconfname="$(basename "$httpdconfname")" |
5be1449d | 2829 | else |
4c2a3841 | 2830 | httpdroot="$($_APACHECTL -V | grep HTTPD_ROOT= | cut -d = -f 2 | tr -d '"')" |
5be1449d | 2831 | _debug httpdroot "$httpdroot" |
2832 | httpdconf="$httpdroot/$httpdconfname" | |
79a267ab | 2833 | httpdconfname="$(basename "$httpdconfname")" |
5be1449d | 2834 | fi |
d62ee940 | 2835 | fi |
78768e98 | 2836 | _debug httpdconf "$httpdconf" |
8f63baf7 | 2837 | _debug httpdconfname "$httpdconfname" |
4c2a3841 | 2838 | if [ ! -f "$httpdconf" ]; then |
78768e98 | 2839 | _err "Apache Config file not found" "$httpdconf" |
4c3b3608 | 2840 | return 1 |
2841 | fi | |
2842 | return 0 | |
2843 | } | |
2844 | ||
2845 | _restoreApache() { | |
4c2a3841 | 2846 | if [ -z "$usingApache" ]; then |
4c3b3608 | 2847 | return 0 |
2848 | fi | |
2849 | _initpath | |
4c2a3841 | 2850 | if ! _apachePath; then |
4c3b3608 | 2851 | return 1 |
2852 | fi | |
4c2a3841 | 2853 | |
2854 | if [ ! -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname" ]; then | |
4c3b3608 | 2855 | _debug "No config file to restore." |
2856 | return 0 | |
2857 | fi | |
4c2a3841 | 2858 | |
2859 | cat "$APACHE_CONF_BACKUP_DIR/$httpdconfname" >"$httpdconf" | |
5ef501c5 | 2860 | _debug "Restored: $httpdconf." |
4c2a3841 | 2861 | if ! _exec $_APACHECTL -t; then |
610e0f21 | 2862 | _exec_err |
4c3b3608 | 2863 | _err "Sorry, restore apache config error, please contact me." |
4c2a3841 | 2864 | return 1 |
4c3b3608 | 2865 | fi |
5ef501c5 | 2866 | _debug "Restored successfully." |
4c3b3608 | 2867 | rm -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname" |
4c2a3841 | 2868 | return 0 |
4c3b3608 | 2869 | } |
2870 | ||
2871 | _setApache() { | |
2872 | _initpath | |
4c2a3841 | 2873 | if ! _apachePath; then |
4c3b3608 | 2874 | return 1 |
2875 | fi | |
2876 | ||
5fc5016d | 2877 | #test the conf first |
869578ce | 2878 | _info "Checking if there is an error in the apache config file before starting." |
4c2a3841 | 2879 | |
44edb2bd | 2880 | if ! _exec "$_APACHECTL" -t >/dev/null; then |
610e0f21 | 2881 | _exec_err |
2882 | _err "The apache config file has error, please fix it first, then try again." | |
869578ce | 2883 | _err "Don't worry, there is nothing changed to your system." |
4c2a3841 | 2884 | return 1 |
5fc5016d | 2885 | else |
2886 | _info "OK" | |
2887 | fi | |
4c2a3841 | 2888 | |
4c3b3608 | 2889 | #backup the conf |
5778811a | 2890 | _debug "Backup apache config file" "$httpdconf" |
4c2a3841 | 2891 | if ! cp "$httpdconf" "$APACHE_CONF_BACKUP_DIR/"; then |
869578ce | 2892 | _err "Can not backup apache config file, so abort. Don't worry, the apache config is not changed." |
61a48a5b | 2893 | _err "This might be a bug of $PROJECT_NAME , please report issue: $PROJECT" |
8f63baf7 | 2894 | return 1 |
2895 | fi | |
4c3b3608 | 2896 | _info "JFYI, Config file $httpdconf is backuped to $APACHE_CONF_BACKUP_DIR/$httpdconfname" |
2897 | _info "In case there is an error that can not be restored automatically, you may try restore it yourself." | |
329174b6 | 2898 | _info "The backup file will be deleted on success, just forget it." |
4c2a3841 | 2899 | |
4c3b3608 | 2900 | #add alias |
4c2a3841 | 2901 | |
2902 | apacheVer="$($_APACHECTL -V | grep "Server version:" | cut -d : -f 2 | cut -d " " -f 2 | cut -d '/' -f 2)" | |
b09d597c | 2903 | _debug "apacheVer" "$apacheVer" |
94787d53 | 2904 | apacheMajor="$(echo "$apacheVer" | cut -d . -f 1)" |
b09d597c | 2905 | apacheMinor="$(echo "$apacheVer" | cut -d . -f 2)" |
2906 | ||
94787d53 | 2907 | if [ "$apacheVer" ] && [ "$apacheMajor$apacheMinor" -ge "24" ]; then |
b09d597c | 2908 | echo " |
4c3b3608 | 2909 | Alias /.well-known/acme-challenge $ACME_DIR |
2910 | ||
2911 | <Directory $ACME_DIR > | |
2912 | Require all granted | |
b09d597c | 2913 | </Directory> |
4c2a3841 | 2914 | " >>"$httpdconf" |
b09d597c | 2915 | else |
2916 | echo " | |
2917 | Alias /.well-known/acme-challenge $ACME_DIR | |
2918 | ||
2919 | <Directory $ACME_DIR > | |
2920 | Order allow,deny | |
2921 | Allow from all | |
4c3b3608 | 2922 | </Directory> |
4c2a3841 | 2923 | " >>"$httpdconf" |
b09d597c | 2924 | fi |
2925 | ||
4c2a3841 | 2926 | _msg="$($_APACHECTL -t 2>&1)" |
2927 | if [ "$?" != "0" ]; then | |
5fc5016d | 2928 | _err "Sorry, apache config error" |
4c2a3841 | 2929 | if _restoreApache; then |
869578ce | 2930 | _err "The apache config file is restored." |
5fc5016d | 2931 | else |
2e87e64b | 2932 | _err "Sorry, the apache config file can not be restored, please report bug." |
5fc5016d | 2933 | fi |
4c2a3841 | 2934 | return 1 |
4c3b3608 | 2935 | fi |
4c2a3841 | 2936 | |
2937 | if [ ! -d "$ACME_DIR" ]; then | |
4c3b3608 | 2938 | mkdir -p "$ACME_DIR" |
2939 | chmod 755 "$ACME_DIR" | |
2940 | fi | |
4c2a3841 | 2941 | |
44edb2bd | 2942 | if ! _exec "$_APACHECTL" graceful; then |
4c2a3841 | 2943 | _exec_err |
610e0f21 | 2944 | _err "$_APACHECTL graceful error, please contact me." |
4c3b3608 | 2945 | _restoreApache |
4c2a3841 | 2946 | return 1 |
4c3b3608 | 2947 | fi |
2948 | usingApache="1" | |
2949 | return 0 | |
2950 | } | |
2951 | ||
9d725af6 | 2952 | #find the real nginx conf file |
2953 | #backup | |
2954 | #set the nginx conf | |
2955 | #returns the real nginx conf file | |
2956 | _setNginx() { | |
2957 | _d="$1" | |
2958 | _croot="$2" | |
2959 | _thumbpt="$3" | |
37f39c08 | 2960 | |
9d725af6 | 2961 | FOUND_REAL_NGINX_CONF="" |
9f90618a | 2962 | FOUND_REAL_NGINX_CONF_LN="" |
9d725af6 | 2963 | BACKUP_NGINX_CONF="" |
2964 | _debug _croot "$_croot" | |
2965 | _start_f="$(echo "$_croot" | cut -d : -f 2)" | |
2966 | _debug _start_f "$_start_f" | |
2967 | if [ -z "$_start_f" ]; then | |
2968 | _debug "find start conf from nginx command" | |
2969 | if [ -z "$NGINX_CONF" ]; then | |
37f39c08 | 2970 | if ! _exists "nginx"; then |
2971 | _err "nginx command is not found." | |
2972 | return 1 | |
2973 | fi | |
9d725af6 | 2974 | NGINX_CONF="$(nginx -V 2>&1 | _egrep_o "--conf-path=[^ ]* " | tr -d " ")" |
2975 | _debug NGINX_CONF "$NGINX_CONF" | |
2976 | NGINX_CONF="$(echo "$NGINX_CONF" | cut -d = -f 2)" | |
2977 | _debug NGINX_CONF "$NGINX_CONF" | |
1081d98b | 2978 | if [ -z "$NGINX_CONF" ]; then |
2979 | _err "Can not find nginx conf." | |
2980 | NGINX_CONF="" | |
2981 | return 1 | |
2982 | fi | |
9d725af6 | 2983 | if [ ! -f "$NGINX_CONF" ]; then |
2984 | _err "'$NGINX_CONF' doesn't exist." | |
2985 | NGINX_CONF="" | |
2986 | return 1 | |
2987 | fi | |
2988 | _debug "Found nginx conf file:$NGINX_CONF" | |
2989 | fi | |
2990 | _start_f="$NGINX_CONF" | |
2991 | fi | |
03f8d6e9 | 2992 | _debug "Start detect nginx conf for $_d from:$_start_f" |
9d725af6 | 2993 | if ! _checkConf "$_d" "$_start_f"; then |
5378d9ca | 2994 | _err "Can not find conf file for domain $d" |
9d725af6 | 2995 | return 1 |
2996 | fi | |
2997 | _info "Found conf file: $FOUND_REAL_NGINX_CONF" | |
2998 | ||
9f90618a | 2999 | _ln=$FOUND_REAL_NGINX_CONF_LN |
03f8d6e9 | 3000 | _debug "_ln" "$_ln" |
3001 | ||
3002 | _lnn=$(_math $_ln + 1) | |
3003 | _debug _lnn "$_lnn" | |
3004 | _start_tag="$(sed -n "$_lnn,${_lnn}p" "$FOUND_REAL_NGINX_CONF")" | |
3005 | _debug "_start_tag" "$_start_tag" | |
3006 | if [ "$_start_tag" = "$NGINX_START" ]; then | |
3007 | _info "The domain $_d is already configured, skip" | |
3008 | FOUND_REAL_NGINX_CONF="" | |
3009 | return 0 | |
3010 | fi | |
3011 | ||
9d725af6 | 3012 | mkdir -p "$DOMAIN_BACKUP_PATH" |
3013 | _backup_conf="$DOMAIN_BACKUP_PATH/$_d.nginx.conf" | |
3014 | _debug _backup_conf "$_backup_conf" | |
3015 | BACKUP_NGINX_CONF="$_backup_conf" | |
3016 | _info "Backup $FOUND_REAL_NGINX_CONF to $_backup_conf" | |
3017 | if ! cp "$FOUND_REAL_NGINX_CONF" "$_backup_conf"; then | |
3018 | _err "backup error." | |
3019 | FOUND_REAL_NGINX_CONF="" | |
3020 | return 1 | |
3021 | fi | |
3022 | ||
37f39c08 | 3023 | if ! _exists "nginx"; then |
3024 | _err "nginx command is not found." | |
3025 | return 1 | |
3026 | fi | |
9d725af6 | 3027 | _info "Check the nginx conf before setting up." |
3028 | if ! _exec "nginx -t" >/dev/null; then | |
3029 | _exec_err | |
3030 | return 1 | |
3031 | fi | |
3032 | ||
3033 | _info "OK, Set up nginx config file" | |
9d725af6 | 3034 | |
302c41ed | 3035 | if ! sed -n "1,${_ln}p" "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"; then |
03f8d6e9 | 3036 | cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF" |
9d725af6 | 3037 | _err "write nginx conf error, but don't worry, the file is restored to the original version." |
3038 | return 1 | |
3039 | fi | |
3040 | ||
03f8d6e9 | 3041 | echo "$NGINX_START |
9d725af6 | 3042 | location ~ \"^/\.well-known/acme-challenge/([-_a-zA-Z0-9]+)\$\" { |
3043 | default_type text/plain; | |
3044 | return 200 \"\$1.$_thumbpt\"; | |
3c07f57a | 3045 | } |
03f8d6e9 | 3046 | #NGINX_START |
3047 | " >>"$FOUND_REAL_NGINX_CONF" | |
9d725af6 | 3048 | |
03f8d6e9 | 3049 | if ! sed -n "${_lnn},99999p" "$_backup_conf" >>"$FOUND_REAL_NGINX_CONF"; then |
3050 | cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF" | |
9d725af6 | 3051 | _err "write nginx conf error, but don't worry, the file is restored." |
3052 | return 1 | |
3053 | fi | |
df711b0e | 3054 | _debug3 "Modified config:$(cat $FOUND_REAL_NGINX_CONF)" |
9d725af6 | 3055 | _info "nginx conf is done, let's check it again." |
3056 | if ! _exec "nginx -t" >/dev/null; then | |
3057 | _exec_err | |
3058 | _err "It seems that nginx conf was broken, let's restore." | |
302c41ed | 3059 | cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF" |
9d725af6 | 3060 | return 1 |
3061 | fi | |
3062 | ||
3063 | _info "Reload nginx" | |
3064 | if ! _exec "nginx -s reload" >/dev/null; then | |
3065 | _exec_err | |
3066 | _err "It seems that nginx reload error, let's restore." | |
302c41ed | 3067 | cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF" |
9d725af6 | 3068 | return 1 |
3069 | fi | |
3070 | ||
3071 | return 0 | |
3072 | } | |
3073 | ||
3074 | #d , conf | |
3075 | _checkConf() { | |
3076 | _d="$1" | |
3077 | _c_file="$2" | |
3078 | _debug "Start _checkConf from:$_c_file" | |
3079 | if [ ! -f "$2" ] && ! echo "$2" | grep '*$' >/dev/null && echo "$2" | grep '*' >/dev/null; then | |
3080 | _debug "wildcard" | |
3081 | for _w_f in $2; do | |
7f618e7e | 3082 | if [ -f "$_w_f" ] && _checkConf "$1" "$_w_f"; then |
9d725af6 | 3083 | return 0 |
3084 | fi | |
3085 | done | |
3086 | #not found | |
3087 | return 1 | |
3088 | elif [ -f "$2" ]; then | |
3089 | _debug "single" | |
3090 | if _isRealNginxConf "$1" "$2"; then | |
3091 | _debug "$2 is found." | |
3092 | FOUND_REAL_NGINX_CONF="$2" | |
3093 | return 0 | |
3094 | fi | |
f08a79d3 | 3095 | if cat "$2" | tr "\t" " " | grep "^ *include *.*;" >/dev/null; then |
9d725af6 | 3096 | _debug "Try include files" |
f08a79d3 | 3097 | for included in $(cat "$2" | tr "\t" " " | grep "^ *include *.*;" | sed "s/include //" | tr -d " ;"); do |
9d725af6 | 3098 | _debug "check included $included" |
3099 | if _checkConf "$1" "$included"; then | |
3100 | return 0 | |
3101 | fi | |
3102 | done | |
3103 | fi | |
3104 | return 1 | |
3105 | else | |
3106 | _debug "$2 not found." | |
3107 | return 1 | |
3108 | fi | |
3109 | return 1 | |
3110 | } | |
3111 | ||
3112 | #d , conf | |
3113 | _isRealNginxConf() { | |
3114 | _debug "_isRealNginxConf $1 $2" | |
302c41ed | 3115 | if [ -f "$2" ]; then |
3f1a76d9 | 3116 | for _fln in $(tr "\t" ' ' <"$2" | grep -n "^ *server_name.* $1" | cut -d : -f 1); do |
302c41ed | 3117 | _debug _fln "$_fln" |
3118 | if [ "$_fln" ]; then | |
04a609b5 | 3119 | _start=$(tr "\t" ' ' <"$2" | _head_n "$_fln" | grep -n "^ *server *" | grep -v server_name | _tail_n 1) |
9f90618a | 3120 | _debug "_start" "$_start" |
3121 | _start_n=$(echo "$_start" | cut -d : -f 1) | |
3122 | _start_nn=$(_math $_start_n + 1) | |
3123 | _debug "_start_n" "$_start_n" | |
3124 | _debug "_start_nn" "$_start_nn" | |
3125 | ||
3126 | _left="$(sed -n "${_start_nn},99999p" "$2")" | |
3127 | _debug2 _left "$_left" | |
012dd698 | 3128 | _end="$(echo "$_left" | tr "\t" ' ' | grep -n "^ *server *" | grep -v server_name | _head_n 1)" |
3129 | _debug "_end" "$_end" | |
3130 | if [ "$_end" ]; then | |
9f90618a | 3131 | _end_n=$(echo "$_end" | cut -d : -f 1) |
3132 | _debug "_end_n" "$_end_n" | |
3133 | _seg_n=$(echo "$_left" | sed -n "1,${_end_n}p") | |
3134 | else | |
3135 | _seg_n="$_left" | |
3136 | fi | |
3137 | ||
3138 | _debug "_seg_n" "$_seg_n" | |
3139 | ||
04a609b5 | 3140 | _skip_ssl=1 |
d1067c60 | 3141 | for _listen_i in $(echo "$_seg_n" | tr "\t" ' ' | grep "^ *listen" | tr -d " "); do |
04a609b5 | 3142 | if [ "$_listen_i" ]; then |
9841063d | 3143 | if [ "$(echo "$_listen_i" | _egrep_o "listen.*ssl")" ]; then |
04a609b5 | 3144 | _debug2 "$_listen_i is ssl" |
3145 | else | |
3146 | _debug2 "$_listen_i is plain text" | |
3147 | _skip_ssl="" | |
c05eb0b1 | 3148 | break |
3149 | fi | |
04a609b5 | 3150 | fi |
3151 | done | |
3152 | ||
3153 | if [ "$_skip_ssl" = "1" ]; then | |
9f90618a | 3154 | _debug "ssl on, skip" |
241cfc43 | 3155 | else |
3156 | FOUND_REAL_NGINX_CONF_LN=$_fln | |
3157 | _debug3 "found FOUND_REAL_NGINX_CONF_LN" "$FOUND_REAL_NGINX_CONF_LN" | |
3158 | return 0 | |
450efea1 | 3159 | fi |
302c41ed | 3160 | fi |
3161 | done | |
9d725af6 | 3162 | fi |
302c41ed | 3163 | return 1 |
9d725af6 | 3164 | } |
3165 | ||
3166 | #restore all the nginx conf | |
3167 | _restoreNginx() { | |
5d943a35 | 3168 | if [ -z "$NGINX_RESTORE_VLIST" ]; then |
9d725af6 | 3169 | _debug "No need to restore nginx, skip." |
3170 | return | |
3171 | fi | |
3172 | _debug "_restoreNginx" | |
5d943a35 | 3173 | _debug "NGINX_RESTORE_VLIST" "$NGINX_RESTORE_VLIST" |
9d725af6 | 3174 | |
5d943a35 | 3175 | for ng_entry in $(echo "$NGINX_RESTORE_VLIST" | tr "$dvsep" ' '); do |
9d725af6 | 3176 | _debug "ng_entry" "$ng_entry" |
3177 | _nd=$(echo "$ng_entry" | cut -d "$sep" -f 1) | |
3178 | _ngconf=$(echo "$ng_entry" | cut -d "$sep" -f 2) | |
3179 | _ngbackupconf=$(echo "$ng_entry" | cut -d "$sep" -f 3) | |
3180 | _info "Restoring from $_ngbackupconf to $_ngconf" | |
302c41ed | 3181 | cat "$_ngbackupconf" >"$_ngconf" |
9d725af6 | 3182 | done |
3183 | ||
3184 | _info "Reload nginx" | |
3185 | if ! _exec "nginx -s reload" >/dev/null; then | |
3186 | _exec_err | |
3187 | _err "It seems that nginx reload error, please report bug." | |
3188 | return 1 | |
3189 | fi | |
3190 | return 0 | |
3191 | } | |
3192 | ||
5ef501c5 | 3193 | _clearup() { |
44edb2bd | 3194 | _stopserver "$serverproc" |
4c3b3608 | 3195 | serverproc="" |
3196 | _restoreApache | |
9d725af6 | 3197 | _restoreNginx |
800e3f45 | 3198 | _clearupdns |
4c2a3841 | 3199 | if [ -z "$DEBUG" ]; then |
e22bcf7c | 3200 | rm -f "$TLS_CONF" |
3201 | rm -f "$TLS_CERT" | |
3202 | rm -f "$TLS_KEY" | |
3203 | rm -f "$TLS_CSR" | |
3204 | fi | |
4c3b3608 | 3205 | } |
3206 | ||
800e3f45 | 3207 | _clearupdns() { |
3208 | _debug "_clearupdns" | |
b5ca9bba | 3209 | _debug "dns_entries" "$dns_entries" |
3210 | ||
3211 | if [ -z "$dns_entries" ]; then | |
65b22b49 | 3212 | _debug "skip dns." |
800e3f45 | 3213 | return |
3214 | fi | |
875625b1 | 3215 | _info "Removing DNS records." |
800e3f45 | 3216 | |
b5ca9bba | 3217 | for entry in $dns_entries; do |
3218 | d=$(_getfield "$entry" 1) | |
3219 | txtdomain=$(_getfield "$entry" 2) | |
3220 | aliasDomain=$(_getfield "$entry" 3) | |
9a733a57 | 3221 | _currentRoot=$(_getfield "$entry" 4) |
b5ca9bba | 3222 | txt=$(_getfield "$entry" 5) |
3223 | d_api=$(_getfield "$entry" 6) | |
3224 | _debug "d" "$d" | |
3225 | _debug "txtdomain" "$txtdomain" | |
3226 | _debug "aliasDomain" "$aliasDomain" | |
9a733a57 | 3227 | _debug "_currentRoot" "$_currentRoot" |
b5ca9bba | 3228 | _debug "txt" "$txt" |
3229 | _debug "d_api" "$d_api" | |
3230 | if [ "$d_api" = "$txt" ]; then | |
3231 | d_api="" | |
800e3f45 | 3232 | fi |
4c2a3841 | 3233 | |
4c2a3841 | 3234 | if [ -z "$d_api" ]; then |
800e3f45 | 3235 | _info "Not Found domain api file: $d_api" |
3236 | continue | |
3237 | fi | |
4c2a3841 | 3238 | |
b5ca9bba | 3239 | if [ "$aliasDomain" ]; then |
3240 | txtdomain="$aliasDomain" | |
3241 | fi | |
3242 | ||
800e3f45 | 3243 | ( |
d5ec5f80 | 3244 | if ! . "$d_api"; then |
800e3f45 | 3245 | _err "Load file $d_api error. Please check your api file and try again." |
3246 | return 1 | |
3247 | fi | |
4c2a3841 | 3248 | |
800e3f45 | 3249 | rmcommand="${_currentRoot}_rm" |
d5ec5f80 | 3250 | if ! _exists "$rmcommand"; then |
800e3f45 | 3251 | _err "It seems that your api file doesn't define $rmcommand" |
3252 | return 1 | |
3253 | fi | |
a180b95c | 3254 | _info "Removing txt: $txt for domain: $txtdomain" |
21f201e3 | 3255 | if ! $rmcommand "$txtdomain" "$txt"; then |
800e3f45 | 3256 | _err "Error removing txt for domain:$txtdomain" |
3257 | return 1 | |
3258 | fi | |
a180b95c | 3259 | _info "Removed: Success" |
800e3f45 | 3260 | ) |
4c2a3841 | 3261 | |
800e3f45 | 3262 | done |
3263 | } | |
3264 | ||
4c3b3608 | 3265 | # webroot removelevel tokenfile |
3266 | _clearupwebbroot() { | |
3267 | __webroot="$1" | |
4c2a3841 | 3268 | if [ -z "$__webroot" ]; then |
4c3b3608 | 3269 | _debug "no webroot specified, skip" |
3270 | return 0 | |
3271 | fi | |
4c2a3841 | 3272 | |
dcf9cb58 | 3273 | _rmpath="" |
4c2a3841 | 3274 | if [ "$2" = '1' ]; then |
dcf9cb58 | 3275 | _rmpath="$__webroot/.well-known" |
4c2a3841 | 3276 | elif [ "$2" = '2' ]; then |
dcf9cb58 | 3277 | _rmpath="$__webroot/.well-known/acme-challenge" |
4c2a3841 | 3278 | elif [ "$2" = '3' ]; then |
dcf9cb58 | 3279 | _rmpath="$__webroot/.well-known/acme-challenge/$3" |
4c3b3608 | 3280 | else |
cc179731 | 3281 | _debug "Skip for removelevel:$2" |
4c3b3608 | 3282 | fi |
4c2a3841 | 3283 | |
3284 | if [ "$_rmpath" ]; then | |
3285 | if [ "$DEBUG" ]; then | |
dcf9cb58 | 3286 | _debug "Debugging, skip removing: $_rmpath" |
3287 | else | |
3288 | rm -rf "$_rmpath" | |
3289 | fi | |
3290 | fi | |
4c2a3841 | 3291 | |
4c3b3608 | 3292 | return 0 |
3293 | ||
3294 | } | |
3295 | ||
b0070f03 | 3296 | _on_before_issue() { |
af1cc3b3 | 3297 | _chk_web_roots="$1" |
02140ce7 | 3298 | _chk_main_domain="$2" |
3299 | _chk_alt_domains="$3" | |
85e1f4ea | 3300 | _chk_pre_hook="$4" |
3301 | _chk_local_addr="$5" | |
30c2d84c | 3302 | _debug _on_before_issue |
38f1b4d2 | 3303 | _debug _chk_main_domain "$_chk_main_domain" |
3304 | _debug _chk_alt_domains "$_chk_alt_domains" | |
d0f7c309 | 3305 | #run pre hook |
85e1f4ea | 3306 | if [ "$_chk_pre_hook" ]; then |
3307 | _info "Run pre hook:'$_chk_pre_hook'" | |
d0f7c309 | 3308 | if ! ( |
85e1f4ea | 3309 | cd "$DOMAIN_PATH" && eval "$_chk_pre_hook" |
d0f7c309 | 3310 | ); then |
3311 | _err "Error when run pre hook." | |
3312 | return 1 | |
3313 | fi | |
3314 | fi | |
3315 | ||
af1cc3b3 | 3316 | if _hasfield "$_chk_web_roots" "$NO_VALUE"; then |
3794b5cb | 3317 | if ! _exists "socat"; then |
3318 | _err "Please install socat tools first." | |
0463b5d6 | 3319 | return 1 |
3320 | fi | |
0463b5d6 | 3321 | fi |
3322 | ||
85e1f4ea | 3323 | _debug Le_LocalAddress "$_chk_local_addr" |
4c2a3841 | 3324 | |
0463b5d6 | 3325 | _index=1 |
3326 | _currentRoot="" | |
3327 | _addrIndex=1 | |
38f1b4d2 | 3328 | _w_index=1 |
931d19ee | 3329 | while true; do |
931d19ee | 3330 | d="$(echo "$_chk_main_domain,$_chk_alt_domains," | cut -d , -f "$_w_index")" |
3331 | _w_index="$(_math "$_w_index" + 1)" | |
3332 | _debug d "$d" | |
3333 | if [ -z "$d" ]; then | |
3334 | break | |
3335 | fi | |
d5ec5f80 | 3336 | _debug "Check for domain" "$d" |
af1cc3b3 | 3337 | _currentRoot="$(_getfield "$_chk_web_roots" $_index)" |
0463b5d6 | 3338 | _debug "_currentRoot" "$_currentRoot" |
3339 | _index=$(_math $_index + 1) | |
3340 | _checkport="" | |
4c2a3841 | 3341 | if [ "$_currentRoot" = "$NO_VALUE" ]; then |
0463b5d6 | 3342 | _info "Standalone mode." |
4c2a3841 | 3343 | if [ -z "$Le_HTTPPort" ]; then |
0463b5d6 | 3344 | Le_HTTPPort=80 |
e7f7e96d | 3345 | _cleardomainconf "Le_HTTPPort" |
0463b5d6 | 3346 | else |
4c2a3841 | 3347 | _savedomainconf "Le_HTTPPort" "$Le_HTTPPort" |
0463b5d6 | 3348 | fi |
3349 | _checkport="$Le_HTTPPort" | |
43ff787b | 3350 | elif [ "$_currentRoot" = "$W_ALPN" ]; then |
3351 | _info "Standalone alpn mode." | |
4c2a3841 | 3352 | if [ -z "$Le_TLSPort" ]; then |
0463b5d6 | 3353 | Le_TLSPort=443 |
3354 | else | |
4c2a3841 | 3355 | _savedomainconf "Le_TLSPort" "$Le_TLSPort" |
0463b5d6 | 3356 | fi |
3357 | _checkport="$Le_TLSPort" | |
3358 | fi | |
4c2a3841 | 3359 | |
3360 | if [ "$_checkport" ]; then | |
0463b5d6 | 3361 | _debug _checkport "$_checkport" |
85e1f4ea | 3362 | _checkaddr="$(_getfield "$_chk_local_addr" $_addrIndex)" |
0463b5d6 | 3363 | _debug _checkaddr "$_checkaddr" |
4c2a3841 | 3364 | |
0463b5d6 | 3365 | _addrIndex="$(_math $_addrIndex + 1)" |
4c2a3841 | 3366 | |
0463b5d6 | 3367 | _netprc="$(_ss "$_checkport" | grep "$_checkport")" |
3368 | netprc="$(echo "$_netprc" | grep "$_checkaddr")" | |
4c2a3841 | 3369 | if [ -z "$netprc" ]; then |
0463b5d6 | 3370 | netprc="$(echo "$_netprc" | grep "$LOCAL_ANY_ADDRESS")" |
3371 | fi | |
4c2a3841 | 3372 | if [ "$netprc" ]; then |
0463b5d6 | 3373 | _err "$netprc" |
4c2a3841 | 3374 | _err "tcp port $_checkport is already used by $(echo "$netprc" | cut -d : -f 4)" |
0463b5d6 | 3375 | _err "Please stop it first" |
3376 | return 1 | |
3377 | fi | |
3378 | fi | |
3379 | done | |
3380 | ||
af1cc3b3 | 3381 | if _hasfield "$_chk_web_roots" "apache"; then |
4c2a3841 | 3382 | if ! _setApache; then |
0463b5d6 | 3383 | _err "set up apache error. Report error to me." |
3384 | return 1 | |
3385 | fi | |
3386 | else | |
3387 | usingApache="" | |
3388 | fi | |
3389 | ||
b0070f03 | 3390 | } |
3391 | ||
3392 | _on_issue_err() { | |
85e1f4ea | 3393 | _chk_post_hook="$1" |
58e4d337 | 3394 | _chk_vlist="$2" |
30c2d84c | 3395 | _debug _on_issue_err |
cd8fc359 | 3396 | |
4c2a3841 | 3397 | if [ "$LOG_FILE" ]; then |
a73c5b33 | 3398 | _err "Please check log file for more details: $LOG_FILE" |
3399 | else | |
54ae008d | 3400 | _err "Please add '--debug' or '--log' to check more details." |
a73c5b33 | 3401 | _err "See: $_DEBUG_WIKI" |
3402 | fi | |
4c2a3841 | 3403 | |
b0070f03 | 3404 | #run the post hook |
85e1f4ea | 3405 | if [ "$_chk_post_hook" ]; then |
3406 | _info "Run post hook:'$_chk_post_hook'" | |
b0070f03 | 3407 | if ! ( |
85e1f4ea | 3408 | cd "$DOMAIN_PATH" && eval "$_chk_post_hook" |
4c2a3841 | 3409 | ); then |
b0070f03 | 3410 | _err "Error when run post hook." |
3411 | return 1 | |
3412 | fi | |
3413 | fi | |
58e4d337 | 3414 | |
3415 | #trigger the validation to flush the pending authz | |
ea722da3 | 3416 | _debug2 "_chk_vlist" "$_chk_vlist" |
58e4d337 | 3417 | if [ "$_chk_vlist" ]; then |
3418 | ( | |
c719a61e | 3419 | _debug2 "start to deactivate authz" |
3420 | ventries=$(echo "$_chk_vlist" | tr "$dvsep" ' ') | |
3421 | for ventry in $ventries; do | |
3422 | d=$(echo "$ventry" | cut -d "$sep" -f 1) | |
3423 | keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2) | |
3424 | uri=$(echo "$ventry" | cut -d "$sep" -f 3) | |
3425 | vtype=$(echo "$ventry" | cut -d "$sep" -f 4) | |
3426 | _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5) | |
f94433e5 | 3427 | __trigger_validation "$uri" "$keyauthorization" |
c719a61e | 3428 | done |
58e4d337 | 3429 | ) |
3430 | fi | |
3431 | ||
bd04638d | 3432 | if [ "$_ACME_IS_RENEW" = "1" ] && _hasfield "$Le_Webroot" "$W_DNS"; then |
309bec47 | 3433 | _err "$_DNS_MANUAL_ERR" |
3434 | fi | |
3435 | ||
58e4d337 | 3436 | if [ "$DEBUG" ] && [ "$DEBUG" -gt "0" ]; then |
3437 | _debug "$(_dlg_versions)" | |
3438 | fi | |
3439 | ||
b0070f03 | 3440 | } |
3441 | ||
3442 | _on_issue_success() { | |
85e1f4ea | 3443 | _chk_post_hook="$1" |
3444 | _chk_renew_hook="$2" | |
30c2d84c | 3445 | _debug _on_issue_success |
c6b68551 | 3446 | |
b0070f03 | 3447 | #run the post hook |
85e1f4ea | 3448 | if [ "$_chk_post_hook" ]; then |
3449 | _info "Run post hook:'$_chk_post_hook'" | |
b0070f03 | 3450 | if ! ( |
c7849a43 | 3451 | export CERT_PATH |
3452 | export CERT_KEY_PATH | |
3453 | export CA_CERT_PATH | |
3454 | export CERT_FULLCHAIN_PATH | |
3455 | export Le_Domain="$_main_domain" | |
85e1f4ea | 3456 | cd "$DOMAIN_PATH" && eval "$_chk_post_hook" |
4c2a3841 | 3457 | ); then |
b0070f03 | 3458 | _err "Error when run post hook." |
3459 | return 1 | |
3460 | fi | |
3461 | fi | |
4c2a3841 | 3462 | |
b0070f03 | 3463 | #run renew hook |
bd04638d | 3464 | if [ "$_ACME_IS_RENEW" ] && [ "$_chk_renew_hook" ]; then |
85e1f4ea | 3465 | _info "Run renew hook:'$_chk_renew_hook'" |
b0070f03 | 3466 | if ! ( |
c7849a43 | 3467 | export CERT_PATH |
3468 | export CERT_KEY_PATH | |
3469 | export CA_CERT_PATH | |
3470 | export CERT_FULLCHAIN_PATH | |
3471 | export Le_Domain="$_main_domain" | |
85e1f4ea | 3472 | cd "$DOMAIN_PATH" && eval "$_chk_renew_hook" |
4c2a3841 | 3473 | ); then |
b0070f03 | 3474 | _err "Error when run renew hook." |
3475 | return 1 | |
3476 | fi | |
4c2a3841 | 3477 | fi |
3478 | ||
867ec010 | 3479 | if _hasfield "$Le_Webroot" "$W_DNS" && [ -z "$FORCE_DNS_MANUAL" ]; then |
309bec47 | 3480 | _err "$_DNS_MANUAL_WARN" |
3481 | fi | |
3482 | ||
b0070f03 | 3483 | } |
3484 | ||
f96d91cb | 3485 | #account_key_length eab-kid eab-hmac-key |
eb59817e | 3486 | registeraccount() { |
f96d91cb | 3487 | _account_key_length="$1" |
3488 | _eab_id="$2" | |
3489 | _eab_hmac_key="$3" | |
eb59817e | 3490 | _initpath |
f96d91cb | 3491 | _regAccount "$_account_key_length" "$_eab_id" "$_eab_hmac_key" |
eb59817e | 3492 | } |
d404e92d | 3493 | |
8a29fbc8 | 3494 | __calcAccountKeyHash() { |
ca7202eb | 3495 | [ -f "$ACCOUNT_KEY_PATH" ] && _digest sha256 <"$ACCOUNT_KEY_PATH" |
8a29fbc8 | 3496 | } |
3497 | ||
339a8ad6 | 3498 | __calc_account_thumbprint() { |
3499 | printf "%s" "$jwk" | tr -d ' ' | _digest "sha256" | _url_replace | |
3500 | } | |
3501 | ||
389518e1 | 3502 | _getAccountEmail() { |
3503 | if [ "$ACCOUNT_EMAIL" ]; then | |
3504 | echo "$ACCOUNT_EMAIL" | |
3505 | return 0 | |
3506 | fi | |
3507 | if [ -z "$CA_EMAIL" ]; then | |
3508 | CA_EMAIL="$(_readcaconf CA_EMAIL)" | |
3509 | fi | |
3510 | if [ "$CA_EMAIL" ]; then | |
3511 | echo "$CA_EMAIL" | |
3512 | return 0 | |
3513 | fi | |
3514 | _readaccountconf "ACCOUNT_EMAIL" | |
3515 | } | |
3516 | ||
57e58ce7 | 3517 | #keylength |
d404e92d | 3518 | _regAccount() { |
3519 | _initpath | |
57e58ce7 | 3520 | _reg_length="$1" |
f96d91cb | 3521 | _eab_id="$2" |
3522 | _eab_hmac_key="$3" | |
f87890cb | 3523 | _debug3 _regAccount "$_regAccount" |
c1151b0d | 3524 | _initAPI |
3525 | ||
1bbc33a0 | 3526 | mkdir -p "$CA_DIR" |
5c48e139 | 3527 | if [ ! -f "$ACCOUNT_KEY_PATH" ] && [ -f "$_OLD_ACCOUNT_KEY" ]; then |
3528 | _info "mv $_OLD_ACCOUNT_KEY to $ACCOUNT_KEY_PATH" | |
3529 | mv "$_OLD_ACCOUNT_KEY" "$ACCOUNT_KEY_PATH" | |
3530 | fi | |
4c2a3841 | 3531 | |
5c48e139 | 3532 | if [ ! -f "$ACCOUNT_JSON_PATH" ] && [ -f "$_OLD_ACCOUNT_JSON" ]; then |
3533 | _info "mv $_OLD_ACCOUNT_JSON to $ACCOUNT_JSON_PATH" | |
3534 | mv "$_OLD_ACCOUNT_JSON" "$ACCOUNT_JSON_PATH" | |
3535 | fi | |
4c2a3841 | 3536 | |
3537 | if [ ! -f "$ACCOUNT_KEY_PATH" ]; then | |
3538 | if ! _create_account_key "$_reg_length"; then | |
d404e92d | 3539 | _err "Create account key error." |
3540 | return 1 | |
3541 | fi | |
3542 | fi | |
4c2a3841 | 3543 | |
3544 | if ! _calcjwk "$ACCOUNT_KEY_PATH"; then | |
d404e92d | 3545 | return 1 |
3546 | fi | |
f96d91cb | 3547 | if [ "$_eab_id" ] && [ "$_eab_hmac_key" ]; then |
3548 | _savecaconf CA_EAB_KEY_ID "$_eab_id" | |
3549 | _savecaconf CA_EAB_HMAC_KEY "$_eab_hmac_key" | |
3550 | fi | |
3551 | _eab_id=$(_readcaconf "CA_EAB_KEY_ID") | |
3552 | _eab_hmac_key=$(_readcaconf "CA_EAB_HMAC_KEY") | |
3553 | _secure_debug3 _eab_id "$_eab_id" | |
3554 | _secure_debug3 _eab_hmac_key "$_eab_hmac_key" | |
389518e1 | 3555 | _email="$(_getAccountEmail)" |
3556 | if [ "$_email" ]; then | |
3557 | _savecaconf "CA_EMAIL" "$_email" | |
3558 | fi | |
c1151b0d | 3559 | if [ "$ACME_VERSION" = "2" ]; then |
389518e1 | 3560 | if [ "$ACME_DIRECTORY" = "$CA_ZEROSSL" ]; then |
3561 | if [ -z "$_eab_id" ] || [ -z "$_eab_hmac_key" ]; then | |
3562 | _info "No EAB credentials found for ZeroSSL, let's get one" | |
3563 | if [ -z "$_email" ]; then | |
578c338d | 3564 | _err "Please provide a email address for ZeroSSL account." |
3565 | _err "See ZeroSSL usage: $_ZEROSSL_WIKI" | |
389518e1 | 3566 | return 1 |
3567 | fi | |
3568 | _eabresp=$(_post "email=$_email" $_ZERO_EAB_ENDPOINT) | |
3569 | if [ "$?" != "0" ]; then | |
3570 | _debug2 "$_eabresp" | |
578c338d | 3571 | _err "Can not get EAB credentials from ZeroSSL." |
389518e1 | 3572 | return 1 |
3573 | fi | |
3574 | _eab_id="$(echo "$_eabresp" | tr ',}' '\n' | grep '"eab_kid"' | cut -d : -f 2 | tr -d '"')" | |
3575 | if [ -z "$_eab_id" ]; then | |
365aa69a | 3576 | _err "Can not resolve _eab_id" |
389518e1 | 3577 | return 1 |
3578 | fi | |
3579 | _eab_hmac_key="$(echo "$_eabresp" | tr ',}' '\n' | grep '"eab_hmac_key"' | cut -d : -f 2 | tr -d '"')" | |
3580 | if [ -z "$_eab_hmac_key" ]; then | |
365aa69a | 3581 | _err "Can not resolve _eab_hmac_key" |
389518e1 | 3582 | return 1 |
3583 | fi | |
3584 | _savecaconf CA_EAB_KEY_ID "$_eab_id" | |
3585 | _savecaconf CA_EAB_HMAC_KEY "$_eab_hmac_key" | |
3586 | fi | |
3587 | fi | |
f96d91cb | 3588 | if [ "$_eab_id" ] && [ "$_eab_hmac_key" ]; then |
3589 | eab_protected="{\"alg\":\"HS256\",\"kid\":\"$_eab_id\",\"url\":\"${ACME_NEW_ACCOUNT}\"}" | |
3590 | _debug3 eab_protected "$eab_protected" | |
3591 | ||
3592 | eab_protected64=$(printf "%s" "$eab_protected" | _base64 | _url_replace) | |
3593 | _debug3 eab_protected64 "$eab_protected64" | |
3594 | ||
3595 | eab_payload64=$(printf "%s" "$jwk" | _base64 | _url_replace) | |
3596 | _debug3 eab_payload64 "$eab_payload64" | |
3597 | ||
3598 | eab_sign_t="$eab_protected64.$eab_payload64" | |
3599 | _debug3 eab_sign_t "$eab_sign_t" | |
3600 | ||
d42ff227 | 3601 | key_hex="$(_durl_replace_base64 "$_eab_hmac_key" | _dbase64 | _hex_dump | tr -d ' ')" |
f96d91cb | 3602 | _debug3 key_hex "$key_hex" |
3603 | ||
d42ff227 | 3604 | eab_signature=$(printf "%s" "$eab_sign_t" | _hmac sha256 $key_hex | _base64 | _url_replace) |
f96d91cb | 3605 | _debug3 eab_signature "$eab_signature" |
3606 | ||
3607 | externalBinding=",\"externalAccountBinding\":{\"protected\":\"$eab_protected64\", \"payload\":\"$eab_payload64\", \"signature\":\"$eab_signature\"}" | |
3608 | _debug3 externalBinding "$externalBinding" | |
3609 | fi | |
389518e1 | 3610 | if [ "$_email" ]; then |
3611 | email_sg="\"contact\": [\"mailto:$_email\"], " | |
c1151b0d | 3612 | fi |
f96d91cb | 3613 | regjson="{$email_sg\"termsOfServiceAgreed\": true$externalBinding}" |
c1151b0d | 3614 | else |
3615 | _reg_res="$ACME_NEW_ACCOUNT_RES" | |
3616 | regjson='{"resource": "'$_reg_res'", "terms-of-service-agreed": true, "agreement": "'$ACME_AGREEMENT'"}' | |
389518e1 | 3617 | if [ "$_email" ]; then |
3618 | regjson='{"resource": "'$_reg_res'", "contact": ["mailto:'$_email'"], "terms-of-service-agreed": true, "agreement": "'$ACME_AGREEMENT'"}' | |
c1151b0d | 3619 | fi |
f87890cb | 3620 | fi |
4c2a3841 | 3621 | |
389518e1 | 3622 | _info "Registering account: $ACME_DIRECTORY" |
d404e92d | 3623 | |
f87890cb | 3624 | if ! _send_signed_request "${ACME_NEW_ACCOUNT}" "$regjson"; then |
3625 | _err "Register account Error: $response" | |
3626 | return 1 | |
3627 | fi | |
d404e92d | 3628 | |
389518e1 | 3629 | _eabAlreadyBound="" |
f87890cb | 3630 | if [ "$code" = "" ] || [ "$code" = '201' ]; then |
3631 | echo "$response" >"$ACCOUNT_JSON_PATH" | |
3632 | _info "Registered" | |
7df20e50 | 3633 | elif [ "$code" = '409' ] || [ "$code" = '200' ]; then |
f87890cb | 3634 | _info "Already registered" |
389518e1 | 3635 | elif [ "$code" = '400' ] && _contains "$response" 'The account is not awaiting external account binding'; then |
3636 | _info "Already register EAB." | |
3637 | _eabAlreadyBound=1 | |
f87890cb | 3638 | else |
3639 | _err "Register account Error: $response" | |
3640 | return 1 | |
3641 | fi | |
d404e92d | 3642 | |
389518e1 | 3643 | if [ -z "$_eabAlreadyBound" ]; then |
3644 | _debug2 responseHeaders "$responseHeaders" | |
3645 | _accUri="$(echo "$responseHeaders" | grep -i "^Location:" | _head_n 1 | cut -d ':' -f 2- | tr -d "\r\n ")" | |
3646 | _debug "_accUri" "$_accUri" | |
3647 | if [ -z "$_accUri" ]; then | |
3648 | _err "Can not find account id url." | |
3649 | _err "$responseHeaders" | |
3650 | return 1 | |
3651 | fi | |
3652 | _savecaconf "ACCOUNT_URL" "$_accUri" | |
3653 | else | |
3654 | ACCOUNT_URL="$(_readcaconf ACCOUNT_URL)" | |
7e0b334b | 3655 | fi |
7e0b334b | 3656 | export ACCOUNT_URL="$_accUri" |
d404e92d | 3657 | |
f87890cb | 3658 | CA_KEY_HASH="$(__calcAccountKeyHash)" |
3659 | _debug "Calc CA_KEY_HASH" "$CA_KEY_HASH" | |
3660 | _savecaconf CA_KEY_HASH "$CA_KEY_HASH" | |
d404e92d | 3661 | |
f87890cb | 3662 | if [ "$code" = '403' ]; then |
3663 | _err "It seems that the account key is already deactivated, please use a new account key." | |
3664 | return 1 | |
3665 | fi | |
4c2a3841 | 3666 | |
f87890cb | 3667 | ACCOUNT_THUMBPRINT="$(__calc_account_thumbprint)" |
3668 | _info "ACCOUNT_THUMBPRINT" "$ACCOUNT_THUMBPRINT" | |
d404e92d | 3669 | } |
3670 | ||
79e2f8a2 | 3671 | #implement updateaccount |
3672 | updateaccount() { | |
3673 | _initpath | |
3674 | ||
3675 | if [ ! -f "$ACCOUNT_KEY_PATH" ] && [ -f "$_OLD_ACCOUNT_KEY" ]; then | |
3676 | _info "mv $_OLD_ACCOUNT_KEY to $ACCOUNT_KEY_PATH" | |
3677 | mv "$_OLD_ACCOUNT_KEY" "$ACCOUNT_KEY_PATH" | |
3678 | fi | |
3679 | ||
3680 | if [ ! -f "$ACCOUNT_JSON_PATH" ] && [ -f "$_OLD_ACCOUNT_JSON" ]; then | |
3681 | _info "mv $_OLD_ACCOUNT_JSON to $ACCOUNT_JSON_PATH" | |
3682 | mv "$_OLD_ACCOUNT_JSON" "$ACCOUNT_JSON_PATH" | |
3683 | fi | |
3684 | ||
3685 | if [ ! -f "$ACCOUNT_KEY_PATH" ]; then | |
3686 | _err "Account key is not found at: $ACCOUNT_KEY_PATH" | |
3687 | return 1 | |
3688 | fi | |
3689 | ||
3690 | _accUri=$(_readcaconf "ACCOUNT_URL") | |
3691 | _debug _accUri "$_accUri" | |
3692 | ||
3693 | if [ -z "$_accUri" ]; then | |
3694 | _err "The account url is empty, please run '--update-account' first to update the account info first," | |
3695 | _err "Then try again." | |
3696 | return 1 | |
3697 | fi | |
3698 | ||
3699 | if ! _calcjwk "$ACCOUNT_KEY_PATH"; then | |
3700 | return 1 | |
3701 | fi | |
3702 | _initAPI | |
3703 | ||
389518e1 | 3704 | _email="$(_getAccountEmail)" |
79e2f8a2 | 3705 | if [ "$ACME_VERSION" = "2" ]; then |
3706 | if [ "$ACCOUNT_EMAIL" ]; then | |
389518e1 | 3707 | updjson='{"contact": ["mailto:'$_email'"]}' |
2febdfc3 SW |
3708 | else |
3709 | updjson='{"contact": []}' | |
79e2f8a2 | 3710 | fi |
3711 | else | |
3712 | # ACMEv1: Updates happen the same way a registration is done. | |
3713 | # https://tools.ietf.org/html/draft-ietf-acme-acme-01#section-6.3 | |
3714 | _regAccount | |
3715 | return | |
3716 | fi | |
3717 | ||
3718 | # this part handles ACMEv2 account updates. | |
3719 | _send_signed_request "$_accUri" "$updjson" | |
3720 | ||
3721 | if [ "$code" = '200' ]; then | |
72e1a1b2 | 3722 | echo "$response" >"$ACCOUNT_JSON_PATH" |
79e2f8a2 | 3723 | _info "account update success for $_accUri." |
3724 | else | |
3725 | _info "Error. The account was not updated." | |
3726 | return 1 | |
3727 | fi | |
3728 | } | |
3729 | ||
422dd1fa | 3730 | #Implement deactivate account |
3731 | deactivateaccount() { | |
3732 | _initpath | |
3733 | ||
3734 | if [ ! -f "$ACCOUNT_KEY_PATH" ] && [ -f "$_OLD_ACCOUNT_KEY" ]; then | |
422dd1fa | 3735 | _info "mv $_OLD_ACCOUNT_KEY to $ACCOUNT_KEY_PATH" |
3736 | mv "$_OLD_ACCOUNT_KEY" "$ACCOUNT_KEY_PATH" | |
3737 | fi | |
3738 | ||
3739 | if [ ! -f "$ACCOUNT_JSON_PATH" ] && [ -f "$_OLD_ACCOUNT_JSON" ]; then | |
422dd1fa | 3740 | _info "mv $_OLD_ACCOUNT_JSON to $ACCOUNT_JSON_PATH" |
3741 | mv "$_OLD_ACCOUNT_JSON" "$ACCOUNT_JSON_PATH" | |
3742 | fi | |
3743 | ||
3744 | if [ ! -f "$ACCOUNT_KEY_PATH" ]; then | |
3745 | _err "Account key is not found at: $ACCOUNT_KEY_PATH" | |
3746 | return 1 | |
3747 | fi | |
3748 | ||
3749 | _accUri=$(_readcaconf "ACCOUNT_URL") | |
3750 | _debug _accUri "$_accUri" | |
3751 | ||
3752 | if [ -z "$_accUri" ]; then | |
3753 | _err "The account url is empty, please run '--update-account' first to update the account info first," | |
3754 | _err "Then try again." | |
3755 | return 1 | |
3756 | fi | |
3757 | ||
3758 | if ! _calcjwk "$ACCOUNT_KEY_PATH"; then | |
3759 | return 1 | |
3760 | fi | |
3761 | _initAPI | |
3762 | ||
d2cde379 | 3763 | if [ "$ACME_VERSION" = "2" ]; then |
3764 | _djson="{\"status\":\"deactivated\"}" | |
3765 | else | |
3766 | _djson="{\"resource\": \"reg\", \"status\":\"deactivated\"}" | |
3767 | fi | |
3768 | if _send_signed_request "$_accUri" "$_djson" && _contains "$response" '"deactivated"'; then | |
422dd1fa | 3769 | _info "Deactivate account success for $_accUri." |
3770 | _accid=$(echo "$response" | _egrep_o "\"id\" *: *[^,]*," | cut -d : -f 2 | tr -d ' ,') | |
3771 | elif [ "$code" = "403" ]; then | |
3772 | _info "The account is already deactivated." | |
3773 | _accid=$(_getfield "$_accUri" "999" "/") | |
3774 | else | |
3775 | _err "Deactivate: account failed for $_accUri." | |
3776 | return 1 | |
3777 | fi | |
3778 | ||
3779 | _debug "Account id: $_accid" | |
3780 | if [ "$_accid" ]; then | |
3781 | _deactivated_account_path="$CA_DIR/deactivated/$_accid" | |
3782 | _debug _deactivated_account_path "$_deactivated_account_path" | |
3783 | if mkdir -p "$_deactivated_account_path"; then | |
3784 | _info "Moving deactivated account info to $_deactivated_account_path/" | |
3785 | mv "$CA_CONF" "$_deactivated_account_path/" | |
3786 | mv "$ACCOUNT_JSON_PATH" "$_deactivated_account_path/" | |
3787 | mv "$ACCOUNT_KEY_PATH" "$_deactivated_account_path/" | |
3788 | else | |
3789 | _err "Can not create dir: $_deactivated_account_path, try to remove the deactivated account key." | |
3790 | rm -f "$CA_CONF" | |
3791 | rm -f "$ACCOUNT_JSON_PATH" | |
3792 | rm -f "$ACCOUNT_KEY_PATH" | |
3793 | fi | |
3794 | fi | |
3795 | } | |
3796 | ||
a61fe418 | 3797 | # domain folder file |
3798 | _findHook() { | |
3799 | _hookdomain="$1" | |
3800 | _hookcat="$2" | |
3801 | _hookname="$3" | |
3802 | ||
c7b16249 | 3803 | if [ -f "$_SCRIPT_HOME/$_hookcat/$_hookname" ]; then |
3804 | d_api="$_SCRIPT_HOME/$_hookcat/$_hookname" | |
3805 | elif [ -f "$_SCRIPT_HOME/$_hookcat/$_hookname.sh" ]; then | |
3806 | d_api="$_SCRIPT_HOME/$_hookcat/$_hookname.sh" | |
b50e701c | 3807 | elif [ "$_hookdomain" ] && [ -f "$LE_WORKING_DIR/$_hookdomain/$_hookname" ]; then |
a61fe418 | 3808 | d_api="$LE_WORKING_DIR/$_hookdomain/$_hookname" |
b50e701c | 3809 | elif [ "$_hookdomain" ] && [ -f "$LE_WORKING_DIR/$_hookdomain/$_hookname.sh" ]; then |
a61fe418 | 3810 | d_api="$LE_WORKING_DIR/$_hookdomain/$_hookname.sh" |
4c2a3841 | 3811 | elif [ -f "$LE_WORKING_DIR/$_hookname" ]; then |
a61fe418 | 3812 | d_api="$LE_WORKING_DIR/$_hookname" |
4c2a3841 | 3813 | elif [ -f "$LE_WORKING_DIR/$_hookname.sh" ]; then |
a61fe418 | 3814 | d_api="$LE_WORKING_DIR/$_hookname.sh" |
4c2a3841 | 3815 | elif [ -f "$LE_WORKING_DIR/$_hookcat/$_hookname" ]; then |
a61fe418 | 3816 | d_api="$LE_WORKING_DIR/$_hookcat/$_hookname" |
4c2a3841 | 3817 | elif [ -f "$LE_WORKING_DIR/$_hookcat/$_hookname.sh" ]; then |
a61fe418 | 3818 | d_api="$LE_WORKING_DIR/$_hookcat/$_hookname.sh" |
3819 | fi | |
3820 | ||
3821 | printf "%s" "$d_api" | |
3822 | } | |
3823 | ||
f940b2a5 | 3824 | #domain |
3825 | __get_domain_new_authz() { | |
3826 | _gdnd="$1" | |
3827 | _info "Getting new-authz for domain" "$_gdnd" | |
40ef86f4 | 3828 | _initAPI |
f940b2a5 | 3829 | _Max_new_authz_retry_times=5 |
3830 | _authz_i=0 | |
4c2a3841 | 3831 | while [ "$_authz_i" -lt "$_Max_new_authz_retry_times" ]; do |
efd96153 | 3832 | _debug "Try new-authz for the $_authz_i time." |
48d9a8c1 | 3833 | if ! _send_signed_request "${ACME_NEW_AUTHZ}" "{\"resource\": \"new-authz\", \"identifier\": {\"type\": \"dns\", \"value\": \"$(_idn "$_gdnd")\"}}"; then |
f940b2a5 | 3834 | _err "Can not get domain new authz." |
3835 | return 1 | |
3836 | fi | |
5413bf87 | 3837 | if _contains "$response" "No registration exists matching provided key"; then |
3838 | _err "It seems there is an error, but it's recovered now, please try again." | |
3839 | _err "If you see this message for a second time, please report bug: $(__green "$PROJECT")" | |
3840 | _clearcaconf "CA_KEY_HASH" | |
3841 | break | |
3842 | fi | |
4c2a3841 | 3843 | if ! _contains "$response" "An error occurred while processing your request"; then |
f940b2a5 | 3844 | _info "The new-authz request is ok." |
3845 | break | |
3846 | fi | |
3847 | _authz_i="$(_math "$_authz_i" + 1)" | |
9e45ac93 | 3848 | _info "The server is busy, Sleep $_authz_i to retry." |
f940b2a5 | 3849 | _sleep "$_authz_i" |
4c2a3841 | 3850 | done |
f940b2a5 | 3851 | |
4c2a3841 | 3852 | if [ "$_authz_i" = "$_Max_new_authz_retry_times" ]; then |
efd96153 | 3853 | _err "new-authz retry reach the max $_Max_new_authz_retry_times times." |
f940b2a5 | 3854 | fi |
4c2a3841 | 3855 | |
78915896 | 3856 | if [ "$code" ] && [ "$code" != '201' ]; then |
f940b2a5 | 3857 | _err "new-authz error: $response" |
3858 | return 1 | |
3859 | fi | |
3860 | ||
3861 | } | |
3862 | ||
58e4d337 | 3863 | #uri keyAuthorization |
f94433e5 | 3864 | __trigger_validation() { |
8bd12ed0 | 3865 | _debug2 "Trigger domain validation." |
58e4d337 | 3866 | _t_url="$1" |
3867 | _debug2 _t_url "$_t_url" | |
3868 | _t_key_authz="$2" | |
3869 | _debug2 _t_key_authz "$_t_key_authz" | |
920cab6f K |
3870 | _t_vtype="$3" |
3871 | _debug2 _t_vtype "$_t_vtype" | |
c1151b0d | 3872 | if [ "$ACME_VERSION" = "2" ]; then |
e7f7e96d | 3873 | _send_signed_request "$_t_url" "{}" |
c1151b0d | 3874 | else |
8bd12ed0 | 3875 | _send_signed_request "$_t_url" "{\"resource\": \"challenge\", \"type\": \"$_t_vtype\", \"keyAuthorization\": \"$_t_key_authz\"}" |
c1151b0d | 3876 | fi |
58e4d337 | 3877 | } |
3878 | ||
b5ca9bba | 3879 | #endpoint domain type |
10eec7d4 | 3880 | _ns_lookup_impl() { |
b5ca9bba | 3881 | _ns_ep="$1" |
3882 | _ns_domain="$2" | |
3883 | _ns_type="$3" | |
3884 | _debug2 "_ns_ep" "$_ns_ep" | |
3885 | _debug2 "_ns_domain" "$_ns_domain" | |
3886 | _debug2 "_ns_type" "$_ns_type" | |
3887 | ||
3888 | response="$(_H1="accept: application/dns-json" _get "$_ns_ep?name=$_ns_domain&type=$_ns_type")" | |
3889 | _ret=$? | |
3890 | _debug2 "response" "$response" | |
3891 | if [ "$_ret" != "0" ]; then | |
3892 | return $_ret | |
3893 | fi | |
3894 | _answers="$(echo "$response" | tr '{}' '<>' | _egrep_o '"Answer":\[[^]]*]' | tr '<>' '\n\n')" | |
3895 | _debug2 "_answers" "$_answers" | |
3896 | echo "$_answers" | |
3897 | } | |
3898 | ||
3899 | #domain, type | |
3900 | _ns_lookup_cf() { | |
3901 | _cf_ld="$1" | |
3902 | _cf_ld_type="$2" | |
3903 | _cf_ep="https://cloudflare-dns.com/dns-query" | |
10eec7d4 | 3904 | _ns_lookup_impl "$_cf_ep" "$_cf_ld" "$_cf_ld_type" |
b5ca9bba | 3905 | } |
3906 | ||
3907 | #domain, type | |
3908 | _ns_purge_cf() { | |
3909 | _cf_d="$1" | |
3910 | _cf_d_type="$2" | |
3911 | _debug "Cloudflare purge $_cf_d_type record for domain $_cf_d" | |
b9b2cd27 | 3912 | _cf_purl="https://cloudflare-dns.com/api/v1/purge?domain=$_cf_d&type=$_cf_d_type" |
b5ca9bba | 3913 | response="$(_post "" "$_cf_purl")" |
3914 | _debug2 response "$response" | |
3915 | } | |
3916 | ||
10eec7d4 | 3917 | #checks if cf server is available |
3918 | _ns_is_available_cf() { | |
1e7534b9 | 3919 | if _get "https://cloudflare-dns.com" >/dev/null 2>&1; then |
10eec7d4 | 3920 | return 0 |
3921 | else | |
3922 | return 1 | |
3923 | fi | |
3924 | } | |
3925 | ||
3926 | #domain, type | |
3927 | _ns_lookup_google() { | |
3928 | _cf_ld="$1" | |
3929 | _cf_ld_type="$2" | |
3930 | _cf_ep="https://dns.google/resolve" | |
3931 | _ns_lookup_impl "$_cf_ep" "$_cf_ld" "$_cf_ld_type" | |
3932 | } | |
3933 | ||
3934 | #domain, type | |
3935 | _ns_lookup() { | |
3936 | if [ -z "$DOH_USE" ]; then | |
3937 | _debug "Detect dns server first." | |
3938 | if _ns_is_available_cf; then | |
3939 | _debug "Use cloudflare doh server" | |
3940 | export DOH_USE=$DOH_CLOUDFLARE | |
3941 | else | |
3942 | _debug "Use google doh server" | |
3943 | export DOH_USE=$DOH_GOOGLE | |
3944 | fi | |
3945 | fi | |
3946 | ||
3947 | if [ "$DOH_USE" = "$DOH_CLOUDFLARE" ] || [ -z "$DOH_USE" ]; then | |
3948 | _ns_lookup_cf "$@" | |
3949 | else | |
3950 | _ns_lookup_google "$@" | |
3951 | fi | |
3952 | ||
3953 | } | |
3954 | ||
b5ca9bba | 3955 | #txtdomain, alias, txt |
3956 | __check_txt() { | |
3957 | _c_txtdomain="$1" | |
3958 | _c_aliasdomain="$2" | |
3959 | _c_txt="$3" | |
3960 | _debug "_c_txtdomain" "$_c_txtdomain" | |
3961 | _debug "_c_aliasdomain" "$_c_aliasdomain" | |
3962 | _debug "_c_txt" "$_c_txt" | |
10eec7d4 | 3963 | _answers="$(_ns_lookup "$_c_aliasdomain" TXT)" |
b5ca9bba | 3964 | _contains "$_answers" "$_c_txt" |
3965 | ||
3966 | } | |
3967 | ||
3968 | #txtdomain | |
3969 | __purge_txt() { | |
3970 | _p_txtdomain="$1" | |
3971 | _debug _p_txtdomain "$_p_txtdomain" | |
10eec7d4 | 3972 | if [ "$DOH_USE" = "$DOH_CLOUDFLARE" ] || [ -z "$DOH_USE" ]; then |
3973 | _ns_purge_cf "$_p_txtdomain" "TXT" | |
3974 | else | |
3975 | _debug "no purge api for google dns api, just sleep 5 secs" | |
3976 | _sleep 5 | |
3977 | fi | |
3978 | ||
b5ca9bba | 3979 | } |
3980 | ||
3981 | #wait and check each dns entries | |
3982 | _check_dns_entries() { | |
3983 | _success_txt="," | |
3984 | _end_time="$(_time)" | |
3985 | _end_time="$(_math "$_end_time" + 1200)" #let's check no more than 20 minutes. | |
3986 | ||
3987 | while [ "$(_time)" -le "$_end_time" ]; do | |
8a24275b | 3988 | _info "You can use '--dnssleep' to disable public dns checks." |
3989 | _info "See: $_DNSCHECK_WIKI" | |
b5ca9bba | 3990 | _left="" |
3991 | for entry in $dns_entries; do | |
3992 | d=$(_getfield "$entry" 1) | |
3993 | txtdomain=$(_getfield "$entry" 2) | |
0093dc3d | 3994 | txtdomain=$(_idn "$txtdomain") |
b5ca9bba | 3995 | aliasDomain=$(_getfield "$entry" 3) |
0093dc3d | 3996 | aliasDomain=$(_idn "$aliasDomain") |
b5ca9bba | 3997 | txt=$(_getfield "$entry" 5) |
3998 | d_api=$(_getfield "$entry" 6) | |
3999 | _debug "d" "$d" | |
4000 | _debug "txtdomain" "$txtdomain" | |
4001 | _debug "aliasDomain" "$aliasDomain" | |
4002 | _debug "txt" "$txt" | |
4003 | _debug "d_api" "$d_api" | |
4004 | _info "Checking $d for $aliasDomain" | |
4005 | if _contains "$_success_txt" ",$txt,"; then | |
4006 | _info "Already success, continue next one." | |
4007 | continue | |
4008 | fi | |
4009 | ||
4010 | if __check_txt "$txtdomain" "$aliasDomain" "$txt"; then | |
4011 | _info "Domain $d '$aliasDomain' success." | |
4012 | _success_txt="$_success_txt,$txt," | |
4013 | continue | |
4014 | fi | |
4015 | _left=1 | |
4016 | _info "Not valid yet, let's wait 10 seconds and check next one." | |
b5ca9bba | 4017 | __purge_txt "$txtdomain" |
4018 | if [ "$txtdomain" != "$aliasDomain" ]; then | |
4019 | __purge_txt "$aliasDomain" | |
4020 | fi | |
b9b2cd27 | 4021 | _sleep 10 |
b5ca9bba | 4022 | done |
4023 | if [ "$_left" ]; then | |
4024 | _info "Let's wait 10 seconds and check again". | |
4025 | _sleep 10 | |
4026 | else | |
4027 | _info "All success, let's return" | |
a44ea0dd | 4028 | return 0 |
b5ca9bba | 4029 | fi |
4030 | done | |
a44ea0dd | 4031 | _info "Timed out waiting for DNS." |
4032 | return 1 | |
b5ca9bba | 4033 | |
4034 | } | |
4035 | ||
e3ebd582 | 4036 | #file |
c384ed96 | 4037 | _get_chain_issuers() { |
e3ebd582 | 4038 | _cfile="$1" |
c384ed96 AK |
4039 | if _contains "$(${ACME_OPENSSL_BIN:-openssl} help crl2pkcs7 2>&1)" "Usage: crl2pkcs7" || _contains "$(${ACME_OPENSSL_BIN:-openssl} crl2pkcs7 -help 2>&1)" "Usage: crl2pkcs7" || _contains "$(${ACME_OPENSSL_BIN:-openssl} crl2pkcs7 help 2>&1)" "unknown option help"; then |
4040 | ${ACME_OPENSSL_BIN:-openssl} crl2pkcs7 -nocrl -certfile $_cfile | ${ACME_OPENSSL_BIN:-openssl} pkcs7 -print_certs -text -noout | grep -i 'Issuer:' | _egrep_o "CN *=[^,]*" | cut -d = -f 2 | |
d5d38b33 | 4041 | else |
c384ed96 AK |
4042 | _cindex=1 |
4043 | for _startn in $(grep -n -- "$BEGIN_CERT" "$_cfile" | cut -d : -f 1); do | |
4044 | _endn="$(grep -n -- "$END_CERT" "$_cfile" | cut -d : -f 1 | _head_n $_cindex | _tail_n 1)" | |
4045 | _debug2 "_startn" "$_startn" | |
4046 | _debug2 "_endn" "$_endn" | |
4047 | if [ "$DEBUG" ]; then | |
4048 | _debug2 "cert$_cindex" "$(sed -n "$_startn,${_endn}p" "$_cfile")" | |
4049 | fi | |
4050 | sed -n "$_startn,${_endn}p" "$_cfile" | ${ACME_OPENSSL_BIN:-openssl} x509 -text -noout | grep 'Issuer:' | _egrep_o "CN *=[^,]*" | cut -d = -f 2 | sed "s/ *\(.*\)/\1/" | |
4051 | _cindex=$(_math $_cindex + 1) | |
4052 | done | |
4053 | fi | |
4054 | } | |
4055 | ||
4056 | # | |
4057 | _get_chain_subjects() { | |
4058 | _cfile="$1" | |
4059 | if _contains "$(${ACME_OPENSSL_BIN:-openssl} help crl2pkcs7 2>&1)" "Usage: crl2pkcs7" || _contains "$(${ACME_OPENSSL_BIN:-openssl} crl2pkcs7 -help 2>&1)" "Usage: crl2pkcs7" || _contains "$(${ACME_OPENSSL_BIN:-openssl} crl2pkcs7 help 2>&1)" "unknown option help"; then | |
4060 | ${ACME_OPENSSL_BIN:-openssl} crl2pkcs7 -nocrl -certfile $_cfile | ${ACME_OPENSSL_BIN:-openssl} pkcs7 -print_certs -text -noout | grep -i 'Subject:' | _egrep_o "CN *=[^,]*" | cut -d = -f 2 | |
4061 | else | |
4062 | _cindex=1 | |
4063 | for _startn in $(grep -n -- "$BEGIN_CERT" "$_cfile" | cut -d : -f 1); do | |
4064 | _endn="$(grep -n -- "$END_CERT" "$_cfile" | cut -d : -f 1 | _head_n $_cindex | _tail_n 1)" | |
4065 | _debug2 "_startn" "$_startn" | |
4066 | _debug2 "_endn" "$_endn" | |
4067 | if [ "$DEBUG" ]; then | |
4068 | _debug2 "cert$_cindex" "$(sed -n "$_startn,${_endn}p" "$_cfile")" | |
4069 | fi | |
4070 | sed -n "$_startn,${_endn}p" "$_cfile" | ${ACME_OPENSSL_BIN:-openssl} x509 -text -noout | grep -i 'Subject:' | _egrep_o "CN *=[^,]*" | cut -d = -f 2 | sed "s/ *\(.*\)/\1/" | |
4071 | _cindex=$(_math $_cindex + 1) | |
4072 | done | |
d5d38b33 | 4073 | fi |
e3ebd582 | 4074 | } |
4075 | ||
4076 | #cert issuer | |
4077 | _match_issuer() { | |
4078 | _cfile="$1" | |
4079 | _missuer="$2" | |
c384ed96 | 4080 | _fissuers="$(_get_chain_issuers $_cfile)" |
d5d38b33 | 4081 | _debug2 _fissuers "$_fissuers" |
c384ed96 AK |
4082 | _rootissuer="$(echo "$_fissuers" | _lower_case | _tail_n 1)" |
4083 | _debug2 _rootissuer "$_rootissuer" | |
fdb96e91 | 4084 | _missuer="$(echo "$_missuer" | _lower_case)" |
c384ed96 | 4085 | _contains "$_rootissuer" "$_missuer" |
e3ebd582 | 4086 | } |
4087 | ||
3c07f57a | 4088 | #webroot, domain domainlist keylength |
4c3b3608 | 4089 | issue() { |
4c2a3841 | 4090 | if [ -z "$2" ]; then |
2e87e64b | 4091 | _usage "Usage: $PROJECT_ENTRY --issue --domain <domain.tld> --webroot <directory>" |
4c3b3608 | 4092 | return 1 |
4093 | fi | |
49d75a0c | 4094 | if [ -z "$1" ]; then |
4095 | _usage "Please specify at least one validation method: '--webroot', '--standalone', '--apache', '--nginx' or '--dns' etc." | |
4096 | return 1 | |
4097 | fi | |
af1cc3b3 | 4098 | _web_roots="$1" |
4099 | _main_domain="$2" | |
02140ce7 | 4100 | _alt_domains="$3" |
d2cde379 | 4101 | |
af1cc3b3 | 4102 | if _contains "$_main_domain" ","; then |
4103 | _main_domain=$(echo "$2,$3" | cut -d , -f 1) | |
02140ce7 | 4104 | _alt_domains=$(echo "$2,$3" | cut -d , -f 2- | sed "s/,${NO_VALUE}$//") |
2aff36e7 | 4105 | fi |
674b5088 | 4106 | _debug _main_domain "$_main_domain" |
4107 | _debug _alt_domains "$_alt_domains" | |
4108 | ||
d9c9114b | 4109 | _key_length="$4" |
85e1f4ea | 4110 | _real_cert="$5" |
4111 | _real_key="$6" | |
4112 | _real_ca="$7" | |
4113 | _reload_cmd="$8" | |
4114 | _real_fullchain="$9" | |
4115 | _pre_hook="${10}" | |
4116 | _post_hook="${11}" | |
4117 | _renew_hook="${12}" | |
4118 | _local_addr="${13}" | |
875625b1 | 4119 | _challenge_alias="${14}" |
e3ebd582 | 4120 | _preferred_chain="${15}" |
4c2a3841 | 4121 | |
bd04638d | 4122 | if [ -z "$_ACME_IS_RENEW" ]; then |
d9c9114b | 4123 | _initpath "$_main_domain" "$_key_length" |
43822d37 | 4124 | mkdir -p "$DOMAIN_PATH" |
4125 | fi | |
eccec5f6 | 4126 | |
a0923622 | 4127 | if _hasfield "$_web_roots" "$W_DNS" && [ -z "$FORCE_DNS_MANUAL" ]; then |
4128 | _err "$_DNS_MANUAL_ERROR" | |
4129 | return 1 | |
4130 | fi | |
4131 | ||
48d9a8c1 | 4132 | _debug "Using ACME_DIRECTORY: $ACME_DIRECTORY" |
4133 | ||
4134 | _initAPI | |
4135 | ||
4c2a3841 | 4136 | if [ -f "$DOMAIN_CONF" ]; then |
61623d22 | 4137 | Le_NextRenewTime=$(_readdomainconf Le_NextRenewTime) |
a4270efa | 4138 | _debug Le_NextRenewTime "$Le_NextRenewTime" |
95e06de5 | 4139 | if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(_time)" -lt "$Le_NextRenewTime" ]; then |
bb25febd | 4140 | _saved_domain=$(_readdomainconf Le_Domain) |
4141 | _debug _saved_domain "$_saved_domain" | |
4142 | _saved_alt=$(_readdomainconf Le_Alt) | |
4143 | _debug _saved_alt "$_saved_alt" | |
02140ce7 | 4144 | if [ "$_saved_domain,$_saved_alt" = "$_main_domain,$_alt_domains" ]; then |
bb25febd | 4145 | _info "Domains not changed." |
4146 | _info "Skip, Next renewal time is: $(__green "$(_readdomainconf Le_NextRenewTimeStr)")" | |
4c2a3841 | 4147 | _info "Add '$(__red '--force')' to force to renew." |
bb25febd | 4148 | return $RENEW_SKIP |
4149 | else | |
4150 | _info "Domains have changed." | |
4151 | fi | |
4c3b3608 | 4152 | fi |
4153 | fi | |
96a46cfc | 4154 | |
af1cc3b3 | 4155 | _savedomainconf "Le_Domain" "$_main_domain" |
02140ce7 | 4156 | _savedomainconf "Le_Alt" "$_alt_domains" |
af1cc3b3 | 4157 | _savedomainconf "Le_Webroot" "$_web_roots" |
4c2a3841 | 4158 | |
c7257bcf | 4159 | _savedomainconf "Le_PreHook" "$_pre_hook" "base64" |
4160 | _savedomainconf "Le_PostHook" "$_post_hook" "base64" | |
4161 | _savedomainconf "Le_RenewHook" "$_renew_hook" "base64" | |
4c2a3841 | 4162 | |
85e1f4ea | 4163 | if [ "$_local_addr" ]; then |
4164 | _savedomainconf "Le_LocalAddress" "$_local_addr" | |
72518d48 | 4165 | else |
4166 | _cleardomainconf "Le_LocalAddress" | |
4167 | fi | |
875625b1 | 4168 | if [ "$_challenge_alias" ]; then |
4169 | _savedomainconf "Le_ChallengeAlias" "$_challenge_alias" | |
4170 | else | |
4171 | _cleardomainconf "Le_ChallengeAlias" | |
4172 | fi | |
e3ebd582 | 4173 | if [ "$_preferred_chain" ]; then |
4174 | _savedomainconf "Le_Preferred_Chain" "$_preferred_chain" "base64" | |
4175 | else | |
4176 | _cleardomainconf "Le_Preferred_Chain" | |
4177 | fi | |
6ae0f7f5 | 4178 | |
a6d22e3b | 4179 | Le_API="$ACME_DIRECTORY" |
4180 | _savedomainconf "Le_API" "$Le_API" | |
4181 | ||
389518e1 | 4182 | _info "Using CA: $ACME_DIRECTORY" |
02140ce7 | 4183 | if [ "$_alt_domains" = "$NO_VALUE" ]; then |
4184 | _alt_domains="" | |
4c3b3608 | 4185 | fi |
4c2a3841 | 4186 | |
d9c9114b | 4187 | if [ "$_key_length" = "$NO_VALUE" ]; then |
4188 | _key_length="" | |
d404e92d | 4189 | fi |
4c2a3841 | 4190 | |
85e1f4ea | 4191 | if ! _on_before_issue "$_web_roots" "$_main_domain" "$_alt_domains" "$_pre_hook" "$_local_addr"; then |
0463b5d6 | 4192 | _err "_on_before_issue." |
4193 | return 1 | |
4c3b3608 | 4194 | fi |
0463b5d6 | 4195 | |
8a29fbc8 | 4196 | _saved_account_key_hash="$(_readcaconf "CA_KEY_HASH")" |
4197 | _debug2 _saved_account_key_hash "$_saved_account_key_hash" | |
4c2a3841 | 4198 | |
e8b54a50 | 4199 | if [ -z "$ACCOUNT_URL" ] || [ -z "$_saved_account_key_hash" ] || [ "$_saved_account_key_hash" != "$(__calcAccountKeyHash)" ]; then |
57e58ce7 | 4200 | if ! _regAccount "$_accountkeylength"; then |
85e1f4ea | 4201 | _on_issue_err "$_post_hook" |
8a29fbc8 | 4202 | return 1 |
4203 | fi | |
57e58ce7 | 4204 | else |
4205 | _debug "_saved_account_key_hash is not changed, skip register account." | |
166096dc | 4206 | fi |
166096dc | 4207 | |
4c2a3841 | 4208 | if [ -f "$CSR_PATH" ] && [ ! -f "$CERT_KEY_PATH" ]; then |
10afcaca | 4209 | _info "Signing from existing CSR." |
4210 | else | |
4211 | _key=$(_readdomainconf Le_Keylength) | |
4212 | _debug "Read key length:$_key" | |
c4b2e582 | 4213 | if [ ! -f "$CERT_KEY_PATH" ] || [ "$_key_length" != "$_key" ] || [ "$Le_ForceNewDomainKey" = "1" ]; then |
d9c9114b | 4214 | if ! createDomainKey "$_main_domain" "$_key_length"; then |
10afcaca | 4215 | _err "Create domain key error." |
4216 | _clearup | |
85e1f4ea | 4217 | _on_issue_err "$_post_hook" |
10afcaca | 4218 | return 1 |
4219 | fi | |
4220 | fi | |
4221 | ||
02140ce7 | 4222 | if ! _createcsr "$_main_domain" "$_alt_domains" "$CERT_KEY_PATH" "$CSR_PATH" "$DOMAIN_SSL_CONF"; then |
10afcaca | 4223 | _err "Create CSR error." |
5ef501c5 | 4224 | _clearup |
85e1f4ea | 4225 | _on_issue_err "$_post_hook" |
41e3eafa | 4226 | return 1 |
4227 | fi | |
4c3b3608 | 4228 | fi |
10afcaca | 4229 | |
d9c9114b | 4230 | _savedomainconf "Le_Keylength" "$_key_length" |
4c2a3841 | 4231 | |
4c3b3608 | 4232 | vlist="$Le_Vlist" |
882ac74a | 4233 | _cleardomainconf "Le_Vlist" |
cae203be | 4234 | _info "Getting domain auth token for each domain" |
4c3b3608 | 4235 | sep='#' |
9d725af6 | 4236 | dvsep=',' |
4c2a3841 | 4237 | if [ -z "$vlist" ]; then |
d2cde379 | 4238 | if [ "$ACME_VERSION" = "2" ]; then |
c1151b0d | 4239 | #make new order request |
0093dc3d | 4240 | _identifiers="{\"type\":\"dns\",\"value\":\"$(_idn "$_main_domain")\"}" |
38f1b4d2 | 4241 | _w_index=1 |
674b5088 | 4242 | while true; do |
dd17124e | 4243 | d="$(echo "$_alt_domains," | cut -d , -f "$_w_index")" |
674b5088 | 4244 | _w_index="$(_math "$_w_index" + 1)" |
4245 | _debug d "$d" | |
4246 | if [ -z "$d" ]; then | |
4247 | break | |
c1151b0d | 4248 | fi |
0093dc3d | 4249 | _identifiers="$_identifiers,{\"type\":\"dns\",\"value\":\"$(_idn "$d")\"}" |
c1151b0d | 4250 | done |
4251 | _debug2 _identifiers "$_identifiers" | |
4252 | if ! _send_signed_request "$ACME_NEW_ORDER" "{\"identifiers\": [$_identifiers]}"; then | |
4253 | _err "Create new order error." | |
4254 | _clearup | |
4255 | _on_issue_err "$_post_hook" | |
4256 | return 1 | |
4257 | fi | |
05aa26e6 | 4258 | Le_LinkOrder="$(echo "$responseHeaders" | grep -i '^Location.*$' | _tail_n 1 | tr -d "\r\n " | cut -d ":" -f 2-)" |
fbdc5a0e | 4259 | _debug Le_LinkOrder "$Le_LinkOrder" |
dbc44c08 | 4260 | Le_OrderFinalize="$(echo "$response" | _egrep_o '"finalize" *: *"[^"]*"' | cut -d '"' -f 4)" |
d2cde379 | 4261 | _debug Le_OrderFinalize "$Le_OrderFinalize" |
4262 | if [ -z "$Le_OrderFinalize" ]; then | |
78915896 | 4263 | _err "Create new order error. Le_OrderFinalize not found. $response" |
c1151b0d | 4264 | _clearup |
4265 | _on_issue_err "$_post_hook" | |
4266 | return 1 | |
4267 | fi | |
4268 | ||
4269 | #for dns manual mode | |
d2cde379 | 4270 | _savedomainconf "Le_OrderFinalize" "$Le_OrderFinalize" |
c1151b0d | 4271 | |
93de1e49 | 4272 | _authorizations_seg="$(echo "$response" | _json_decode | _egrep_o '"authorizations" *: *\[[^\[]*\]' | cut -d '[' -f 2 | tr -d ']' | tr -d '"')" |
c1151b0d | 4273 | _debug2 _authorizations_seg "$_authorizations_seg" |
4274 | if [ -z "$_authorizations_seg" ]; then | |
4275 | _err "_authorizations_seg not found." | |
4276 | _clearup | |
4277 | _on_issue_err "$_post_hook" | |
4278 | return 1 | |
4279 | fi | |
4280 | ||
4281 | #domain and authz map | |
4282 | _authorizations_map="" | |
f8d22c48 | 4283 | for _authz_url in $(echo "$_authorizations_seg" | tr ',' ' '); do |
c1151b0d | 4284 | _debug2 "_authz_url" "$_authz_url" |
0483d841 | 4285 | if ! _send_signed_request "$_authz_url"; then |
c1151b0d | 4286 | _err "get to authz error." |
263c38ca | 4287 | _err "_authorizations_seg" "$_authorizations_seg" |
4288 | _err "_authz_url" "$_authz_url" | |
c1151b0d | 4289 | _clearup |
4290 | _on_issue_err "$_post_hook" | |
4291 | return 1 | |
4292 | fi | |
4293 | ||
4294 | response="$(echo "$response" | _normalizeJson)" | |
4295 | _debug2 response "$response" | |
4296 | _d="$(echo "$response" | _egrep_o '"value" *: *"[^"]*"' | cut -d : -f 2 | tr -d ' "')" | |
72f54ca6 | 4297 | if _contains "$response" "\"wildcard\" *: *true"; then |
4298 | _d="*.$_d" | |
4299 | fi | |
c1151b0d | 4300 | _debug2 _d "$_d" |
4301 | _authorizations_map="$_d,$response | |
4302 | $_authorizations_map" | |
4303 | done | |
4304 | _debug2 _authorizations_map "$_authorizations_map" | |
4305 | fi | |
4306 | ||
c1151b0d | 4307 | _index=0 |
a63b05a9 | 4308 | _currentRoot="" |
38f1b4d2 | 4309 | _w_index=1 |
88bbe55b | 4310 | while true; do |
88bbe55b | 4311 | d="$(echo "$_main_domain,$_alt_domains," | cut -d , -f "$_w_index")" |
4312 | _w_index="$(_math "$_w_index" + 1)" | |
4313 | _debug d "$d" | |
4314 | if [ -z "$d" ]; then | |
4315 | break | |
4316 | fi | |
ca7202eb | 4317 | _info "Getting webroot for domain" "$d" |
c1151b0d | 4318 | _index=$(_math $_index + 1) |
af1cc3b3 | 4319 | _w="$(echo $_web_roots | cut -d , -f $_index)" |
9d725af6 | 4320 | _debug _w "$_w" |
4c2a3841 | 4321 | if [ "$_w" ]; then |
a63b05a9 | 4322 | _currentRoot="$_w" |
4323 | fi | |
4324 | _debug "_currentRoot" "$_currentRoot" | |
4c2a3841 | 4325 | |
a63b05a9 | 4326 | vtype="$VTYPE_HTTP" |
c1151b0d | 4327 | #todo, v2 wildcard force to use dns |
3881f221 | 4328 | if _startswith "$_currentRoot" "$W_DNS"; then |
a63b05a9 | 4329 | vtype="$VTYPE_DNS" |
4330 | fi | |
4c2a3841 | 4331 | |
08681f4a | 4332 | if [ "$_currentRoot" = "$W_ALPN" ]; then |
4333 | vtype="$VTYPE_ALPN" | |
4334 | fi | |
4335 | ||
c1151b0d | 4336 | if [ "$ACME_VERSION" = "2" ]; then |
d04c6dd3 | 4337 | _idn_d="$(_idn "$d")" |
dbc43550 L |
4338 | _candidates="$(echo "$_authorizations_map" | grep -i "^$_idn_d,")" |
4339 | _debug2 _candidates "$_candidates" | |
4340 | if [ "$(echo "$_candidates" | wc -l)" -gt 1 ]; then | |
4341 | for _can in $_candidates; do | |
d04c6dd3 | 4342 | if _startswith "$(echo "$_can" | tr '.' '|')" "$(echo "$_idn_d" | tr '.' '|'),"; then |
dbc43550 | 4343 | _candidates="$_can" |
d04c6dd3 | 4344 | break |
4345 | fi | |
4346 | done | |
4347 | fi | |
dbc43550 | 4348 | response="$(echo "$_candidates" | sed "s/$_idn_d,//")" |
c1151b0d | 4349 | _debug2 "response" "$response" |
4350 | if [ -z "$response" ]; then | |
4351 | _err "get to authz error." | |
263c38ca | 4352 | _err "_authorizations_map" "$_authorizations_map" |
c1151b0d | 4353 | _clearup |
4354 | _on_issue_err "$_post_hook" | |
4355 | return 1 | |
4356 | fi | |
4357 | else | |
4358 | if ! __get_domain_new_authz "$d"; then | |
4359 | _clearup | |
4360 | _on_issue_err "$_post_hook" | |
4361 | return 1 | |
4362 | fi | |
c4d8fd83 | 4363 | fi |
4364 | ||
4c2a3841 | 4365 | if [ -z "$thumbprint" ]; then |
339a8ad6 | 4366 | thumbprint="$(__calc_account_thumbprint)" |
4c3b3608 | 4367 | fi |
4368 | ||
dbc44c08 | 4369 | entry="$(echo "$response" | _egrep_o '[^\{]*"type":"'$vtype'"[^\}]*')" |
4c3b3608 | 4370 | _debug entry "$entry" |
9541ea6a | 4371 | keyauthorization="" |
4c2a3841 | 4372 | if [ -z "$entry" ]; then |
9541ea6a | 4373 | if ! _startswith "$d" '*.'; then |
4374 | _debug "Not a wildcard domain, lets check whether the validation is already valid." | |
4375 | if echo "$response" | grep '"status":"valid"' >/dev/null 2>&1; then | |
4376 | _debug "$d is already valid." | |
4377 | keyauthorization="$STATE_VERIFIED" | |
4378 | _debug keyauthorization "$keyauthorization" | |
4379 | fi | |
4380 | fi | |
4381 | if [ -z "$keyauthorization" ]; then | |
fc3a1817 | 4382 | _err "Error, can not get domain token entry $d for $vtype" |
9541ea6a | 4383 | _supported_vtypes="$(echo "$response" | _egrep_o "\"challenges\":\[[^]]*]" | tr '{' "\n" | grep type | cut -d '"' -f 4 | tr "\n" ' ')" |
4384 | if [ "$_supported_vtypes" ]; then | |
4385 | _err "The supported validation types are: $_supported_vtypes, but you specified: $vtype" | |
4386 | fi | |
4387 | _clearup | |
4388 | _on_issue_err "$_post_hook" | |
4389 | return 1 | |
b51ed9bb | 4390 | fi |
c1151b0d | 4391 | fi |
f8b225e7 | 4392 | |
9541ea6a | 4393 | if [ -z "$keyauthorization" ]; then |
4394 | token="$(echo "$entry" | _egrep_o '"token":"[^"]*' | cut -d : -f 2 | tr -d '"')" | |
4395 | _debug token "$token" | |
cae203be | 4396 | |
9541ea6a | 4397 | if [ -z "$token" ]; then |
4398 | _err "Error, can not get domain token $entry" | |
4399 | _clearup | |
4400 | _on_issue_err "$_post_hook" | |
4401 | return 1 | |
4402 | fi | |
4403 | if [ "$ACME_VERSION" = "2" ]; then | |
4404 | uri="$(echo "$entry" | _egrep_o '"url":"[^"]*' | cut -d '"' -f 4 | _head_n 1)" | |
4405 | else | |
4406 | uri="$(echo "$entry" | _egrep_o '"uri":"[^"]*' | cut -d '"' -f 4)" | |
4407 | fi | |
4408 | _debug uri "$uri" | |
4c3b3608 | 4409 | |
9541ea6a | 4410 | if [ -z "$uri" ]; then |
4411 | _err "Error, can not get domain uri. $entry" | |
4412 | _clearup | |
4413 | _on_issue_err "$_post_hook" | |
4414 | return 1 | |
4415 | fi | |
4416 | keyauthorization="$token.$thumbprint" | |
d35bf517 | 4417 | _debug keyauthorization "$keyauthorization" |
9541ea6a | 4418 | |
4419 | if printf "%s" "$response" | grep '"status":"valid"' >/dev/null 2>&1; then | |
4420 | _debug "$d is already verified." | |
4421 | keyauthorization="$STATE_VERIFIED" | |
4422 | _debug keyauthorization "$keyauthorization" | |
4423 | fi | |
ec603bee | 4424 | fi |
4425 | ||
a63b05a9 | 4426 | dvlist="$d$sep$keyauthorization$sep$uri$sep$vtype$sep$_currentRoot" |
4c3b3608 | 4427 | _debug dvlist "$dvlist" |
4c2a3841 | 4428 | |
9d725af6 | 4429 | vlist="$vlist$dvlist$dvsep" |
4c3b3608 | 4430 | |
4431 | done | |
9d725af6 | 4432 | _debug vlist "$vlist" |
4c3b3608 | 4433 | #add entry |
b5ca9bba | 4434 | dns_entries="" |
4c3b3608 | 4435 | dnsadded="" |
9d725af6 | 4436 | ventries=$(echo "$vlist" | tr "$dvsep" ' ') |
1f7df33e | 4437 | _alias_index=1 |
4c2a3841 | 4438 | for ventry in $ventries; do |
ca7202eb | 4439 | d=$(echo "$ventry" | cut -d "$sep" -f 1) |
4440 | keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2) | |
4441 | vtype=$(echo "$ventry" | cut -d "$sep" -f 4) | |
4442 | _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5) | |
72f54ca6 | 4443 | _debug d "$d" |
4c2a3841 | 4444 | if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then |
69212114 | 4445 | _debug "$d is already verified, skip $vtype." |
fd536d37 | 4446 | _alias_index="$(_math "$_alias_index" + 1)" |
ec603bee | 4447 | continue |
4448 | fi | |
4449 | ||
4c2a3841 | 4450 | if [ "$vtype" = "$VTYPE_DNS" ]; then |
4c3b3608 | 4451 | dnsadded='0' |
72f54ca6 | 4452 | _dns_root_d="$d" |
4453 | if _startswith "$_dns_root_d" "*."; then | |
4454 | _dns_root_d="$(echo "$_dns_root_d" | sed 's/*.//')" | |
4455 | fi | |
875625b1 | 4456 | _d_alias="$(_getfield "$_challenge_alias" "$_alias_index")" |
4457 | _alias_index="$(_math "$_alias_index" + 1)" | |
4458 | _debug "_d_alias" "$_d_alias" | |
4459 | if [ "$_d_alias" ]; then | |
64821ad4 | 4460 | if _startswith "$_d_alias" "$DNS_ALIAS_PREFIX"; then |
4461 | txtdomain="$(echo "$_d_alias" | sed "s/$DNS_ALIAS_PREFIX//")" | |
4462 | else | |
4463 | txtdomain="_acme-challenge.$_d_alias" | |
4464 | fi | |
82b0ebb7 | 4465 | dns_entry="${_dns_root_d}${dvsep}_acme-challenge.$_dns_root_d$dvsep$txtdomain$dvsep$_currentRoot" |
875625b1 | 4466 | else |
4467 | txtdomain="_acme-challenge.$_dns_root_d" | |
82b0ebb7 | 4468 | dns_entry="${_dns_root_d}${dvsep}_acme-challenge.$_dns_root_d$dvsep$dvsep$_currentRoot" |
875625b1 | 4469 | fi |
82b0ebb7 | 4470 | |
4c3b3608 | 4471 | _debug txtdomain "$txtdomain" |
11927a76 | 4472 | txt="$(printf "%s" "$keyauthorization" | _digest "sha256" | _url_replace)" |
4c3b3608 | 4473 | _debug txt "$txt" |
a61fe418 | 4474 | |
b50e701c | 4475 | d_api="$(_findHook "$_dns_root_d" $_SUB_FOLDER_DNSAPI "$_currentRoot")" |
4c3b3608 | 4476 | _debug d_api "$d_api" |
82b0ebb7 | 4477 | |
4478 | dns_entry="$dns_entry$dvsep$txt${dvsep}$d_api" | |
4479 | _debug2 dns_entry "$dns_entry" | |
4c2a3841 | 4480 | if [ "$d_api" ]; then |
a180b95c | 4481 | _debug "Found domain api file: $d_api" |
4c3b3608 | 4482 | else |
3881f221 | 4483 | if [ "$_currentRoot" != "$W_DNS" ]; then |
4484 | _err "Can not find dns api hook for: $_currentRoot" | |
4485 | _info "You need to add the txt record manually." | |
4486 | fi | |
5f8b60a0 | 4487 | _info "$(__red "Add the following TXT record:")" |
81772fb7 | 4488 | _info "$(__red "Domain: '$(__green "$txtdomain")'")" |
4489 | _info "$(__red "TXT value: '$(__green "$txt")'")" | |
4490 | _info "$(__red "Please be aware that you prepend _acme-challenge. before your domain")" | |
4491 | _info "$(__red "so the resulting subdomain will be: $txtdomain")" | |
4c3b3608 | 4492 | continue |
4493 | fi | |
4c2a3841 | 4494 | |
73b8b120 | 4495 | ( |
ca7202eb | 4496 | if ! . "$d_api"; then |
73b8b120 | 4497 | _err "Load file $d_api error. Please check your api file and try again." |
4498 | return 1 | |
4499 | fi | |
4c2a3841 | 4500 | |
158f22f7 | 4501 | addcommand="${_currentRoot}_add" |
ca7202eb | 4502 | if ! _exists "$addcommand"; then |
73b8b120 | 4503 | _err "It seems that your api file is not correct, it must have a function named: $addcommand" |
4504 | return 1 | |
4505 | fi | |
a180b95c | 4506 | _info "Adding txt value: $txt for domain: $txtdomain" |
ca7202eb | 4507 | if ! $addcommand "$txtdomain" "$txt"; then |
73b8b120 | 4508 | _err "Error add txt for domain:$txtdomain" |
4509 | return 1 | |
4510 | fi | |
a180b95c | 4511 | _info "The txt record is added: Success." |
73b8b120 | 4512 | ) |
4c2a3841 | 4513 | |
4514 | if [ "$?" != "0" ]; then | |
ea722da3 | 4515 | _on_issue_err "$_post_hook" "$vlist" |
545f2355 | 4516 | _clearup |
4c3b3608 | 4517 | return 1 |
4518 | fi | |
82b0ebb7 | 4519 | dns_entries="$dns_entries$dns_entry |
4520 | " | |
4521 | _debug2 "$dns_entries" | |
4c3b3608 | 4522 | dnsadded='1' |
4523 | fi | |
4524 | done | |
4525 | ||
4c2a3841 | 4526 | if [ "$dnsadded" = '0' ]; then |
4527 | _savedomainconf "Le_Vlist" "$vlist" | |
4c3b3608 | 4528 | _debug "Dns record not added yet, so, save to $DOMAIN_CONF and exit." |
a8b62261 | 4529 | _err "Please add the TXT records to the domains, and re-run with --renew." |
5f8b60a0 | 4530 | _on_issue_err "$_post_hook" |
545f2355 | 4531 | _clearup |
4c3b3608 | 4532 | return 1 |
4533 | fi | |
4c2a3841 | 4534 | |
4c3b3608 | 4535 | fi |
4c2a3841 | 4536 | |
b5ca9bba | 4537 | if [ "$dns_entries" ]; then |
4c2a3841 | 4538 | if [ -z "$Le_DNSSleep" ]; then |
427c2780 | 4539 | _info "Let's check each DNS record now. Sleep 20 seconds first." |
b5ca9bba | 4540 | _sleep 20 |
4541 | if ! _check_dns_entries; then | |
4542 | _err "check dns error." | |
4543 | _on_issue_err "$_post_hook" | |
4544 | _clearup | |
4545 | return 1 | |
4546 | fi | |
0e38c60d | 4547 | else |
4c2a3841 | 4548 | _savedomainconf "Le_DNSSleep" "$Le_DNSSleep" |
b5ca9bba | 4549 | _info "Sleep $(__green $Le_DNSSleep) seconds for the txt records to take effect" |
4550 | _sleep "$Le_DNSSleep" | |
0e38c60d | 4551 | fi |
4c3b3608 | 4552 | fi |
4c2a3841 | 4553 | |
5d943a35 | 4554 | NGINX_RESTORE_VLIST="" |
4c3b3608 | 4555 | _debug "ok, let's start to verify" |
a63b05a9 | 4556 | |
0463b5d6 | 4557 | _ncIndex=1 |
9d725af6 | 4558 | ventries=$(echo "$vlist" | tr "$dvsep" ' ') |
4c2a3841 | 4559 | for ventry in $ventries; do |
ca7202eb | 4560 | d=$(echo "$ventry" | cut -d "$sep" -f 1) |
4561 | keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2) | |
4562 | uri=$(echo "$ventry" | cut -d "$sep" -f 3) | |
4563 | vtype=$(echo "$ventry" | cut -d "$sep" -f 4) | |
4564 | _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5) | |
ec603bee | 4565 | |
4c2a3841 | 4566 | if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then |
ec603bee | 4567 | _info "$d is already verified, skip $vtype." |
4568 | continue | |
4569 | fi | |
4570 | ||
dd068467 | 4571 | _info "Verifying: $d" |
4c3b3608 | 4572 | _debug "d" "$d" |
4573 | _debug "keyauthorization" "$keyauthorization" | |
4574 | _debug "uri" "$uri" | |
4575 | removelevel="" | |
e22bcf7c | 4576 | token="$(printf "%s" "$keyauthorization" | cut -d '.' -f 1)" |
a63b05a9 | 4577 | |
4578 | _debug "_currentRoot" "$_currentRoot" | |
4579 | ||
4c2a3841 | 4580 | if [ "$vtype" = "$VTYPE_HTTP" ]; then |
4581 | if [ "$_currentRoot" = "$NO_VALUE" ]; then | |
4c3b3608 | 4582 | _info "Standalone mode server" |
85e1f4ea | 4583 | _ncaddr="$(_getfield "$_local_addr" "$_ncIndex")" |
0463b5d6 | 4584 | _ncIndex="$(_math $_ncIndex + 1)" |
3794b5cb | 4585 | _startserver "$keyauthorization" "$_ncaddr" |
4c2a3841 | 4586 | if [ "$?" != "0" ]; then |
5ef501c5 | 4587 | _clearup |
58e4d337 | 4588 | _on_issue_err "$_post_hook" "$vlist" |
6fc1447f | 4589 | return 1 |
4590 | fi | |
5dbf664a | 4591 | sleep 1 |
ca7202eb | 4592 | _debug serverproc "$serverproc" |
0e44f587 | 4593 | elif [ "$_currentRoot" = "$MODE_STATELESS" ]; then |
4594 | _info "Stateless mode for domain:$d" | |
4595 | _sleep 1 | |
9d725af6 | 4596 | elif _startswith "$_currentRoot" "$NGINX"; then |
4597 | _info "Nginx mode for domain:$d" | |
4598 | #set up nginx server | |
4599 | FOUND_REAL_NGINX_CONF="" | |
4600 | BACKUP_NGINX_CONF="" | |
4601 | if ! _setNginx "$d" "$_currentRoot" "$thumbprint"; then | |
4602 | _clearup | |
58e4d337 | 4603 | _on_issue_err "$_post_hook" "$vlist" |
9d725af6 | 4604 | return 1 |
03f8d6e9 | 4605 | fi |
302c41ed | 4606 | |
03f8d6e9 | 4607 | if [ "$FOUND_REAL_NGINX_CONF" ]; then |
9d725af6 | 4608 | _realConf="$FOUND_REAL_NGINX_CONF" |
4609 | _backup="$BACKUP_NGINX_CONF" | |
4610 | _debug _realConf "$_realConf" | |
5d943a35 | 4611 | NGINX_RESTORE_VLIST="$d$sep$_realConf$sep$_backup$dvsep$NGINX_RESTORE_VLIST" |
9d725af6 | 4612 | fi |
4613 | _sleep 1 | |
4c3b3608 | 4614 | else |
4c2a3841 | 4615 | if [ "$_currentRoot" = "apache" ]; then |
6f930641 | 4616 | wellknown_path="$ACME_DIR" |
4617 | else | |
a63b05a9 | 4618 | wellknown_path="$_currentRoot/.well-known/acme-challenge" |
4c2a3841 | 4619 | if [ ! -d "$_currentRoot/.well-known" ]; then |
6f930641 | 4620 | removelevel='1' |
4c2a3841 | 4621 | elif [ ! -d "$_currentRoot/.well-known/acme-challenge" ]; then |
6f930641 | 4622 | removelevel='2' |
4623 | else | |
4624 | removelevel='3' | |
4625 | fi | |
4c3b3608 | 4626 | fi |
6f930641 | 4627 | |
4c3b3608 | 4628 | _debug wellknown_path "$wellknown_path" |
6f930641 | 4629 | |
4c3b3608 | 4630 | _debug "writing token:$token to $wellknown_path/$token" |
4631 | ||
4632 | mkdir -p "$wellknown_path" | |
93fc48a2 | 4633 | |
4c2a3841 | 4634 | if ! printf "%s" "$keyauthorization" >"$wellknown_path/$token"; then |
93fc48a2 | 4635 | _err "$d:Can not write token to file : $wellknown_path/$token" |
4636 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" | |
4637 | _clearup | |
58e4d337 | 4638 | _on_issue_err "$_post_hook" "$vlist" |
93fc48a2 | 4639 | return 1 |
4640 | fi | |
4641 | ||
4c2a3841 | 4642 | if [ ! "$usingApache" ]; then |
44edb2bd | 4643 | if webroot_owner=$(_stat "$_currentRoot"); then |
32fdc196 | 4644 | _debug "Changing owner/group of .well-known to $webroot_owner" |
c87cd0de | 4645 | if ! _exec "chown -R \"$webroot_owner\" \"$_currentRoot/.well-known\""; then |
4646 | _debug "$(cat "$_EXEC_TEMP_ERR")" | |
4647 | _exec_err >/dev/null 2>&1 | |
4648 | fi | |
32fdc196 | 4649 | else |
b54ce310 | 4650 | _debug "not changing owner/group of webroot" |
32fdc196 | 4651 | fi |
df886ffa | 4652 | fi |
4c2a3841 | 4653 | |
4c3b3608 | 4654 | fi |
08681f4a | 4655 | elif [ "$vtype" = "$VTYPE_ALPN" ]; then |
4656 | acmevalidationv1="$(printf "%s" "$keyauthorization" | _digest "sha256" "hex")" | |
4657 | _debug acmevalidationv1 "$acmevalidationv1" | |
4658 | if ! _starttlsserver "$d" "" "$Le_TLSPort" "$keyauthorization" "$_ncaddr" "$acmevalidationv1"; then | |
4659 | _err "Start tls server error." | |
4660 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" | |
4661 | _clearup | |
4662 | _on_issue_err "$_post_hook" "$vlist" | |
4663 | return 1 | |
4664 | fi | |
4c3b3608 | 4665 | fi |
4c2a3841 | 4666 | |
920cab6f | 4667 | if ! __trigger_validation "$uri" "$keyauthorization" "$vtype"; then |
c4d8fd83 | 4668 | _err "$d:Can not get challenge: $response" |
4669 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" | |
4670 | _clearup | |
58e4d337 | 4671 | _on_issue_err "$_post_hook" "$vlist" |
c4d8fd83 | 4672 | return 1 |
4673 | fi | |
4c2a3841 | 4674 | |
c1151b0d | 4675 | if [ "$code" ] && [ "$code" != '202' ]; then |
8bd12ed0 | 4676 | if [ "$code" = '200' ]; then |
c1151b0d | 4677 | _debug "trigger validation code: $code" |
4678 | else | |
8bd12ed0 K |
4679 | _err "$d:Challenge error: $response" |
4680 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" | |
4681 | _clearup | |
4682 | _on_issue_err "$_post_hook" "$vlist" | |
4683 | return 1 | |
c1151b0d | 4684 | fi |
4c3b3608 | 4685 | fi |
4c2a3841 | 4686 | |
6fc1447f | 4687 | waittimes=0 |
4c2a3841 | 4688 | if [ -z "$MAX_RETRY_TIMES" ]; then |
6fc1447f | 4689 | MAX_RETRY_TIMES=30 |
4690 | fi | |
4c2a3841 | 4691 | |
4692 | while true; do | |
0c538f75 | 4693 | waittimes=$(_math "$waittimes" + 1) |
4c2a3841 | 4694 | if [ "$waittimes" -ge "$MAX_RETRY_TIMES" ]; then |
6fc1447f | 4695 | _err "$d:Timeout" |
4696 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" | |
4697 | _clearup | |
58e4d337 | 4698 | _on_issue_err "$_post_hook" "$vlist" |
6fc1447f | 4699 | return 1 |
4700 | fi | |
4c2a3841 | 4701 | |
5dbf664a | 4702 | _debug "sleep 2 secs to verify" |
4703 | sleep 2 | |
4c3b3608 | 4704 | _debug "checking" |
0483d841 | 4705 | if [ "$ACME_VERSION" = "2" ]; then |
4706 | _send_signed_request "$uri" | |
4707 | else | |
4708 | response="$(_get "$uri")" | |
4709 | fi | |
4c2a3841 | 4710 | if [ "$?" != "0" ]; then |
c60883ef | 4711 | _err "$d:Verify error:$response" |
a63b05a9 | 4712 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" |
4c3b3608 | 4713 | _clearup |
58e4d337 | 4714 | _on_issue_err "$_post_hook" "$vlist" |
4c3b3608 | 4715 | return 1 |
4716 | fi | |
9aaf36cd | 4717 | _debug2 original "$response" |
4c2a3841 | 4718 | |
4719 | response="$(echo "$response" | _normalizeJson)" | |
7012b91f | 4720 | _debug2 response "$response" |
4c2a3841 | 4721 | |
4722 | status=$(echo "$response" | _egrep_o '"status":"[^"]*' | cut -d : -f 2 | tr -d '"') | |
4723 | if [ "$status" = "valid" ]; then | |
93f3098a | 4724 | _info "$(__green Success)" |
ca7202eb | 4725 | _stopserver "$serverproc" |
4c3b3608 | 4726 | serverproc="" |
a63b05a9 | 4727 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" |
4c2a3841 | 4728 | break |
4c3b3608 | 4729 | fi |
4c2a3841 | 4730 | |
4731 | if [ "$status" = "invalid" ]; then | |
d0d74907 | 4732 | error="$(echo "$response" | _egrep_o '"error":\{[^\}]*')" |
4c2a3841 | 4733 | _debug2 error "$error" |
4734 | errordetail="$(echo "$error" | _egrep_o '"detail": *"[^"]*' | cut -d '"' -f 4)" | |
4735 | _debug2 errordetail "$errordetail" | |
4736 | if [ "$errordetail" ]; then | |
4737 | _err "$d:Verify error:$errordetail" | |
4738 | else | |
4739 | _err "$d:Verify error:$error" | |
4740 | fi | |
4741 | if [ "$DEBUG" ]; then | |
4742 | if [ "$vtype" = "$VTYPE_HTTP" ]; then | |
4743 | _debug "Debug: get token url." | |
4744 | _get "http://$d/.well-known/acme-challenge/$token" "" 1 | |
4745 | fi | |
4746 | fi | |
a63b05a9 | 4747 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" |
4c3b3608 | 4748 | _clearup |
58e4d337 | 4749 | _on_issue_err "$_post_hook" "$vlist" |
4c2a3841 | 4750 | return 1 |
4c3b3608 | 4751 | fi |
4c2a3841 | 4752 | |
4753 | if [ "$status" = "pending" ]; then | |
4c3b3608 | 4754 | _info "Pending" |
93740c99 | 4755 | elif [ "$status" = "processing" ]; then |
4756 | _info "Processing" | |
4c3b3608 | 4757 | else |
4c2a3841 | 4758 | _err "$d:Verify error:$response" |
a63b05a9 | 4759 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" |
4c3b3608 | 4760 | _clearup |
58e4d337 | 4761 | _on_issue_err "$_post_hook" "$vlist" |
4c3b3608 | 4762 | return 1 |
4763 | fi | |
4c2a3841 | 4764 | |
4c3b3608 | 4765 | done |
4c2a3841 | 4766 | |
4c3b3608 | 4767 | done |
4768 | ||
4769 | _clearup | |
4770 | _info "Verify finished, start to sign." | |
11927a76 | 4771 | der="$(_getfile "${CSR_PATH}" "${BEGIN_CSR}" "${END_CSR}" | tr -d "\r\n" | _url_replace)" |
4c2a3841 | 4772 | |
c1151b0d | 4773 | if [ "$ACME_VERSION" = "2" ]; then |
bd04638d | 4774 | _info "Lets finalize the order." |
4775 | _info "Le_OrderFinalize" "$Le_OrderFinalize" | |
d2cde379 | 4776 | if ! _send_signed_request "${Le_OrderFinalize}" "{\"csr\": \"$der\"}"; then |
c1151b0d | 4777 | _err "Sign failed." |
4778 | _on_issue_err "$_post_hook" | |
4779 | return 1 | |
4780 | fi | |
4781 | if [ "$code" != "200" ]; then | |
e7f7e96d | 4782 | _err "Sign failed, finalize code is not 200." |
668c43ab | 4783 | _err "$response" |
c1151b0d | 4784 | _on_issue_err "$_post_hook" |
4785 | return 1 | |
4786 | fi | |
e7f7e96d | 4787 | if [ -z "$Le_LinkOrder" ]; then |
0c9c1ae6 | 4788 | Le_LinkOrder="$(echo "$responseHeaders" | grep -i '^Location.*$' | _tail_n 1 | tr -d "\r\n \t" | cut -d ":" -f 2-)" |
e7f7e96d | 4789 | fi |
fbdc5a0e | 4790 | |
e7f7e96d | 4791 | _savedomainconf "Le_LinkOrder" "$Le_LinkOrder" |
4792 | ||
4793 | _link_cert_retry=0 | |
ff9be30f | 4794 | _MAX_CERT_RETRY=30 |
a3d8b993 | 4795 | while [ "$_link_cert_retry" -lt "$_MAX_CERT_RETRY" ]; do |
e7f7e96d | 4796 | if _contains "$response" "\"status\":\"valid\""; then |
4797 | _debug "Order status is valid." | |
d0d74907 | 4798 | Le_LinkCert="$(echo "$response" | _egrep_o '"certificate" *: *"[^"]*"' | cut -d '"' -f 4)" |
e7f7e96d | 4799 | _debug Le_LinkCert "$Le_LinkCert" |
4800 | if [ -z "$Le_LinkCert" ]; then | |
4801 | _err "Sign error, can not find Le_LinkCert" | |
4802 | _err "$response" | |
4803 | _on_issue_err "$_post_hook" | |
4804 | return 1 | |
4805 | fi | |
4806 | break | |
4807 | elif _contains "$response" "\"processing\""; then | |
4808 | _info "Order status is processing, lets sleep and retry." | |
1fe8235a | 4809 | _retryafter=$(echo "$responseHeaders" | grep -i "^Retry-After *:" | cut -d : -f 2 | tr -d ' ' | tr -d '\r') |
4810 | _debug "_retryafter" "$_retryafter" | |
4811 | if [ "$_retryafter" ]; then | |
4812 | _info "Retry after: $_retryafter" | |
4813 | _sleep $_retryafter | |
4814 | else | |
4815 | _sleep 2 | |
4816 | fi | |
e7f7e96d | 4817 | else |
4818 | _err "Sign error, wrong status" | |
4819 | _err "$response" | |
4820 | _on_issue_err "$_post_hook" | |
4821 | return 1 | |
4822 | fi | |
fbdc5a0e | 4823 | #the order is processing, so we are going to poll order status |
4824 | if [ -z "$Le_LinkOrder" ]; then | |
4825 | _err "Sign error, can not get order link location header" | |
4826 | _err "responseHeaders" "$responseHeaders" | |
4827 | _on_issue_err "$_post_hook" | |
4828 | return 1 | |
4829 | fi | |
4830 | _info "Polling order status: $Le_LinkOrder" | |
e7f7e96d | 4831 | if ! _send_signed_request "$Le_LinkOrder"; then |
4832 | _err "Sign failed, can not post to Le_LinkOrder cert:$Le_LinkOrder." | |
4833 | _err "$response" | |
4834 | _on_issue_err "$_post_hook" | |
4835 | return 1 | |
4836 | fi | |
4837 | _link_cert_retry="$(_math $_link_cert_retry + 1)" | |
4838 | done | |
4c2a3841 | 4839 | |
e7f7e96d | 4840 | if [ -z "$Le_LinkCert" ]; then |
4841 | _err "Sign failed, can not get Le_LinkCert, retry time limit." | |
4842 | _err "$response" | |
4843 | _on_issue_err "$_post_hook" | |
4844 | return 1 | |
4845 | fi | |
bd04638d | 4846 | _info "Downloading cert." |
4847 | _info "Le_LinkCert" "$Le_LinkCert" | |
f2acdd27 | 4848 | if ! _send_signed_request "$Le_LinkCert"; then |
668c43ab | 4849 | _err "Sign failed, can not download cert:$Le_LinkCert." |
4850 | _err "$response" | |
c1151b0d | 4851 | _on_issue_err "$_post_hook" |
4852 | return 1 | |
4853 | fi | |
4c3b3608 | 4854 | |
f2acdd27 | 4855 | echo "$response" >"$CERT_PATH" |
e3ebd582 | 4856 | _split_cert_chain "$CERT_PATH" "$CERT_FULLCHAIN_PATH" "$CA_CERT_PATH" |
4857 | ||
d5d38b33 | 4858 | if [ "$_preferred_chain" ] && [ -f "$CERT_FULLCHAIN_PATH" ]; then |
c384ed96 AK |
4859 | if [ "$DEBUG" ]; then |
4860 | _debug "default chain issuers: " "$(_get_chain_issuers "$CERT_FULLCHAIN_PATH")" | |
4861 | fi | |
d5d38b33 | 4862 | if ! _match_issuer "$CERT_FULLCHAIN_PATH" "$_preferred_chain"; then |
e3ebd582 | 4863 | rels="$(echo "$responseHeaders" | tr -d ' <>' | grep -i "^link:" | grep -i 'rel="alternate"' | cut -d : -f 2- | cut -d ';' -f 1)" |
4864 | _debug2 "rels" "$rels" | |
4865 | for rel in $rels; do | |
4866 | _info "Try rel: $rel" | |
4867 | if ! _send_signed_request "$rel"; then | |
4868 | _err "Sign failed, can not download cert:$rel" | |
4869 | _err "$response" | |
4870 | continue | |
4871 | fi | |
4872 | _relcert="$CERT_PATH.alt" | |
4873 | _relfullchain="$CERT_FULLCHAIN_PATH.alt" | |
4874 | _relca="$CA_CERT_PATH.alt" | |
4875 | echo "$response" >"$_relcert" | |
4876 | _split_cert_chain "$_relcert" "$_relfullchain" "$_relca" | |
c384ed96 AK |
4877 | if [ "$DEBUG" ]; then |
4878 | _debug "rel chain issuers: " "$(_get_chain_issuers "$_relfullchain")" | |
4879 | fi | |
d5d38b33 | 4880 | if _match_issuer "$_relfullchain" "$_preferred_chain"; then |
e3ebd582 | 4881 | _info "Matched issuer in: $rel" |
4882 | cat $_relcert >"$CERT_PATH" | |
4883 | cat $_relfullchain >"$CERT_FULLCHAIN_PATH" | |
4884 | cat $_relca >"$CA_CERT_PATH" | |
c384ed96 AK |
4885 | rm -f "$_relcert" |
4886 | rm -f "$_relfullchain" | |
4887 | rm -f "$_relca" | |
e3ebd582 | 4888 | break |
4889 | fi | |
c384ed96 AK |
4890 | rm -f "$_relcert" |
4891 | rm -f "$_relfullchain" | |
4892 | rm -f "$_relca" | |
e3ebd582 | 4893 | done |
4894 | fi | |
1c35f46b | 4895 | fi |
c1151b0d | 4896 | else |
4897 | if ! _send_signed_request "${ACME_NEW_ORDER}" "{\"resource\": \"$ACME_NEW_ORDER_RES\", \"csr\": \"$der\"}" "needbase64"; then | |
323febe8 | 4898 | _err "Sign failed. $response" |
c1151b0d | 4899 | _on_issue_err "$_post_hook" |
4900 | return 1 | |
4901 | fi | |
4902 | _rcert="$response" | |
36a7a840 | 4903 | Le_LinkCert="$(grep -i '^Location.*$' "$HTTP_HEADER" | _tail_n 1 | tr -d "\r\n" | cut -d " " -f 2)" |
4c2a3841 | 4904 | echo "$BEGIN_CERT" >"$CERT_PATH" |
4c3b3608 | 4905 | |
72518d48 | 4906 | #if ! _get "$Le_LinkCert" | _base64 "multiline" >> "$CERT_PATH" ; then |
4907 | # _debug "Get cert failed. Let's try last response." | |
3c07f57a | 4908 | # printf -- "%s" "$_rcert" | _dbase64 "multiline" | _base64 "multiline" >> "$CERT_PATH" |
72518d48 | 4909 | #fi |
4c2a3841 | 4910 | |
4911 | if ! printf -- "%s" "$_rcert" | _dbase64 "multiline" | _base64 "multiline" >>"$CERT_PATH"; then | |
72518d48 | 4912 | _debug "Try cert link." |
4c2a3841 | 4913 | _get "$Le_LinkCert" | _base64 "multiline" >>"$CERT_PATH" |
d404e92d | 4914 | fi |
4915 | ||
4c2a3841 | 4916 | echo "$END_CERT" >>"$CERT_PATH" |
c1151b0d | 4917 | fi |
4918 | ||
4919 | _debug "Le_LinkCert" "$Le_LinkCert" | |
4920 | _savedomainconf "Le_LinkCert" "$Le_LinkCert" | |
4921 | ||
183063a2 | 4922 | if [ -z "$Le_LinkCert" ] || ! _checkcert "$CERT_PATH"; then |
716f7277 | 4923 | response="$(echo "$response" | _dbase64 "multiline" | tr -d '\0' | _normalizeJson)" |
183063a2 | 4924 | _err "Sign failed: $(echo "$response" | _egrep_o '"detail":"[^"]*"')" |
4925 | _on_issue_err "$_post_hook" | |
4926 | return 1 | |
4927 | fi | |
4928 | ||
c1151b0d | 4929 | if [ "$Le_LinkCert" ]; then |
43822d37 | 4930 | _info "$(__green "Cert success.")" |
4c3b3608 | 4931 | cat "$CERT_PATH" |
5980ebc7 | 4932 | |
4c2a3841 | 4933 | _info "Your cert is in $(__green " $CERT_PATH ")" |
4934 | ||
4935 | if [ -f "$CERT_KEY_PATH" ]; then | |
4936 | _info "Your cert key is in $(__green " $CERT_KEY_PATH ")" | |
5980ebc7 | 4937 | fi |
4938 | ||
bd04638d | 4939 | if [ ! "$USER_PATH" ] || [ ! "$_ACME_IN_CRON" ]; then |
281aa349 | 4940 | USER_PATH="$PATH" |
4941 | _saveaccountconf "USER_PATH" "$USER_PATH" | |
4942 | fi | |
4c3b3608 | 4943 | fi |
4c3b3608 | 4944 | |
1c35f46b | 4945 | if [ "$ACME_VERSION" = "2" ]; then |
4946 | _debug "v2 chain." | |
4947 | else | |
183063a2 | 4948 | cp "$CERT_PATH" "$CERT_FULLCHAIN_PATH" |
1c35f46b | 4949 | Le_LinkIssuer=$(grep -i '^Link' "$HTTP_HEADER" | _head_n 1 | cut -d " " -f 2 | cut -d ';' -f 1 | tr -d '<>') |
d8ba26e6 | 4950 | |
1c35f46b | 4951 | if [ "$Le_LinkIssuer" ]; then |
4952 | if ! _contains "$Le_LinkIssuer" ":"; then | |
4953 | _info "$(__red "Relative issuer link found.")" | |
4954 | Le_LinkIssuer="$_ACME_SERVER_HOST$Le_LinkIssuer" | |
d8ba26e6 | 4955 | fi |
1c35f46b | 4956 | _debug Le_LinkIssuer "$Le_LinkIssuer" |
4957 | _savedomainconf "Le_LinkIssuer" "$Le_LinkIssuer" | |
d8ba26e6 | 4958 | |
1c35f46b | 4959 | _link_issuer_retry=0 |
4960 | _MAX_ISSUER_RETRY=5 | |
4961 | while [ "$_link_issuer_retry" -lt "$_MAX_ISSUER_RETRY" ]; do | |
4962 | _debug _link_issuer_retry "$_link_issuer_retry" | |
4963 | if [ "$ACME_VERSION" = "2" ]; then | |
0483d841 | 4964 | if _send_signed_request "$Le_LinkIssuer"; then |
4965 | echo "$response" >"$CA_CERT_PATH" | |
1c35f46b | 4966 | break |
4967 | fi | |
4968 | else | |
4969 | if _get "$Le_LinkIssuer" >"$CA_CERT_PATH.der"; then | |
4970 | echo "$BEGIN_CERT" >"$CA_CERT_PATH" | |
4971 | _base64 "multiline" <"$CA_CERT_PATH.der" >>"$CA_CERT_PATH" | |
4972 | echo "$END_CERT" >>"$CA_CERT_PATH" | |
0f120c41 | 4973 | if ! _checkcert "$CA_CERT_PATH"; then |
183063a2 | 4974 | _err "Can not get the ca cert." |
4975 | break | |
4976 | fi | |
1c35f46b | 4977 | cat "$CA_CERT_PATH" >>"$CERT_FULLCHAIN_PATH" |
4978 | rm -f "$CA_CERT_PATH.der" | |
4979 | break | |
4980 | fi | |
c1151b0d | 4981 | fi |
1c35f46b | 4982 | _link_issuer_retry=$(_math $_link_issuer_retry + 1) |
4983 | _sleep "$_link_issuer_retry" | |
4984 | done | |
4985 | if [ "$_link_issuer_retry" = "$_MAX_ISSUER_RETRY" ]; then | |
4986 | _err "Max retry for issuer ca cert is reached." | |
d8ba26e6 | 4987 | fi |
1c35f46b | 4988 | else |
4989 | _debug "No Le_LinkIssuer header found." | |
d8ba26e6 | 4990 | fi |
4c3b3608 | 4991 | fi |
1c35f46b | 4992 | [ -f "$CA_CERT_PATH" ] && _info "The intermediate CA cert is in $(__green " $CA_CERT_PATH ")" |
4993 | [ -f "$CERT_FULLCHAIN_PATH" ] && _info "And the full chain certs is there: $(__green " $CERT_FULLCHAIN_PATH ")" | |
4c2a3841 | 4994 | |
3aae1ae3 | 4995 | Le_CertCreateTime=$(_time) |
4c2a3841 | 4996 | _savedomainconf "Le_CertCreateTime" "$Le_CertCreateTime" |
4997 | ||
4998 | Le_CertCreateTimeStr=$(date -u) | |
4999 | _savedomainconf "Le_CertCreateTimeStr" "$Le_CertCreateTimeStr" | |
5000 | ||
ec67a1b2 | 5001 | if [ -z "$Le_RenewalDays" ] || [ "$Le_RenewalDays" -lt "0" ]; then |
5002 | Le_RenewalDays="$DEFAULT_RENEW" | |
054cb72e | 5003 | else |
4c2a3841 | 5004 | _savedomainconf "Le_RenewalDays" "$Le_RenewalDays" |
13d7cae9 | 5005 | fi |
4c2a3841 | 5006 | |
5007 | if [ "$CA_BUNDLE" ]; then | |
78009539 PS |
5008 | _saveaccountconf CA_BUNDLE "$CA_BUNDLE" |
5009 | else | |
5010 | _clearaccountconf "CA_BUNDLE" | |
5011 | fi | |
5012 | ||
2aa75f03 | 5013 | if [ "$CA_PATH" ]; then |
5014 | _saveaccountconf CA_PATH "$CA_PATH" | |
5015 | else | |
5016 | _clearaccountconf "CA_PATH" | |
5017 | fi | |
78009539 | 5018 | |
4c2a3841 | 5019 | if [ "$HTTPS_INSECURE" ]; then |
fac1e367 | 5020 | _saveaccountconf HTTPS_INSECURE "$HTTPS_INSECURE" |
5021 | else | |
4c2a3841 | 5022 | _clearaccountconf "HTTPS_INSECURE" |
13d7cae9 | 5023 | fi |
00a50605 | 5024 | |
4c2a3841 | 5025 | if [ "$Le_Listen_V4" ]; then |
5026 | _savedomainconf "Le_Listen_V4" "$Le_Listen_V4" | |
50827188 | 5027 | _cleardomainconf Le_Listen_V6 |
4c2a3841 | 5028 | elif [ "$Le_Listen_V6" ]; then |
5029 | _savedomainconf "Le_Listen_V6" "$Le_Listen_V6" | |
50827188 | 5030 | _cleardomainconf Le_Listen_V4 |
5031 | fi | |
f6dcd989 | 5032 | |
c4b2e582 | 5033 | if [ "$Le_ForceNewDomainKey" = "1" ]; then |
5034 | _savedomainconf "Le_ForceNewDomainKey" "$Le_ForceNewDomainKey" | |
5035 | else | |
5036 | _cleardomainconf Le_ForceNewDomainKey | |
5037 | fi | |
5038 | ||
ca7202eb | 5039 | Le_NextRenewTime=$(_math "$Le_CertCreateTime" + "$Le_RenewalDays" \* 24 \* 60 \* 60) |
4c2a3841 | 5040 | |
ca7202eb | 5041 | Le_NextRenewTimeStr=$(_time2str "$Le_NextRenewTime") |
4c2a3841 | 5042 | _savedomainconf "Le_NextRenewTimeStr" "$Le_NextRenewTimeStr" |
5043 | ||
ca7202eb | 5044 | Le_NextRenewTime=$(_math "$Le_NextRenewTime" - 86400) |
4c2a3841 | 5045 | _savedomainconf "Le_NextRenewTime" "$Le_NextRenewTime" |
f6dcd989 | 5046 | |
85e1f4ea | 5047 | if [ "$_real_cert$_real_key$_real_ca$_reload_cmd$_real_fullchain" ]; then |
5048 | _savedomainconf "Le_RealCertPath" "$_real_cert" | |
5049 | _savedomainconf "Le_RealCACertPath" "$_real_ca" | |
5050 | _savedomainconf "Le_RealKeyPath" "$_real_key" | |
7690f73e | 5051 | _savedomainconf "Le_ReloadCmd" "$_reload_cmd" "base64" |
85e1f4ea | 5052 | _savedomainconf "Le_RealFullChainPath" "$_real_fullchain" |
ce8dca7a | 5053 | if ! _installcert "$_main_domain" "$_real_cert" "$_real_key" "$_real_ca" "$_real_fullchain" "$_reload_cmd"; then |
5054 | return 1 | |
5055 | fi | |
01f54558 | 5056 | fi |
4c0d3f1b | 5057 | |
ce8dca7a | 5058 | if ! _on_issue_success "$_post_hook" "$_renew_hook"; then |
5059 | _err "Call hook error." | |
5060 | return 1 | |
5061 | fi | |
4c3b3608 | 5062 | } |
5063 | ||
d73438a3 | 5064 | #in_out_cert out_fullchain out_ca |
e3ebd582 | 5065 | _split_cert_chain() { |
5066 | _certf="$1" | |
5067 | _fullchainf="$2" | |
5068 | _caf="$3" | |
5069 | if [ "$(grep -- "$BEGIN_CERT" "$_certf" | wc -l)" -gt "1" ]; then | |
5070 | _debug "Found cert chain" | |
5071 | cat "$_certf" >"$_fullchainf" | |
5072 | _end_n="$(grep -n -- "$END_CERT" "$_fullchainf" | _head_n 1 | cut -d : -f 1)" | |
5073 | _debug _end_n "$_end_n" | |
5074 | sed -n "1,${_end_n}p" "$_fullchainf" >"$_certf" | |
5075 | _end_n="$(_math $_end_n + 1)" | |
5076 | sed -n "${_end_n},9999p" "$_fullchainf" >"$_caf" | |
5077 | fi | |
5078 | } | |
5079 | ||
43822d37 | 5080 | #domain [isEcc] |
4c3b3608 | 5081 | renew() { |
5082 | Le_Domain="$1" | |
4c2a3841 | 5083 | if [ -z "$Le_Domain" ]; then |
2e87e64b | 5084 | _usage "Usage: $PROJECT_ENTRY --renew --domain <domain.tld> [--ecc]" |
4c3b3608 | 5085 | return 1 |
5086 | fi | |
5087 | ||
43822d37 | 5088 | _isEcc="$2" |
5089 | ||
e799ef29 | 5090 | _initpath "$Le_Domain" "$_isEcc" |
43822d37 | 5091 | |
e2053b22 | 5092 | _info "$(__green "Renew: '$Le_Domain'")" |
4c2a3841 | 5093 | if [ ! -f "$DOMAIN_CONF" ]; then |
2e87e64b | 5094 | _info "'$Le_Domain' is not an issued domain, skip." |
acae0ac2 | 5095 | return $RENEW_SKIP |
4c3b3608 | 5096 | fi |
4c2a3841 | 5097 | |
5098 | if [ "$Le_RenewalDays" ]; then | |
1e6b68f5 | 5099 | _savedomainconf Le_RenewalDays "$Le_RenewalDays" |
5100 | fi | |
5101 | ||
8663fb7e | 5102 | . "$DOMAIN_CONF" |
c5f1cca3 | 5103 | _debug Le_API "$Le_API" |
f2add8de | 5104 | |
5105 | if [ "$Le_API" = "$LETSENCRYPT_CA_V1" ]; then | |
5106 | _cleardomainconf Le_API | |
5107 | Le_API="$DEFAULT_CA" | |
5108 | fi | |
5109 | if [ "$Le_API" = "$LETSENCRYPT_STAGING_CA_V1" ]; then | |
5110 | _cleardomainconf Le_API | |
5111 | Le_API="$DEFAULT_STAGING_CA" | |
5112 | fi | |
5113 | ||
4c2a3841 | 5114 | if [ "$Le_API" ]; then |
48d9a8c1 | 5115 | export ACME_DIRECTORY="$Le_API" |
c4236e58 | 5116 | #reload ca configs |
5117 | ACCOUNT_KEY_PATH="" | |
5118 | ACCOUNT_JSON_PATH="" | |
5119 | CA_CONF="" | |
5120 | _debug3 "initpath again." | |
5121 | _initpath "$Le_Domain" "$_isEcc" | |
5c48e139 | 5122 | fi |
4c2a3841 | 5123 | |
5124 | if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(_time)" -lt "$Le_NextRenewTime" ]; then | |
e2053b22 | 5125 | _info "Skip, Next renewal time is: $(__green "$Le_NextRenewTimeStr")" |
5126 | _info "Add '$(__red '--force')' to force to renew." | |
e799ef29 | 5127 | return "$RENEW_SKIP" |
4c3b3608 | 5128 | fi |
4c2a3841 | 5129 | |
bd04638d | 5130 | if [ "$_ACME_IN_CRON" = "1" ] && [ -z "$Le_CertCreateTime" ]; then |
c4d0aec5 | 5131 | _info "Skip invalid cert for: $Le_Domain" |
acae0ac2 | 5132 | return $RENEW_SKIP |
c4d0aec5 | 5133 | fi |
5134 | ||
bd04638d | 5135 | _ACME_IS_RENEW="1" |
7690f73e | 5136 | Le_ReloadCmd="$(_readdomainconf Le_ReloadCmd)" |
c7257bcf | 5137 | Le_PreHook="$(_readdomainconf Le_PreHook)" |
5138 | Le_PostHook="$(_readdomainconf Le_PostHook)" | |
5139 | Le_RenewHook="$(_readdomainconf Le_RenewHook)" | |
b7b01999 | 5140 | Le_Preferred_Chain="$(_readdomainconf Le_Preferred_Chain)" |
e3ebd582 | 5141 | issue "$Le_Webroot" "$Le_Domain" "$Le_Alt" "$Le_Keylength" "$Le_RealCertPath" "$Le_RealKeyPath" "$Le_RealCACertPath" "$Le_ReloadCmd" "$Le_RealFullChainPath" "$Le_PreHook" "$Le_PostHook" "$Le_RenewHook" "$Le_LocalAddress" "$Le_ChallengeAlias" "$Le_Preferred_Chain" |
e799ef29 | 5142 | res="$?" |
4c2a3841 | 5143 | if [ "$res" != "0" ]; then |
e799ef29 | 5144 | return "$res" |
a61fe418 | 5145 | fi |
4c2a3841 | 5146 | |
5147 | if [ "$Le_DeployHook" ]; then | |
93bce1b2 | 5148 | _deploy "$Le_Domain" "$Le_DeployHook" |
e799ef29 | 5149 | res="$?" |
a61fe418 | 5150 | fi |
4c2a3841 | 5151 | |
bd04638d | 5152 | _ACME_IS_RENEW="" |
4c3b3608 | 5153 | |
e799ef29 | 5154 | return "$res" |
4c3b3608 | 5155 | } |
5156 | ||
cc179731 | 5157 | #renewAll [stopRenewOnError] |
4c3b3608 | 5158 | renewAll() { |
5159 | _initpath | |
cc179731 | 5160 | _stopRenewOnError="$1" |
5161 | _debug "_stopRenewOnError" "$_stopRenewOnError" | |
5162 | _ret="0" | |
b50e701c | 5163 | _success_msg="" |
5164 | _error_msg="" | |
5165 | _skipped_msg="" | |
c6b68551 | 5166 | _error_level=$NOTIFY_LEVEL_SKIP |
5167 | _notify_code=$RENEW_SKIP | |
f803c6c0 | 5168 | _set_level=${NOTIFY_LEVEL:-$NOTIFY_LEVEL_DEFAULT} |
5169 | _debug "_set_level" "$_set_level" | |
e591d5cf | 5170 | for di in "${CERT_HOME}"/*.*/; do |
5171 | _debug di "$di" | |
44483dba | 5172 | if ! [ -d "$di" ]; then |
2e87e64b | 5173 | _debug "Not a directory, skip: $di" |
3498a585 | 5174 | continue |
5175 | fi | |
e591d5cf | 5176 | d=$(basename "$di") |
201aa244 | 5177 | _debug d "$d" |
43822d37 | 5178 | ( |
201aa244 | 5179 | if _endswith "$d" "$ECC_SUFFIX"; then |
5180 | _isEcc=$(echo "$d" | cut -d "$ECC_SEP" -f 2) | |
5181 | d=$(echo "$d" | cut -d "$ECC_SEP" -f 1) | |
43822d37 | 5182 | fi |
5183 | renew "$d" "$_isEcc" | |
4d2f38b0 | 5184 | ) |
cc179731 | 5185 | rc="$?" |
5186 | _debug "Return code: $rc" | |
c6b68551 | 5187 | if [ "$rc" = "0" ]; then |
5188 | if [ $_error_level -gt $NOTIFY_LEVEL_RENEW ]; then | |
5189 | _error_level="$NOTIFY_LEVEL_RENEW" | |
5190 | _notify_code=0 | |
5191 | fi | |
bd04638d | 5192 | if [ "$_ACME_IN_CRON" ]; then |
f803c6c0 | 5193 | if [ $_set_level -ge $NOTIFY_LEVEL_RENEW ]; then |
c6b68551 | 5194 | if [ "$NOTIFY_MODE" = "$NOTIFY_MODE_CERT" ]; then |
5195 | _send_notify "Renew $d success" "Good, the cert is renewed." "$NOTIFY_HOOK" 0 | |
5196 | fi | |
5197 | fi | |
5198 | fi | |
5199 | _success_msg="${_success_msg} $d | |
b50e701c | 5200 | " |
c6b68551 | 5201 | elif [ "$rc" = "$RENEW_SKIP" ]; then |
5202 | if [ $_error_level -gt $NOTIFY_LEVEL_SKIP ]; then | |
5203 | _error_level="$NOTIFY_LEVEL_SKIP" | |
5204 | _notify_code=$RENEW_SKIP | |
5205 | fi | |
bd04638d | 5206 | if [ "$_ACME_IN_CRON" ]; then |
f803c6c0 | 5207 | if [ $_set_level -ge $NOTIFY_LEVEL_SKIP ]; then |
c6b68551 | 5208 | if [ "$NOTIFY_MODE" = "$NOTIFY_MODE_CERT" ]; then |
5209 | _send_notify "Renew $d skipped" "Good, the cert is skipped." "$NOTIFY_HOOK" "$RENEW_SKIP" | |
5210 | fi | |
b50e701c | 5211 | fi |
cc179731 | 5212 | fi |
c6b68551 | 5213 | _info "Skipped $d" |
5214 | _skipped_msg="${_skipped_msg} $d | |
5215 | " | |
b50e701c | 5216 | else |
c6b68551 | 5217 | if [ $_error_level -gt $NOTIFY_LEVEL_ERROR ]; then |
5218 | _error_level="$NOTIFY_LEVEL_ERROR" | |
5219 | _notify_code=1 | |
5220 | fi | |
bd04638d | 5221 | if [ "$_ACME_IN_CRON" ]; then |
f803c6c0 | 5222 | if [ $_set_level -ge $NOTIFY_LEVEL_ERROR ]; then |
c6b68551 | 5223 | if [ "$NOTIFY_MODE" = "$NOTIFY_MODE_CERT" ]; then |
5224 | _send_notify "Renew $d error" "There is an error." "$NOTIFY_HOOK" 1 | |
5225 | fi | |
5226 | fi | |
5227 | fi | |
5228 | _error_msg="${_error_msg} $d | |
b50e701c | 5229 | " |
c6b68551 | 5230 | if [ "$_stopRenewOnError" ]; then |
5231 | _err "Error renew $d, stop now." | |
5232 | _ret="$rc" | |
5233 | break | |
5234 | else | |
5235 | _ret="$rc" | |
5236 | _err "Error renew $d." | |
5237 | fi | |
cc179731 | 5238 | fi |
4c3b3608 | 5239 | done |
c6b68551 | 5240 | _debug _error_level "$_error_level" |
a2738e85 | 5241 | _debug _set_level "$_set_level" |
bd04638d | 5242 | if [ "$_ACME_IN_CRON" ] && [ $_error_level -le $_set_level ]; then |
b50e701c | 5243 | if [ -z "$NOTIFY_MODE" ] || [ "$NOTIFY_MODE" = "$NOTIFY_MODE_BULK" ]; then |
5244 | _msg_subject="Renew" | |
5245 | if [ "$_error_msg" ]; then | |
5246 | _msg_subject="${_msg_subject} Error" | |
c6b68551 | 5247 | _msg_data="Error certs: |
5248 | ${_error_msg} | |
5249 | " | |
b50e701c | 5250 | fi |
5251 | if [ "$_success_msg" ]; then | |
5252 | _msg_subject="${_msg_subject} Success" | |
c6b68551 | 5253 | _msg_data="${_msg_data}Success certs: |
5254 | ${_success_msg} | |
5255 | " | |
b50e701c | 5256 | fi |
5257 | if [ "$_skipped_msg" ]; then | |
5258 | _msg_subject="${_msg_subject} Skipped" | |
c6b68551 | 5259 | _msg_data="${_msg_data}Skipped certs: |
5260 | ${_skipped_msg} | |
b50e701c | 5261 | " |
c6b68551 | 5262 | fi |
5263 | ||
5264 | _send_notify "$_msg_subject" "$_msg_data" "$NOTIFY_HOOK" "$_notify_code" | |
b50e701c | 5265 | fi |
5266 | fi | |
5267 | ||
201aa244 | 5268 | return "$_ret" |
4c3b3608 | 5269 | } |
5270 | ||
10afcaca | 5271 | #csr webroot |
4c2a3841 | 5272 | signcsr() { |
10afcaca | 5273 | _csrfile="$1" |
5274 | _csrW="$2" | |
5275 | if [ -z "$_csrfile" ] || [ -z "$_csrW" ]; then | |
2e87e64b | 5276 | _usage "Usage: $PROJECT_ENTRY --sign-csr --csr <csr-file> --webroot <directory>" |
10afcaca | 5277 | return 1 |
5278 | fi | |
5279 | ||
875625b1 | 5280 | _real_cert="$3" |
5281 | _real_key="$4" | |
5282 | _real_ca="$5" | |
5283 | _reload_cmd="$6" | |
5284 | _real_fullchain="$7" | |
5285 | _pre_hook="${8}" | |
5286 | _post_hook="${9}" | |
5287 | _renew_hook="${10}" | |
5288 | _local_addr="${11}" | |
5289 | _challenge_alias="${12}" | |
c384ed96 | 5290 | _preferred_chain="${13}" |
875625b1 | 5291 | |
10afcaca | 5292 | _csrsubj=$(_readSubjectFromCSR "$_csrfile") |
4c2a3841 | 5293 | if [ "$?" != "0" ]; then |
10afcaca | 5294 | _err "Can not read subject from csr: $_csrfile" |
5295 | return 1 | |
5296 | fi | |
ad752b31 | 5297 | _debug _csrsubj "$_csrsubj" |
2c9ed4c5 | 5298 | if _contains "$_csrsubj" ' ' || ! _contains "$_csrsubj" '.'; then |
5299 | _info "It seems that the subject: $_csrsubj is not a valid domain name. Drop it." | |
5300 | _csrsubj="" | |
5301 | fi | |
10afcaca | 5302 | |
5303 | _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile") | |
4c2a3841 | 5304 | if [ "$?" != "0" ]; then |
10afcaca | 5305 | _err "Can not read domain list from csr: $_csrfile" |
5306 | return 1 | |
5307 | fi | |
5308 | _debug "_csrdomainlist" "$_csrdomainlist" | |
4c2a3841 | 5309 | |
5310 | if [ -z "$_csrsubj" ]; then | |
ad752b31 | 5311 | _csrsubj="$(_getfield "$_csrdomainlist" 1)" |
5312 | _debug _csrsubj "$_csrsubj" | |
5313 | _csrdomainlist="$(echo "$_csrdomainlist" | cut -d , -f 2-)" | |
5314 | _debug "_csrdomainlist" "$_csrdomainlist" | |
5315 | fi | |
4c2a3841 | 5316 | |
5317 | if [ -z "$_csrsubj" ]; then | |
ad752b31 | 5318 | _err "Can not read subject from csr: $_csrfile" |
5319 | return 1 | |
5320 | fi | |
4c2a3841 | 5321 | |
10afcaca | 5322 | _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile") |
4c2a3841 | 5323 | if [ "$?" != "0" ] || [ -z "$_csrkeylength" ]; then |
10afcaca | 5324 | _err "Can not read key length from csr: $_csrfile" |
5325 | return 1 | |
5326 | fi | |
4c2a3841 | 5327 | |
cd9fb3b6 | 5328 | if [ -z "$ACME_VERSION" ] && _contains "$_csrsubj,$_csrdomainlist" "*."; then |
5329 | export ACME_VERSION=2 | |
5330 | fi | |
10afcaca | 5331 | _initpath "$_csrsubj" "$_csrkeylength" |
5332 | mkdir -p "$DOMAIN_PATH" | |
4c2a3841 | 5333 | |
10afcaca | 5334 | _info "Copy csr to: $CSR_PATH" |
5335 | cp "$_csrfile" "$CSR_PATH" | |
4c2a3841 | 5336 | |
c384ed96 | 5337 | issue "$_csrW" "$_csrsubj" "$_csrdomainlist" "$_csrkeylength" "$_real_cert" "$_real_key" "$_real_ca" "$_reload_cmd" "$_real_fullchain" "$_pre_hook" "$_post_hook" "$_renew_hook" "$_local_addr" "$_challenge_alias" "$_preferred_chain" |
4c2a3841 | 5338 | |
10afcaca | 5339 | } |
5340 | ||
5341 | showcsr() { | |
4c2a3841 | 5342 | _csrfile="$1" |
10afcaca | 5343 | _csrd="$2" |
5344 | if [ -z "$_csrfile" ] && [ -z "$_csrd" ]; then | |
2e87e64b | 5345 | _usage "Usage: $PROJECT_ENTRY --show-csr --csr <csr-file>" |
10afcaca | 5346 | return 1 |
5347 | fi | |
5348 | ||
5349 | _initpath | |
4c2a3841 | 5350 | |
10afcaca | 5351 | _csrsubj=$(_readSubjectFromCSR "$_csrfile") |
4c2a3841 | 5352 | if [ "$?" != "0" ] || [ -z "$_csrsubj" ]; then |
10afcaca | 5353 | _err "Can not read subject from csr: $_csrfile" |
5354 | return 1 | |
5355 | fi | |
4c2a3841 | 5356 | |
10afcaca | 5357 | _info "Subject=$_csrsubj" |
5358 | ||
5359 | _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile") | |
4c2a3841 | 5360 | if [ "$?" != "0" ]; then |
10afcaca | 5361 | _err "Can not read domain list from csr: $_csrfile" |
5362 | return 1 | |
5363 | fi | |
5364 | _debug "_csrdomainlist" "$_csrdomainlist" | |
5365 | ||
5366 | _info "SubjectAltNames=$_csrdomainlist" | |
5367 | ||
10afcaca | 5368 | _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile") |
4c2a3841 | 5369 | if [ "$?" != "0" ] || [ -z "$_csrkeylength" ]; then |
10afcaca | 5370 | _err "Can not read key length from csr: $_csrfile" |
5371 | return 1 | |
5372 | fi | |
5373 | _info "KeyLength=$_csrkeylength" | |
5374 | } | |
5375 | ||
95ef046d | 5376 | #listraw domain |
6d7eda3e | 5377 | list() { |
22ea4004 | 5378 | _raw="$1" |
95ef046d | 5379 | _domain="$2" |
6d7eda3e | 5380 | _initpath |
4c2a3841 | 5381 | |
dcf4f8f6 | 5382 | _sep="|" |
4c2a3841 | 5383 | if [ "$_raw" ]; then |
95ef046d | 5384 | if [ -z "$_domain" ]; then |
5385 | printf "%s\n" "Main_Domain${_sep}KeyLength${_sep}SAN_Domains${_sep}CA${_sep}Created${_sep}Renew" | |
5386 | fi | |
e591d5cf | 5387 | for di in "${CERT_HOME}"/*.*/; do |
5388 | d=$(basename "$di") | |
201aa244 | 5389 | _debug d "$d" |
dcf4f8f6 | 5390 | ( |
201aa244 | 5391 | if _endswith "$d" "$ECC_SUFFIX"; then |
be0df07d | 5392 | _isEcc="ecc" |
201aa244 | 5393 | d=$(echo "$d" | cut -d "$ECC_SEP" -f 1) |
43822d37 | 5394 | fi |
be0df07d | 5395 | DOMAIN_CONF="$di/$d.conf" |
4c2a3841 | 5396 | if [ -f "$DOMAIN_CONF" ]; then |
dcf4f8f6 | 5397 | . "$DOMAIN_CONF" |
269847d1 | 5398 | _ca="$(_getCAShortName "$Le_API")" |
95ef046d | 5399 | if [ -z "$_domain" ]; then |
5400 | printf "%s\n" "$Le_Domain${_sep}\"$Le_Keylength\"${_sep}$Le_Alt${_sep}$_ca${_sep}$Le_CertCreateTimeStr${_sep}$Le_NextRenewTimeStr" | |
5401 | else | |
5402 | if [ "$_domain" = "$d" ]; then | |
5403 | cat "$DOMAIN_CONF" | |
5404 | fi | |
5405 | fi | |
dcf4f8f6 | 5406 | fi |
5407 | ) | |
5408 | done | |
5409 | else | |
4c2a3841 | 5410 | if _exists column; then |
95ef046d | 5411 | list "raw" "$_domain" | column -t -s "$_sep" |
22ea4004 | 5412 | else |
95ef046d | 5413 | list "raw" "$_domain" | tr "$_sep" '\t' |
22ea4004 | 5414 | fi |
dcf4f8f6 | 5415 | fi |
6d7eda3e | 5416 | |
6d7eda3e | 5417 | } |
5418 | ||
93bce1b2 | 5419 | _deploy() { |
5420 | _d="$1" | |
5421 | _hooks="$2" | |
5422 | ||
5423 | for _d_api in $(echo "$_hooks" | tr ',' " "); do | |
b50e701c | 5424 | _deployApi="$(_findHook "$_d" $_SUB_FOLDER_DEPLOY "$_d_api")" |
93bce1b2 | 5425 | if [ -z "$_deployApi" ]; then |
5426 | _err "The deploy hook $_d_api is not found." | |
5427 | return 1 | |
5428 | fi | |
5429 | _debug _deployApi "$_deployApi" | |
5430 | ||
5431 | if ! ( | |
5432 | if ! . "$_deployApi"; then | |
5433 | _err "Load file $_deployApi error. Please check your api file and try again." | |
5434 | return 1 | |
5435 | fi | |
5436 | ||
5437 | d_command="${_d_api}_deploy" | |
5438 | if ! _exists "$d_command"; then | |
5439 | _err "It seems that your api file is not correct, it must have a function named: $d_command" | |
5440 | return 1 | |
5441 | fi | |
5442 | ||
5443 | if ! $d_command "$_d" "$CERT_KEY_PATH" "$CERT_PATH" "$CA_CERT_PATH" "$CERT_FULLCHAIN_PATH"; then | |
5444 | _err "Error deploy for domain:$_d" | |
5445 | return 1 | |
5446 | fi | |
5447 | ); then | |
5448 | _err "Deploy error." | |
5449 | return 1 | |
5450 | else | |
5451 | _info "$(__green Success)" | |
5452 | fi | |
5453 | done | |
5454 | } | |
5455 | ||
5456 | #domain hooks | |
a61fe418 | 5457 | deploy() { |
93bce1b2 | 5458 | _d="$1" |
5459 | _hooks="$2" | |
a61fe418 | 5460 | _isEcc="$3" |
93bce1b2 | 5461 | if [ -z "$_hooks" ]; then |
2e87e64b | 5462 | _usage "Usage: $PROJECT_ENTRY --deploy --domain <domain.tld> --deploy-hook <hookname> [--ecc] " |
a61fe418 | 5463 | return 1 |
5464 | fi | |
5465 | ||
93bce1b2 | 5466 | _initpath "$_d" "$_isEcc" |
4c2a3841 | 5467 | if [ ! -d "$DOMAIN_PATH" ]; then |
9672c6b8 | 5468 | _err "The domain '$_d' is not a cert name. You must use the cert name to specify the cert to install." |
5469 | _err "Can not find path:'$DOMAIN_PATH'" | |
a61fe418 | 5470 | return 1 |
5471 | fi | |
4c2a3841 | 5472 | |
93bce1b2 | 5473 | . "$DOMAIN_CONF" |
4c2a3841 | 5474 | |
93bce1b2 | 5475 | _savedomainconf Le_DeployHook "$_hooks" |
4c2a3841 | 5476 | |
93bce1b2 | 5477 | _deploy "$_d" "$_hooks" |
a61fe418 | 5478 | } |
5479 | ||
4c3b3608 | 5480 | installcert() { |
85e1f4ea | 5481 | _main_domain="$1" |
5482 | if [ -z "$_main_domain" ]; then | |
2e87e64b | 5483 | _usage "Usage: $PROJECT_ENTRY --install-cert --domain <domain.tld> [--ecc] [--cert-file <file>] [--key-file <file>] [--ca-file <file>] [ --reloadcmd <command>] [--fullchain-file <file>]" |
4c3b3608 | 5484 | return 1 |
5485 | fi | |
5486 | ||
85e1f4ea | 5487 | _real_cert="$2" |
5488 | _real_key="$3" | |
5489 | _real_ca="$4" | |
5490 | _reload_cmd="$5" | |
5491 | _real_fullchain="$6" | |
43822d37 | 5492 | _isEcc="$7" |
5493 | ||
85e1f4ea | 5494 | _initpath "$_main_domain" "$_isEcc" |
4c2a3841 | 5495 | if [ ! -d "$DOMAIN_PATH" ]; then |
9672c6b8 | 5496 | _err "The domain '$_main_domain' is not a cert name. You must use the cert name to specify the cert to install." |
5497 | _err "Can not find path:'$DOMAIN_PATH'" | |
43822d37 | 5498 | return 1 |
5499 | fi | |
5500 | ||
85e1f4ea | 5501 | _savedomainconf "Le_RealCertPath" "$_real_cert" |
5502 | _savedomainconf "Le_RealCACertPath" "$_real_ca" | |
5503 | _savedomainconf "Le_RealKeyPath" "$_real_key" | |
7690f73e | 5504 | _savedomainconf "Le_ReloadCmd" "$_reload_cmd" "base64" |
85e1f4ea | 5505 | _savedomainconf "Le_RealFullChainPath" "$_real_fullchain" |
5506 | ||
044da37c | 5507 | _installcert "$_main_domain" "$_real_cert" "$_real_key" "$_real_ca" "$_real_fullchain" "$_reload_cmd" |
43822d37 | 5508 | } |
4c3b3608 | 5509 | |
044da37c | 5510 | #domain cert key ca fullchain reloadcmd backup-prefix |
43822d37 | 5511 | _installcert() { |
85e1f4ea | 5512 | _main_domain="$1" |
5513 | _real_cert="$2" | |
5514 | _real_key="$3" | |
5515 | _real_ca="$4" | |
044da37c | 5516 | _real_fullchain="$5" |
5517 | _reload_cmd="$6" | |
5518 | _backup_prefix="$7" | |
4c3b3608 | 5519 | |
85e1f4ea | 5520 | if [ "$_real_cert" = "$NO_VALUE" ]; then |
5521 | _real_cert="" | |
4d2f38b0 | 5522 | fi |
85e1f4ea | 5523 | if [ "$_real_key" = "$NO_VALUE" ]; then |
5524 | _real_key="" | |
4d2f38b0 | 5525 | fi |
85e1f4ea | 5526 | if [ "$_real_ca" = "$NO_VALUE" ]; then |
5527 | _real_ca="" | |
4d2f38b0 | 5528 | fi |
85e1f4ea | 5529 | if [ "$_reload_cmd" = "$NO_VALUE" ]; then |
5530 | _reload_cmd="" | |
4d2f38b0 | 5531 | fi |
85e1f4ea | 5532 | if [ "$_real_fullchain" = "$NO_VALUE" ]; then |
5533 | _real_fullchain="" | |
4d2f38b0 | 5534 | fi |
4c2a3841 | 5535 | |
044da37c | 5536 | _backup_path="$DOMAIN_BACKUP_PATH/$_backup_prefix" |
5537 | mkdir -p "$_backup_path" | |
5538 | ||
85e1f4ea | 5539 | if [ "$_real_cert" ]; then |
5540 | _info "Installing cert to:$_real_cert" | |
bd04638d | 5541 | if [ -f "$_real_cert" ] && [ ! "$_ACME_IS_RENEW" ]; then |
044da37c | 5542 | cp "$_real_cert" "$_backup_path/cert.bak" |
4c3b3608 | 5543 | fi |
206be3c1 | 5544 | cat "$CERT_PATH" >"$_real_cert" || return 1 |
4c3b3608 | 5545 | fi |
4c2a3841 | 5546 | |
85e1f4ea | 5547 | if [ "$_real_ca" ]; then |
5548 | _info "Installing CA to:$_real_ca" | |
5549 | if [ "$_real_ca" = "$_real_cert" ]; then | |
5550 | echo "" >>"$_real_ca" | |
206be3c1 | 5551 | cat "$CA_CERT_PATH" >>"$_real_ca" || return 1 |
4c3b3608 | 5552 | else |
bd04638d | 5553 | if [ -f "$_real_ca" ] && [ ! "$_ACME_IS_RENEW" ]; then |
044da37c | 5554 | cp "$_real_ca" "$_backup_path/ca.bak" |
78552b18 | 5555 | fi |
206be3c1 | 5556 | cat "$CA_CERT_PATH" >"$_real_ca" || return 1 |
4c3b3608 | 5557 | fi |
5558 | fi | |
5559 | ||
85e1f4ea | 5560 | if [ "$_real_key" ]; then |
5561 | _info "Installing key to:$_real_key" | |
bd04638d | 5562 | if [ -f "$_real_key" ] && [ ! "$_ACME_IS_RENEW" ]; then |
044da37c | 5563 | cp "$_real_key" "$_backup_path/key.bak" |
4c3b3608 | 5564 | fi |
82014583 | 5565 | if [ -f "$_real_key" ]; then |
206be3c1 | 5566 | cat "$CERT_KEY_PATH" >"$_real_key" || return 1 |
82014583 | 5567 | else |
206be3c1 | 5568 | cat "$CERT_KEY_PATH" >"$_real_key" || return 1 |
7b92371a | 5569 | chmod 600 "$_real_key" |
82014583 | 5570 | fi |
4c3b3608 | 5571 | fi |
4c2a3841 | 5572 | |
85e1f4ea | 5573 | if [ "$_real_fullchain" ]; then |
5574 | _info "Installing full chain to:$_real_fullchain" | |
bd04638d | 5575 | if [ -f "$_real_fullchain" ] && [ ! "$_ACME_IS_RENEW" ]; then |
044da37c | 5576 | cp "$_real_fullchain" "$_backup_path/fullchain.bak" |
a63b05a9 | 5577 | fi |
206be3c1 | 5578 | cat "$CERT_FULLCHAIN_PATH" >"$_real_fullchain" || return 1 |
4c2a3841 | 5579 | fi |
4c3b3608 | 5580 | |
85e1f4ea | 5581 | if [ "$_reload_cmd" ]; then |
5582 | _info "Run reload cmd: $_reload_cmd" | |
25555b8c | 5583 | if ( |
839bf0e2 | 5584 | export CERT_PATH |
5585 | export CERT_KEY_PATH | |
5586 | export CA_CERT_PATH | |
5587 | export CERT_FULLCHAIN_PATH | |
b3f61297 | 5588 | export Le_Domain="$_main_domain" |
85e1f4ea | 5589 | cd "$DOMAIN_PATH" && eval "$_reload_cmd" |
839bf0e2 | 5590 | ); then |
43822d37 | 5591 | _info "$(__green "Reload success")" |
4d2f38b0 | 5592 | else |
5593 | _err "Reload error for :$Le_Domain" | |
5594 | fi | |
5595 | fi | |
5596 | ||
4c3b3608 | 5597 | } |
5598 | ||
77f96b38 | 5599 | __read_password() { |
5600 | unset _pp | |
5601 | prompt="Enter Password:" | |
0b04a7f1 | 5602 | while IFS= read -p "$prompt" -r -s -n 1 char; do |
5603 | if [ "$char" = $'\0' ]; then | |
4ebad105 | 5604 | break |
0b04a7f1 | 5605 | fi |
5606 | prompt='*' | |
5607 | _pp="$_pp$char" | |
77f96b38 | 5608 | done |
5609 | echo "$_pp" | |
5610 | } | |
5611 | ||
5612 | _install_win_taskscheduler() { | |
5613 | _lesh="$1" | |
5614 | _centry="$2" | |
5615 | _randomminute="$3" | |
5616 | if ! _exists cygpath; then | |
5617 | _err "cygpath not found" | |
5618 | return 1 | |
5619 | fi | |
5620 | if ! _exists schtasks; then | |
5621 | _err "schtasks.exe is not found, are you on Windows?" | |
5622 | return 1 | |
5623 | fi | |
5624 | _winbash="$(cygpath -w $(which bash))" | |
5625 | _debug _winbash "$_winbash" | |
5626 | if [ -z "$_winbash" ]; then | |
5627 | _err "can not find bash path" | |
5628 | return 1 | |
5629 | fi | |
5630 | _myname="$(whoami)" | |
5631 | _debug "_myname" "$_myname" | |
5632 | if [ -z "$_myname" ]; then | |
5633 | _err "can not find my user name" | |
5634 | return 1 | |
5635 | fi | |
5636 | _debug "_lesh" "$_lesh" | |
5637 | ||
5638 | _info "To install scheduler task in your Windows account, you must input your windows password." | |
5639 | _info "$PROJECT_NAME doesn't save your password." | |
5640 | _info "Please input your Windows password for: $(__green "$_myname")" | |
5641 | _password="$(__read_password)" | |
5642 | #SCHTASKS.exe '/create' '/SC' 'DAILY' '/TN' "$_WINDOWS_SCHEDULER_NAME" '/F' '/ST' "00:$_randomminute" '/RU' "$_myname" '/RP' "$_password" '/TR' "$_winbash -l -c '$_lesh --cron --home \"$LE_WORKING_DIR\" $_centry'" >/dev/null | |
5643 | echo SCHTASKS.exe '/create' '/SC' 'DAILY' '/TN' "$_WINDOWS_SCHEDULER_NAME" '/F' '/ST' "00:$_randomminute" '/RU' "$_myname" '/RP' "$_password" '/TR' "\"$_winbash -l -c '$_lesh --cron --home \"$LE_WORKING_DIR\" $_centry'\"" | cmd.exe >/dev/null | |
5644 | echo | |
5645 | ||
5646 | } | |
5647 | ||
5648 | _uninstall_win_taskscheduler() { | |
5649 | if ! _exists schtasks; then | |
5650 | _err "schtasks.exe is not found, are you on Windows?" | |
5651 | return 1 | |
5652 | fi | |
5653 | if ! echo SCHTASKS /query /tn "$_WINDOWS_SCHEDULER_NAME" | cmd.exe >/dev/null; then | |
5654 | _debug "scheduler $_WINDOWS_SCHEDULER_NAME is not found." | |
5655 | else | |
5656 | _info "Removing $_WINDOWS_SCHEDULER_NAME" | |
0b04a7f1 | 5657 | echo SCHTASKS /delete /f /tn "$_WINDOWS_SCHEDULER_NAME" | cmd.exe >/dev/null |
77f96b38 | 5658 | fi |
5659 | } | |
5660 | ||
27dbe77f | 5661 | #confighome |
4c3b3608 | 5662 | installcronjob() { |
27dbe77f | 5663 | _c_home="$1" |
4c3b3608 | 5664 | _initpath |
415f375c | 5665 | _CRONTAB="crontab" |
77f96b38 | 5666 | if [ -f "$LE_WORKING_DIR/$PROJECT_ENTRY" ]; then |
5667 | lesh="\"$LE_WORKING_DIR\"/$PROJECT_ENTRY" | |
5668 | else | |
5669 | _err "Can not install cronjob, $PROJECT_ENTRY not found." | |
5670 | return 1 | |
5671 | fi | |
5672 | if [ "$_c_home" ]; then | |
5673 | _c_entry="--config-home \"$_c_home\" " | |
5674 | fi | |
5675 | _t=$(_time) | |
5676 | random_minute=$(_math $_t % 60) | |
5677 | ||
415f375c | 5678 | if ! _exists "$_CRONTAB" && _exists "fcrontab"; then |
5679 | _CRONTAB="fcrontab" | |
5680 | fi | |
77f96b38 | 5681 | |
415f375c | 5682 | if ! _exists "$_CRONTAB"; then |
77f96b38 | 5683 | if _exists cygpath && _exists schtasks.exe; then |
5684 | _info "It seems you are on Windows, let's install Windows scheduler task." | |
5685 | if _install_win_taskscheduler "$lesh" "$_c_entry" "$random_minute"; then | |
5686 | _info "Install Windows scheduler task success." | |
5687 | return 0 | |
5688 | else | |
5689 | _err "Install Windows scheduler task failed." | |
5690 | return 1 | |
5691 | fi | |
5692 | fi | |
415f375c | 5693 | _err "crontab/fcrontab doesn't exist, so, we can not install cron jobs." |
77546ea5 | 5694 | _err "All your certs will not be renewed automatically." |
a7b7355d | 5695 | _err "You must add your own cron job to call '$PROJECT_ENTRY --cron' everyday." |
77546ea5 | 5696 | return 1 |
5697 | fi | |
4c3b3608 | 5698 | _info "Installing cron job" |
415f375c | 5699 | if ! $_CRONTAB -l | grep "$PROJECT_ENTRY --cron"; then |
e2c939fb | 5700 | if _exists uname && uname -a | grep SunOS >/dev/null; then |
415f375c | 5701 | $_CRONTAB -l | { |
4c2a3841 | 5702 | cat |
0533bde9 | 5703 | echo "$random_minute 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" $_c_entry> /dev/null" |
415f375c | 5704 | } | $_CRONTAB -- |
22ea4004 | 5705 | else |
415f375c | 5706 | $_CRONTAB -l | { |
4c2a3841 | 5707 | cat |
0533bde9 | 5708 | echo "$random_minute 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" $_c_entry> /dev/null" |
415f375c | 5709 | } | $_CRONTAB - |
22ea4004 | 5710 | fi |
4c3b3608 | 5711 | fi |
4c2a3841 | 5712 | if [ "$?" != "0" ]; then |
4c3b3608 | 5713 | _err "Install cron job failed. You need to manually renew your certs." |
5714 | _err "Or you can add cronjob by yourself:" | |
a7b7355d | 5715 | _err "$lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null" |
4c3b3608 | 5716 | return 1 |
5717 | fi | |
5718 | } | |
5719 | ||
5720 | uninstallcronjob() { | |
415f375c | 5721 | _CRONTAB="crontab" |
5722 | if ! _exists "$_CRONTAB" && _exists "fcrontab"; then | |
5723 | _CRONTAB="fcrontab" | |
5724 | fi | |
5725 | ||
5726 | if ! _exists "$_CRONTAB"; then | |
77f96b38 | 5727 | if _exists cygpath && _exists schtasks.exe; then |
5728 | _info "It seems you are on Windows, let's uninstall Windows scheduler task." | |
5729 | if _uninstall_win_taskscheduler; then | |
5730 | _info "Uninstall Windows scheduler task success." | |
5731 | return 0 | |
5732 | else | |
5733 | _err "Uninstall Windows scheduler task failed." | |
5734 | return 1 | |
5735 | fi | |
5736 | fi | |
37db5b81 | 5737 | return |
5738 | fi | |
4c3b3608 | 5739 | _info "Removing cron job" |
415f375c | 5740 | cr="$($_CRONTAB -l | grep "$PROJECT_ENTRY --cron")" |
4c2a3841 | 5741 | if [ "$cr" ]; then |
5742 | if _exists uname && uname -a | grep solaris >/dev/null; then | |
415f375c | 5743 | $_CRONTAB -l | sed "/$PROJECT_ENTRY --cron/d" | $_CRONTAB -- |
22ea4004 | 5744 | else |
415f375c | 5745 | $_CRONTAB -l | sed "/$PROJECT_ENTRY --cron/d" | $_CRONTAB - |
22ea4004 | 5746 | fi |
a7b7355d | 5747 | LE_WORKING_DIR="$(echo "$cr" | cut -d ' ' -f 9 | tr -d '"')" |
4c3b3608 | 5748 | _info LE_WORKING_DIR "$LE_WORKING_DIR" |
27dbe77f | 5749 | if _contains "$cr" "--config-home"; then |
f5b546b3 | 5750 | LE_CONFIG_HOME="$(echo "$cr" | cut -d ' ' -f 11 | tr -d '"')" |
5751 | _debug LE_CONFIG_HOME "$LE_CONFIG_HOME" | |
27dbe77f | 5752 | fi |
4c2a3841 | 5753 | fi |
4c3b3608 | 5754 | _initpath |
a7b7355d | 5755 | |
4c3b3608 | 5756 | } |
5757 | ||
1041c9f9 | 5758 | #domain isECC revokeReason |
6cb415f5 | 5759 | revoke() { |
5760 | Le_Domain="$1" | |
4c2a3841 | 5761 | if [ -z "$Le_Domain" ]; then |
2e87e64b | 5762 | _usage "Usage: $PROJECT_ENTRY --revoke --domain <domain.tld> [--ecc]" |
6cb415f5 | 5763 | return 1 |
5764 | fi | |
4c2a3841 | 5765 | |
43822d37 | 5766 | _isEcc="$2" |
1041c9f9 | 5767 | _reason="$3" |
5768 | if [ -z "$_reason" ]; then | |
5769 | _reason="0" | |
5770 | fi | |
c4a375b3 | 5771 | _initpath "$Le_Domain" "$_isEcc" |
4c2a3841 | 5772 | if [ ! -f "$DOMAIN_CONF" ]; then |
6cb415f5 | 5773 | _err "$Le_Domain is not a issued domain, skip." |
4c2a3841 | 5774 | return 1 |
6cb415f5 | 5775 | fi |
4c2a3841 | 5776 | |
5777 | if [ ! -f "$CERT_PATH" ]; then | |
6cb415f5 | 5778 | _err "Cert for $Le_Domain $CERT_PATH is not found, skip." |
5779 | return 1 | |
5780 | fi | |
6cb415f5 | 5781 | |
11927a76 | 5782 | cert="$(_getfile "${CERT_PATH}" "${BEGIN_CERT}" "${END_CERT}" | tr -d "\r\n" | _url_replace)" |
4c2a3841 | 5783 | |
5784 | if [ -z "$cert" ]; then | |
6cb415f5 | 5785 | _err "Cert for $Le_Domain is empty found, skip." |
5786 | return 1 | |
5787 | fi | |
4c2a3841 | 5788 | |
48d9a8c1 | 5789 | _initAPI |
5790 | ||
d2cde379 | 5791 | if [ "$ACME_VERSION" = "2" ]; then |
1041c9f9 | 5792 | data="{\"certificate\": \"$cert\",\"reason\":$_reason}" |
d2cde379 | 5793 | else |
5794 | data="{\"resource\": \"revoke-cert\", \"certificate\": \"$cert\"}" | |
5795 | fi | |
48d9a8c1 | 5796 | uri="${ACME_REVOKE_CERT}" |
6cb415f5 | 5797 | |
4c2a3841 | 5798 | if [ -f "$CERT_KEY_PATH" ]; then |
1befee5a | 5799 | _info "Try domain key first." |
c4a375b3 | 5800 | if _send_signed_request "$uri" "$data" "" "$CERT_KEY_PATH"; then |
4c2a3841 | 5801 | if [ -z "$response" ]; then |
1befee5a | 5802 | _info "Revoke success." |
c4a375b3 | 5803 | rm -f "$CERT_PATH" |
1befee5a | 5804 | return 0 |
4c2a3841 | 5805 | else |
1befee5a | 5806 | _err "Revoke error by domain key." |
5807 | _err "$response" | |
5808 | fi | |
6cb415f5 | 5809 | fi |
4c2a3841 | 5810 | else |
eca57bee | 5811 | _info "Domain key file doesn't exist." |
6cb415f5 | 5812 | fi |
6cb415f5 | 5813 | |
1befee5a | 5814 | _info "Try account key." |
6cb415f5 | 5815 | |
c4a375b3 | 5816 | if _send_signed_request "$uri" "$data" "" "$ACCOUNT_KEY_PATH"; then |
4c2a3841 | 5817 | if [ -z "$response" ]; then |
6cb415f5 | 5818 | _info "Revoke success." |
c4a375b3 | 5819 | rm -f "$CERT_PATH" |
6cb415f5 | 5820 | return 0 |
4c2a3841 | 5821 | else |
6cb415f5 | 5822 | _err "Revoke error." |
c9c31c04 | 5823 | _debug "$response" |
6cb415f5 | 5824 | fi |
5825 | fi | |
5826 | return 1 | |
5827 | } | |
4c3b3608 | 5828 | |
78f0201d | 5829 | #domain ecc |
5830 | remove() { | |
5831 | Le_Domain="$1" | |
5832 | if [ -z "$Le_Domain" ]; then | |
2e87e64b | 5833 | _usage "Usage: $PROJECT_ENTRY --remove --domain <domain.tld> [--ecc]" |
78f0201d | 5834 | return 1 |
5835 | fi | |
5836 | ||
5837 | _isEcc="$2" | |
5838 | ||
5839 | _initpath "$Le_Domain" "$_isEcc" | |
5840 | _removed_conf="$DOMAIN_CONF.removed" | |
5841 | if [ ! -f "$DOMAIN_CONF" ]; then | |
5842 | if [ -f "$_removed_conf" ]; then | |
5843 | _err "$Le_Domain is already removed, You can remove the folder by yourself: $DOMAIN_PATH" | |
5844 | else | |
5845 | _err "$Le_Domain is not a issued domain, skip." | |
5846 | fi | |
5847 | return 1 | |
5848 | fi | |
5849 | ||
5850 | if mv "$DOMAIN_CONF" "$_removed_conf"; then | |
68aea3af | 5851 | _info "$Le_Domain is removed, the key and cert files are in $(__green $DOMAIN_PATH)" |
78f0201d | 5852 | _info "You can remove them by yourself." |
5853 | return 0 | |
5854 | else | |
5855 | _err "Remove $Le_Domain failed." | |
5856 | return 1 | |
5857 | fi | |
5858 | } | |
5859 | ||
0c00e870 | 5860 | #domain vtype |
5861 | _deactivate() { | |
5862 | _d_domain="$1" | |
5863 | _d_type="$2" | |
5864 | _initpath | |
4c2a3841 | 5865 | |
d2cde379 | 5866 | if [ "$ACME_VERSION" = "2" ]; then |
5867 | _identifiers="{\"type\":\"dns\",\"value\":\"$_d_domain\"}" | |
5868 | if ! _send_signed_request "$ACME_NEW_ORDER" "{\"identifiers\": [$_identifiers]}"; then | |
5869 | _err "Can not get domain new order." | |
5870 | return 1 | |
5871 | fi | |
d0d74907 | 5872 | _authorizations_seg="$(echo "$response" | _egrep_o '"authorizations" *: *\[[^\]*\]' | cut -d '[' -f 2 | tr -d ']' | tr -d '"')" |
d2cde379 | 5873 | _debug2 _authorizations_seg "$_authorizations_seg" |
5874 | if [ -z "$_authorizations_seg" ]; then | |
5875 | _err "_authorizations_seg not found." | |
5876 | _clearup | |
5877 | _on_issue_err "$_post_hook" | |
5878 | return 1 | |
5879 | fi | |
4c2a3841 | 5880 | |
d2cde379 | 5881 | authzUri="$_authorizations_seg" |
5882 | _debug2 "authzUri" "$authzUri" | |
0483d841 | 5883 | if ! _send_signed_request "$authzUri"; then |
d2cde379 | 5884 | _err "get to authz error." |
263c38ca | 5885 | _err "_authorizations_seg" "$_authorizations_seg" |
5886 | _err "authzUri" "$authzUri" | |
d2cde379 | 5887 | _clearup |
5888 | _on_issue_err "$_post_hook" | |
5889 | return 1 | |
5890 | fi | |
4c2a3841 | 5891 | |
d2cde379 | 5892 | response="$(echo "$response" | _normalizeJson)" |
5893 | _debug2 response "$response" | |
5894 | _URL_NAME="url" | |
5895 | else | |
5896 | if ! __get_domain_new_authz "$_d_domain"; then | |
5897 | _err "Can not get domain new authz token." | |
5898 | return 1 | |
5899 | fi | |
5900 | ||
0712e989 | 5901 | authzUri="$(echo "$responseHeaders" | grep "^Location:" | _head_n 1 | cut -d ':' -f 2- | tr -d "\r\n")" |
d2cde379 | 5902 | _debug "authzUri" "$authzUri" |
5903 | if [ "$code" ] && [ ! "$code" = '201' ]; then | |
5904 | _err "new-authz error: $response" | |
5905 | return 1 | |
5906 | fi | |
5907 | _URL_NAME="uri" | |
14d7bfda | 5908 | fi |
0c00e870 | 5909 | |
cc8f2afc | 5910 | entries="$(echo "$response" | tr '][' '==' | _egrep_o "challenges\": *=[^=]*=" | tr '}{' '\n' | grep "\"status\": *\"valid\"")" |
14d7bfda | 5911 | if [ -z "$entries" ]; then |
5912 | _info "No valid entries found." | |
5913 | if [ -z "$thumbprint" ]; then | |
5914 | thumbprint="$(__calc_account_thumbprint)" | |
5915 | fi | |
5916 | _debug "Trigger validation." | |
d2cde379 | 5917 | vtype="$VTYPE_DNS" |
d0d74907 | 5918 | entry="$(echo "$response" | _egrep_o '[^\{]*"type":"'$vtype'"[^\}]*')" |
14d7bfda | 5919 | _debug entry "$entry" |
5920 | if [ -z "$entry" ]; then | |
5921 | _err "Error, can not get domain token $d" | |
0c00e870 | 5922 | return 1 |
5923 | fi | |
d0d74907 | 5924 | token="$(echo "$entry" | _egrep_o '"token":"[^"]*' | cut -d : -f 2 | tr -d '"')" |
14d7bfda | 5925 | _debug token "$token" |
4c2a3841 | 5926 | |
d0d74907 | 5927 | uri="$(echo "$entry" | _egrep_o "\"$_URL_NAME\":\"[^\"]*" | cut -d : -f 2,3 | tr -d '"')" |
14d7bfda | 5928 | _debug uri "$uri" |
5929 | ||
5930 | keyauthorization="$token.$thumbprint" | |
5931 | _debug keyauthorization "$keyauthorization" | |
5932 | __trigger_validation "$uri" "$keyauthorization" | |
5933 | ||
5934 | fi | |
5935 | ||
5936 | _d_i=0 | |
5937 | _d_max_retry=$(echo "$entries" | wc -l) | |
5938 | while [ "$_d_i" -lt "$_d_max_retry" ]; do | |
5939 | _info "Deactivate: $_d_domain" | |
5940 | _d_i="$(_math $_d_i + 1)" | |
5941 | entry="$(echo "$entries" | sed -n "${_d_i}p")" | |
0c00e870 | 5942 | _debug entry "$entry" |
4c2a3841 | 5943 | |
5944 | if [ -z "$entry" ]; then | |
fb2029e7 | 5945 | _info "No more valid entry found." |
0c00e870 | 5946 | break |
5947 | fi | |
4c2a3841 | 5948 | |
d0d74907 | 5949 | _vtype="$(echo "$entry" | _egrep_o '"type": *"[^"]*"' | cut -d : -f 2 | tr -d '"')" |
c4a375b3 | 5950 | _debug _vtype "$_vtype" |
0c00e870 | 5951 | _info "Found $_vtype" |
5952 | ||
199ca77c | 5953 | uri="$(echo "$entry" | _egrep_o "\"$_URL_NAME\":\"[^\"]*\"" | tr -d '" ' | cut -d : -f 2-)" |
c4a375b3 | 5954 | _debug uri "$uri" |
4c2a3841 | 5955 | |
5956 | if [ "$_d_type" ] && [ "$_d_type" != "$_vtype" ]; then | |
0c00e870 | 5957 | _info "Skip $_vtype" |
5958 | continue | |
5959 | fi | |
4c2a3841 | 5960 | |
0c00e870 | 5961 | _info "Deactivate: $_vtype" |
4c2a3841 | 5962 | |
d2cde379 | 5963 | if [ "$ACME_VERSION" = "2" ]; then |
5964 | _djson="{\"status\":\"deactivated\"}" | |
5965 | else | |
5966 | _djson="{\"resource\": \"authz\", \"status\":\"deactivated\"}" | |
5967 | fi | |
5968 | ||
5969 | if _send_signed_request "$authzUri" "$_djson" && _contains "$response" '"deactivated"'; then | |
14d7bfda | 5970 | _info "Deactivate: $_vtype success." |
5971 | else | |
0c00e870 | 5972 | _err "Can not deactivate $_vtype." |
14d7bfda | 5973 | break |
0c00e870 | 5974 | fi |
4c2a3841 | 5975 | |
0c00e870 | 5976 | done |
5977 | _debug "$_d_i" | |
14d7bfda | 5978 | if [ "$_d_i" -eq "$_d_max_retry" ]; then |
0c00e870 | 5979 | _info "Deactivated success!" |
5980 | else | |
5981 | _err "Deactivate failed." | |
5982 | fi | |
5983 | ||
5984 | } | |
5985 | ||
5986 | deactivate() { | |
3f4513b3 | 5987 | _d_domain_list="$1" |
0c00e870 | 5988 | _d_type="$2" |
5989 | _initpath | |
a3bdaa85 | 5990 | _initAPI |
3f4513b3 | 5991 | _debug _d_domain_list "$_d_domain_list" |
4c2a3841 | 5992 | if [ -z "$(echo $_d_domain_list | cut -d , -f 1)" ]; then |
2e87e64b | 5993 | _usage "Usage: $PROJECT_ENTRY --deactivate --domain <domain.tld> [--domain <domain2.tld> ...]" |
0c00e870 | 5994 | return 1 |
5995 | fi | |
4c2a3841 | 5996 | for _d_dm in $(echo "$_d_domain_list" | tr ',' ' '); do |
5997 | if [ -z "$_d_dm" ] || [ "$_d_dm" = "$NO_VALUE" ]; then | |
3f4513b3 | 5998 | continue |
5999 | fi | |
c4a375b3 | 6000 | if ! _deactivate "$_d_dm" "$_d_type"; then |
86c017ec | 6001 | return 1 |
6002 | fi | |
3f4513b3 | 6003 | done |
0c00e870 | 6004 | } |
6005 | ||
4c3b3608 | 6006 | # Detect profile file if not specified as environment variable |
6007 | _detect_profile() { | |
4c2a3841 | 6008 | if [ -n "$PROFILE" -a -f "$PROFILE" ]; then |
4c3b3608 | 6009 | echo "$PROFILE" |
6010 | return | |
6011 | fi | |
6012 | ||
4c3b3608 | 6013 | DETECTED_PROFILE='' |
4c3b3608 | 6014 | SHELLTYPE="$(basename "/$SHELL")" |
6015 | ||
4c2a3841 | 6016 | if [ "$SHELLTYPE" = "bash" ]; then |
6017 | if [ -f "$HOME/.bashrc" ]; then | |
4c3b3608 | 6018 | DETECTED_PROFILE="$HOME/.bashrc" |
4c2a3841 | 6019 | elif [ -f "$HOME/.bash_profile" ]; then |
4c3b3608 | 6020 | DETECTED_PROFILE="$HOME/.bash_profile" |
6021 | fi | |
4c2a3841 | 6022 | elif [ "$SHELLTYPE" = "zsh" ]; then |
4c3b3608 | 6023 | DETECTED_PROFILE="$HOME/.zshrc" |
6024 | fi | |
6025 | ||
4c2a3841 | 6026 | if [ -z "$DETECTED_PROFILE" ]; then |
6027 | if [ -f "$HOME/.profile" ]; then | |
4c3b3608 | 6028 | DETECTED_PROFILE="$HOME/.profile" |
4c2a3841 | 6029 | elif [ -f "$HOME/.bashrc" ]; then |
4c3b3608 | 6030 | DETECTED_PROFILE="$HOME/.bashrc" |
4c2a3841 | 6031 | elif [ -f "$HOME/.bash_profile" ]; then |
4c3b3608 | 6032 | DETECTED_PROFILE="$HOME/.bash_profile" |
4c2a3841 | 6033 | elif [ -f "$HOME/.zshrc" ]; then |
4c3b3608 | 6034 | DETECTED_PROFILE="$HOME/.zshrc" |
6035 | fi | |
6036 | fi | |
6037 | ||
1be222f6 | 6038 | echo "$DETECTED_PROFILE" |
4c3b3608 | 6039 | } |
6040 | ||
6041 | _initconf() { | |
6042 | _initpath | |
4c2a3841 | 6043 | if [ ! -f "$ACCOUNT_CONF_PATH" ]; then |
0ca5b799 | 6044 | echo " |
d404e92d | 6045 | |
d0871bda | 6046 | #LOG_FILE=\"$DEFAULT_LOG_FILE\" |
6b500036 | 6047 | #LOG_LEVEL=1 |
5ea6e9c9 | 6048 | |
251d1c5c | 6049 | #AUTO_UPGRADE=\"1\" |
89002ed2 | 6050 | |
569d6c55 | 6051 | #NO_TIMESTAMP=1 |
5b771039 | 6052 | |
d5ec5f80 | 6053 | " >"$ACCOUNT_CONF_PATH" |
4c3b3608 | 6054 | fi |
6055 | } | |
6056 | ||
c8e9a31e | 6057 | # nocron |
c60883ef | 6058 | _precheck() { |
c8e9a31e | 6059 | _nocron="$1" |
4c2a3841 | 6060 | |
6061 | if ! _exists "curl" && ! _exists "wget"; then | |
c60883ef | 6062 | _err "Please install curl or wget first, we need to access http resources." |
4c3b3608 | 6063 | return 1 |
6064 | fi | |
4c2a3841 | 6065 | |
6066 | if [ -z "$_nocron" ]; then | |
415f375c | 6067 | if ! _exists "crontab" && ! _exists "fcrontab"; then |
77f96b38 | 6068 | if _exists cygpath && _exists schtasks.exe; then |
6069 | _info "It seems you are on Windows, we will install Windows scheduler task." | |
6070 | else | |
6071 | _err "It is recommended to install crontab first. try to install 'cron, crontab, crontabs or vixie-cron'." | |
6072 | _err "We need to set cron job to renew the certs automatically." | |
6073 | _err "Otherwise, your certs will not be able to be renewed automatically." | |
6074 | if [ -z "$FORCE" ]; then | |
6075 | _err "Please add '--force' and try install again to go without crontab." | |
6076 | _err "./$PROJECT_ENTRY --install --force" | |
6077 | return 1 | |
6078 | fi | |
c8e9a31e | 6079 | fi |
77546ea5 | 6080 | fi |
4c3b3608 | 6081 | fi |
4c2a3841 | 6082 | |
d8ba26e6 | 6083 | if ! _exists "${ACME_OPENSSL_BIN:-openssl}"; then |
851fedf7 | 6084 | _err "Please install openssl first. ACME_OPENSSL_BIN=$ACME_OPENSSL_BIN" |
c60883ef | 6085 | _err "We need openssl to generate keys." |
4c3b3608 | 6086 | return 1 |
6087 | fi | |
4c2a3841 | 6088 | |
3794b5cb | 6089 | if ! _exists "socat"; then |
6090 | _err "It is recommended to install socat first." | |
6091 | _err "We use socat for standalone server if you use standalone mode." | |
c60883ef | 6092 | _err "If you don't use standalone mode, just ignore this warning." |
6093 | fi | |
4c2a3841 | 6094 | |
c60883ef | 6095 | return 0 |
6096 | } | |
6097 | ||
0a7c9364 | 6098 | _setShebang() { |
6099 | _file="$1" | |
6100 | _shebang="$2" | |
4c2a3841 | 6101 | if [ -z "$_shebang" ]; then |
43822d37 | 6102 | _usage "Usage: file shebang" |
0a7c9364 | 6103 | return 1 |
6104 | fi | |
6105 | cp "$_file" "$_file.tmp" | |
4c2a3841 | 6106 | echo "$_shebang" >"$_file" |
6107 | sed -n 2,99999p "$_file.tmp" >>"$_file" | |
6108 | rm -f "$_file.tmp" | |
0a7c9364 | 6109 | } |
6110 | ||
27dbe77f | 6111 | #confighome |
94dc5f33 | 6112 | _installalias() { |
27dbe77f | 6113 | _c_home="$1" |
94dc5f33 | 6114 | _initpath |
6115 | ||
6116 | _envfile="$LE_WORKING_DIR/$PROJECT_ENTRY.env" | |
4c2a3841 | 6117 | if [ "$_upgrading" ] && [ "$_upgrading" = "1" ]; then |
44edb2bd | 6118 | echo "$(cat "$_envfile")" | sed "s|^LE_WORKING_DIR.*$||" >"$_envfile" |
6119 | echo "$(cat "$_envfile")" | sed "s|^alias le.*$||" >"$_envfile" | |
6120 | echo "$(cat "$_envfile")" | sed "s|^alias le.sh.*$||" >"$_envfile" | |
94dc5f33 | 6121 | fi |
6122 | ||
27dbe77f | 6123 | if [ "$_c_home" ]; then |
be83a6a3 | 6124 | _c_entry=" --config-home '$_c_home'" |
27dbe77f | 6125 | fi |
6126 | ||
1786a5e5 | 6127 | _setopt "$_envfile" "export LE_WORKING_DIR" "=" "\"$LE_WORKING_DIR\"" |
f5b546b3 | 6128 | if [ "$_c_home" ]; then |
6129 | _setopt "$_envfile" "export LE_CONFIG_HOME" "=" "\"$LE_CONFIG_HOME\"" | |
d04434e3 | 6130 | else |
6131 | _sed_i "/^export LE_CONFIG_HOME/d" "$_envfile" | |
f5b546b3 | 6132 | fi |
be83a6a3 | 6133 | _setopt "$_envfile" "alias $PROJECT_ENTRY" "=" "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\"" |
94dc5f33 | 6134 | |
6135 | _profile="$(_detect_profile)" | |
4c2a3841 | 6136 | if [ "$_profile" ]; then |
94dc5f33 | 6137 | _debug "Found profile: $_profile" |
aba5c634 | 6138 | _info "Installing alias to '$_profile'" |
94dc5f33 | 6139 | _setopt "$_profile" ". \"$_envfile\"" |
6140 | _info "OK, Close and reopen your terminal to start using $PROJECT_NAME" | |
6141 | else | |
6142 | _info "No profile is found, you will need to go into $LE_WORKING_DIR to use $PROJECT_NAME" | |
6143 | fi | |
94dc5f33 | 6144 | |
6145 | #for csh | |
6146 | _cshfile="$LE_WORKING_DIR/$PROJECT_ENTRY.csh" | |
94dc5f33 | 6147 | _csh_profile="$HOME/.cshrc" |
4c2a3841 | 6148 | if [ -f "$_csh_profile" ]; then |
aba5c634 | 6149 | _info "Installing alias to '$_csh_profile'" |
6626371d | 6150 | _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\"" |
f5b546b3 | 6151 | if [ "$_c_home" ]; then |
6152 | _setopt "$_cshfile" "setenv LE_CONFIG_HOME" " " "\"$LE_CONFIG_HOME\"" | |
d04434e3 | 6153 | else |
6154 | _sed_i "/^setenv LE_CONFIG_HOME/d" "$_cshfile" | |
f5b546b3 | 6155 | fi |
be83a6a3 | 6156 | _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\"" |
4c2a3841 | 6157 | _setopt "$_csh_profile" "source \"$_cshfile\"" |
94dc5f33 | 6158 | fi |
4c2a3841 | 6159 | |
acafa585 | 6160 | #for tcsh |
6161 | _tcsh_profile="$HOME/.tcshrc" | |
4c2a3841 | 6162 | if [ -f "$_tcsh_profile" ]; then |
aba5c634 | 6163 | _info "Installing alias to '$_tcsh_profile'" |
acafa585 | 6164 | _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\"" |
f5b546b3 | 6165 | if [ "$_c_home" ]; then |
6166 | _setopt "$_cshfile" "setenv LE_CONFIG_HOME" " " "\"$LE_CONFIG_HOME\"" | |
6167 | fi | |
be83a6a3 | 6168 | _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\"" |
4c2a3841 | 6169 | _setopt "$_tcsh_profile" "source \"$_cshfile\"" |
acafa585 | 6170 | fi |
94dc5f33 | 6171 | |
6172 | } | |
6173 | ||
58c4eaaf | 6174 | # nocron confighome noprofile accountemail |
c60883ef | 6175 | install() { |
f3e4cea3 | 6176 | |
4c2a3841 | 6177 | if [ -z "$LE_WORKING_DIR" ]; then |
f3e4cea3 | 6178 | LE_WORKING_DIR="$DEFAULT_INSTALL_HOME" |
6179 | fi | |
4c2a3841 | 6180 | |
c8e9a31e | 6181 | _nocron="$1" |
27dbe77f | 6182 | _c_home="$2" |
86ef0a26 | 6183 | _noprofile="$3" |
58c4eaaf | 6184 | _accountemail="$4" |
6185 | ||
4c2a3841 | 6186 | if ! _initpath; then |
c60883ef | 6187 | _err "Install failed." |
4c3b3608 | 6188 | return 1 |
6189 | fi | |
4c2a3841 | 6190 | if [ "$_nocron" ]; then |
52677b0a | 6191 | _debug "Skip install cron job" |
6192 | fi | |
4c2a3841 | 6193 | |
bd04638d | 6194 | if [ "$_ACME_IN_CRON" != "1" ]; then |
4356eefb | 6195 | if ! _precheck "$_nocron"; then |
6196 | _err "Pre-check failed, can not install." | |
6197 | return 1 | |
6198 | fi | |
4c3b3608 | 6199 | fi |
4c2a3841 | 6200 | |
8e845d9f | 6201 | if [ -z "$_c_home" ] && [ "$LE_CONFIG_HOME" != "$LE_WORKING_DIR" ]; then |
6202 | _info "Using config home: $LE_CONFIG_HOME" | |
6203 | _c_home="$LE_CONFIG_HOME" | |
6204 | fi | |
6205 | ||
6cc11ffb | 6206 | #convert from le |
4c2a3841 | 6207 | if [ -d "$HOME/.le" ]; then |
6208 | for envfile in "le.env" "le.sh.env"; do | |
6209 | if [ -f "$HOME/.le/$envfile" ]; then | |
6210 | if grep "le.sh" "$HOME/.le/$envfile" >/dev/null; then | |
6211 | _upgrading="1" | |
6212 | _info "You are upgrading from le.sh" | |
6213 | _info "Renaming \"$HOME/.le\" to $LE_WORKING_DIR" | |
6214 | mv "$HOME/.le" "$LE_WORKING_DIR" | |
6215 | mv "$LE_WORKING_DIR/$envfile" "$LE_WORKING_DIR/$PROJECT_ENTRY.env" | |
6216 | break | |
6cc11ffb | 6217 | fi |
6218 | fi | |
6219 | done | |
6220 | fi | |
6221 | ||
4c3b3608 | 6222 | _info "Installing to $LE_WORKING_DIR" |
635695ec | 6223 | |
d04434e3 | 6224 | if [ ! -d "$LE_WORKING_DIR" ]; then |
6225 | if ! mkdir -p "$LE_WORKING_DIR"; then | |
6226 | _err "Can not create working dir: $LE_WORKING_DIR" | |
6227 | return 1 | |
6228 | fi | |
6229 | ||
6230 | chmod 700 "$LE_WORKING_DIR" | |
4a0f23e2 | 6231 | fi |
4c2a3841 | 6232 | |
d04434e3 | 6233 | if [ ! -d "$LE_CONFIG_HOME" ]; then |
6234 | if ! mkdir -p "$LE_CONFIG_HOME"; then | |
6235 | _err "Can not create config dir: $LE_CONFIG_HOME" | |
6236 | return 1 | |
6237 | fi | |
762978f8 | 6238 | |
d04434e3 | 6239 | chmod 700 "$LE_CONFIG_HOME" |
27dbe77f | 6240 | fi |
6241 | ||
d5ec5f80 | 6242 | cp "$PROJECT_ENTRY" "$LE_WORKING_DIR/" && chmod +x "$LE_WORKING_DIR/$PROJECT_ENTRY" |
4c3b3608 | 6243 | |
4c2a3841 | 6244 | if [ "$?" != "0" ]; then |
a7b7355d | 6245 | _err "Install failed, can not copy $PROJECT_ENTRY" |
4c3b3608 | 6246 | return 1 |
6247 | fi | |
6248 | ||
a7b7355d | 6249 | _info "Installed to $LE_WORKING_DIR/$PROJECT_ENTRY" |
4c3b3608 | 6250 | |
bd04638d | 6251 | if [ "$_ACME_IN_CRON" != "1" ] && [ -z "$_noprofile" ]; then |
4356eefb | 6252 | _installalias "$_c_home" |
6253 | fi | |
4c3b3608 | 6254 | |
4c2a3841 | 6255 | for subf in $_SUB_FOLDERS; do |
6256 | if [ -d "$subf" ]; then | |
d5ec5f80 | 6257 | mkdir -p "$LE_WORKING_DIR/$subf" |
6258 | cp "$subf"/* "$LE_WORKING_DIR"/"$subf"/ | |
a61fe418 | 6259 | fi |
6260 | done | |
6261 | ||
4c2a3841 | 6262 | if [ ! -f "$ACCOUNT_CONF_PATH" ]; then |
4c3b3608 | 6263 | _initconf |
6264 | fi | |
6cc11ffb | 6265 | |
4c2a3841 | 6266 | if [ "$_DEFAULT_ACCOUNT_CONF_PATH" != "$ACCOUNT_CONF_PATH" ]; then |
635695ec | 6267 | _setopt "$_DEFAULT_ACCOUNT_CONF_PATH" "ACCOUNT_CONF_PATH" "=" "\"$ACCOUNT_CONF_PATH\"" |
6cc11ffb | 6268 | fi |
6269 | ||
4c2a3841 | 6270 | if [ "$_DEFAULT_CERT_HOME" != "$CERT_HOME" ]; then |
b2817897 | 6271 | _saveaccountconf "CERT_HOME" "$CERT_HOME" |
6272 | fi | |
6273 | ||
4c2a3841 | 6274 | if [ "$_DEFAULT_ACCOUNT_KEY_PATH" != "$ACCOUNT_KEY_PATH" ]; then |
b2817897 | 6275 | _saveaccountconf "ACCOUNT_KEY_PATH" "$ACCOUNT_KEY_PATH" |
6276 | fi | |
4c2a3841 | 6277 | |
6278 | if [ -z "$_nocron" ]; then | |
27dbe77f | 6279 | installcronjob "$_c_home" |
c8e9a31e | 6280 | fi |
0a7c9364 | 6281 | |
4c2a3841 | 6282 | if [ -z "$NO_DETECT_SH" ]; then |
641989fd | 6283 | #Modify shebang |
4c2a3841 | 6284 | if _exists bash; then |
694af4ae | 6285 | _bash_path="$(bash -c "command -v bash 2>/dev/null")" |
6286 | if [ -z "$_bash_path" ]; then | |
6287 | _bash_path="$(bash -c 'echo $SHELL')" | |
6288 | fi | |
6289 | fi | |
6290 | if [ "$_bash_path" ]; then | |
329174b6 | 6291 | _info "Good, bash is found, so change the shebang to use bash as preferred." |
694af4ae | 6292 | _shebang='#!'"$_bash_path" |
641989fd | 6293 | _setShebang "$LE_WORKING_DIR/$PROJECT_ENTRY" "$_shebang" |
4c2a3841 | 6294 | for subf in $_SUB_FOLDERS; do |
6295 | if [ -d "$LE_WORKING_DIR/$subf" ]; then | |
6296 | for _apifile in "$LE_WORKING_DIR/$subf/"*.sh; do | |
a61fe418 | 6297 | _setShebang "$_apifile" "$_shebang" |
6298 | done | |
6299 | fi | |
6300 | done | |
0a7c9364 | 6301 | fi |
6302 | fi | |
6303 | ||
58c4eaaf | 6304 | if [ "$_accountemail" ]; then |
6305 | _saveaccountconf "ACCOUNT_EMAIL" "$_accountemail" | |
6306 | fi | |
6307 | ||
4c3b3608 | 6308 | _info OK |
6309 | } | |
6310 | ||
52677b0a | 6311 | # nocron |
4c3b3608 | 6312 | uninstall() { |
52677b0a | 6313 | _nocron="$1" |
4c2a3841 | 6314 | if [ -z "$_nocron" ]; then |
52677b0a | 6315 | uninstallcronjob |
6316 | fi | |
4c3b3608 | 6317 | _initpath |
6318 | ||
9aa3be7f | 6319 | _uninstallalias |
4c2a3841 | 6320 | |
d5ec5f80 | 6321 | rm -f "$LE_WORKING_DIR/$PROJECT_ENTRY" |
f5b546b3 | 6322 | _info "The keys and certs are in \"$(__green "$LE_CONFIG_HOME")\", you can remove them by yourself." |
9aa3be7f | 6323 | |
6324 | } | |
6325 | ||
6326 | _uninstallalias() { | |
6327 | _initpath | |
6328 | ||
4c3b3608 | 6329 | _profile="$(_detect_profile)" |
4c2a3841 | 6330 | if [ "$_profile" ]; then |
9aa3be7f | 6331 | _info "Uninstalling alias from: '$_profile'" |
d5ec5f80 | 6332 | text="$(cat "$_profile")" |
4c2a3841 | 6333 | echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.env\"$||" >"$_profile" |
4c3b3608 | 6334 | fi |
6335 | ||
94dc5f33 | 6336 | _csh_profile="$HOME/.cshrc" |
4c2a3841 | 6337 | if [ -f "$_csh_profile" ]; then |
9aa3be7f | 6338 | _info "Uninstalling alias from: '$_csh_profile'" |
d5ec5f80 | 6339 | text="$(cat "$_csh_profile")" |
4c2a3841 | 6340 | echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" >"$_csh_profile" |
94dc5f33 | 6341 | fi |
4c2a3841 | 6342 | |
acafa585 | 6343 | _tcsh_profile="$HOME/.tcshrc" |
4c2a3841 | 6344 | if [ -f "$_tcsh_profile" ]; then |
9aa3be7f | 6345 | _info "Uninstalling alias from: '$_csh_profile'" |
d5ec5f80 | 6346 | text="$(cat "$_tcsh_profile")" |
4c2a3841 | 6347 | echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" >"$_tcsh_profile" |
acafa585 | 6348 | fi |
4c3b3608 | 6349 | |
6350 | } | |
6351 | ||
6352 | cron() { | |
bd04638d | 6353 | export _ACME_IN_CRON=1 |
89002ed2 | 6354 | _initpath |
d8ba26e6 | 6355 | _info "$(__green "===Starting cron===")" |
4c2a3841 | 6356 | if [ "$AUTO_UPGRADE" = "1" ]; then |
89002ed2 | 6357 | export LE_WORKING_DIR |
6358 | ( | |
4c2a3841 | 6359 | if ! upgrade; then |
6360 | _err "Cron:Upgrade failed!" | |
6361 | return 1 | |
6362 | fi | |
89002ed2 | 6363 | ) |
d5ec5f80 | 6364 | . "$LE_WORKING_DIR/$PROJECT_ENTRY" >/dev/null |
1ab63043 | 6365 | |
4c2a3841 | 6366 | if [ -t 1 ]; then |
1ab63043 | 6367 | __INTERACTIVE="1" |
6368 | fi | |
4c2a3841 | 6369 | |
89002ed2 | 6370 | _info "Auto upgraded to: $VER" |
6371 | fi | |
4c3b3608 | 6372 | renewAll |
cc179731 | 6373 | _ret="$?" |
bd04638d | 6374 | _ACME_IN_CRON="" |
d8ba26e6 | 6375 | _info "$(__green "===End cron===")" |
0ba95a3d | 6376 | exit $_ret |
4c3b3608 | 6377 | } |
6378 | ||
6379 | version() { | |
a63b05a9 | 6380 | echo "$PROJECT" |
6381 | echo "v$VER" | |
4c3b3608 | 6382 | } |
6383 | ||
b50e701c | 6384 | # subject content hooks code |
6385 | _send_notify() { | |
6386 | _nsubject="$1" | |
6387 | _ncontent="$2" | |
6388 | _nhooks="$3" | |
6389 | _nerror="$4" | |
6390 | ||
6391 | if [ "$NOTIFY_LEVEL" = "$NOTIFY_LEVEL_DISABLE" ]; then | |
6392 | _debug "The NOTIFY_LEVEL is $NOTIFY_LEVEL, disabled, just return." | |
6393 | return 0 | |
6394 | fi | |
6395 | ||
6396 | if [ -z "$_nhooks" ]; then | |
6397 | _debug "The NOTIFY_HOOK is empty, just return." | |
6398 | return 0 | |
6399 | fi | |
6400 | ||
6401 | _send_err=0 | |
6402 | for _n_hook in $(echo "$_nhooks" | tr ',' " "); do | |
6403 | _n_hook_file="$(_findHook "" $_SUB_FOLDER_NOTIFY "$_n_hook")" | |
143eac09 | 6404 | _info "Sending via: $_n_hook" |
6405 | _debug "Found $_n_hook_file for $_n_hook" | |
75191e71 | 6406 | if [ -z "$_n_hook_file" ]; then |
6407 | _err "Can not find the hook file for $_n_hook" | |
6408 | continue | |
6409 | fi | |
b50e701c | 6410 | if ! ( |
6411 | if ! . "$_n_hook_file"; then | |
6412 | _err "Load file $_n_hook_file error. Please check your api file and try again." | |
6413 | return 1 | |
6414 | fi | |
6415 | ||
6416 | d_command="${_n_hook}_send" | |
6417 | if ! _exists "$d_command"; then | |
6418 | _err "It seems that your api file is not correct, it must have a function named: $d_command" | |
6419 | return 1 | |
6420 | fi | |
6421 | ||
6422 | if ! $d_command "$_nsubject" "$_ncontent" "$_nerror"; then | |
6423 | _err "Error send message by $d_command" | |
6424 | return 1 | |
6425 | fi | |
6426 | ||
6427 | return 0 | |
6428 | ); then | |
6429 | _err "Set $_n_hook_file error." | |
6430 | _send_err=1 | |
6431 | else | |
6432 | _info "$_n_hook $(__green Success)" | |
6433 | fi | |
6434 | done | |
6435 | return $_send_err | |
6436 | ||
6437 | } | |
6438 | ||
6439 | # hook | |
6440 | _set_notify_hook() { | |
6441 | _nhooks="$1" | |
6442 | ||
5698bec6 | 6443 | _test_subject="Hello, this is a notification from $PROJECT_NAME" |
143eac09 | 6444 | _test_content="If you receive this message, your notification works." |
b50e701c | 6445 | |
6446 | _send_notify "$_test_subject" "$_test_content" "$_nhooks" 0 | |
6447 | ||
6448 | } | |
6449 | ||
6450 | #[hook] [level] [mode] | |
6451 | setnotify() { | |
6452 | _nhook="$1" | |
6453 | _nlevel="$2" | |
6454 | _nmode="$3" | |
6455 | ||
6456 | _initpath | |
6457 | ||
6458 | if [ -z "$_nhook$_nlevel$_nmode" ]; then | |
2e87e64b | 6459 | _usage "Usage: $PROJECT_ENTRY --set-notify [--notify-hook <hookname>] [--notify-level <0|1|2|3>] [--notify-mode <0|1>]" |
b50e701c | 6460 | _usage "$_NOTIFY_WIKI" |
6461 | return 1 | |
6462 | fi | |
6463 | ||
6464 | if [ "$_nlevel" ]; then | |
6465 | _info "Set notify level to: $_nlevel" | |
6466 | export "NOTIFY_LEVEL=$_nlevel" | |
6467 | _saveaccountconf "NOTIFY_LEVEL" "$NOTIFY_LEVEL" | |
6468 | fi | |
6469 | ||
6470 | if [ "$_nmode" ]; then | |
6471 | _info "Set notify mode to: $_nmode" | |
6472 | export "NOTIFY_MODE=$_nmode" | |
6473 | _saveaccountconf "NOTIFY_MODE" "$NOTIFY_MODE" | |
6474 | fi | |
6475 | ||
6476 | if [ "$_nhook" ]; then | |
6477 | _info "Set notify hook to: $_nhook" | |
6478 | if [ "$_nhook" = "$NO_VALUE" ]; then | |
6479 | _info "Clear notify hook" | |
6480 | _clearaccountconf "NOTIFY_HOOK" | |
6481 | else | |
6482 | if _set_notify_hook "$_nhook"; then | |
6483 | export NOTIFY_HOOK="$_nhook" | |
6484 | _saveaccountconf "NOTIFY_HOOK" "$NOTIFY_HOOK" | |
6485 | return 0 | |
6486 | else | |
6487 | _err "Can not set notify hook to: $_nhook" | |
6488 | return 1 | |
6489 | fi | |
6490 | fi | |
6491 | fi | |
6492 | ||
6493 | } | |
6494 | ||
4c3b3608 | 6495 | showhelp() { |
d0871bda | 6496 | _initpath |
4c3b3608 | 6497 | version |
2e87e64b | 6498 | echo "Usage: $PROJECT_ENTRY <command> ... [parameters ...] |
a63b05a9 | 6499 | Commands: |
c0fbe823 CE |
6500 | -h, --help Show this help message. |
6501 | -v, --version Show version info. | |
a7b7355d | 6502 | --install Install $PROJECT_NAME to your system. |
6503 | --uninstall Uninstall $PROJECT_NAME, and uninstall the cron job. | |
d8beaf72 | 6504 | --upgrade Upgrade $PROJECT_NAME to the latest code from $PROJECT. |
a63b05a9 | 6505 | --issue Issue a cert. |
a61fe418 | 6506 | --deploy Deploy the cert to your server. |
d81369d6 | 6507 | -i, --install-cert Install the issued cert to apache/nginx or any other server. |
c0fbe823 | 6508 | -r, --renew Renew a cert. |
27dbe77f | 6509 | --renew-all Renew all the certs. |
a63b05a9 | 6510 | --revoke Revoke a cert. |
47b49f1b | 6511 | --remove Remove the cert from list of certs known to $PROJECT_NAME. |
10afcaca | 6512 | --list List all the certs. |
58150f5d | 6513 | --to-pkcs12 Export the certificate and key to a pfx file. |
d81369d6 | 6514 | --to-pkcs8 Convert to pkcs8 format. |
7decf768 CE |
6515 | --sign-csr Issue a cert from an existing csr. |
6516 | --show-csr Show the content of a csr. | |
6517 | -ccr, --create-csr Create CSR, professional use. | |
6518 | --create-domain-key Create an domain private key, professional use. | |
27dbe77f | 6519 | --update-account Update account info. |
6520 | --register-account Register account key. | |
422dd1fa | 6521 | --deactivate-account Deactivate the account. |
0984585d | 6522 | --create-account-key Create an account private key, professional use. |
7decf768 CE |
6523 | --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. |
6524 | --uninstall-cronjob Uninstall the cron job. The 'uninstall' command can do this automatically. | |
6525 | --cron Run cron job to renew all the certs. | |
b50e701c | 6526 | --set-notify Set the cron notification hook, level or mode. |
7decf768 | 6527 | --deactivate Deactivate the domain authz, professional use. |
8d0e4851 | 6528 | --set-default-ca Used with '--server', Set the default CA to use. |
6529 | See: $_SERVER_WIKI | |
b50e701c | 6530 | |
3c07f57a | 6531 | |
a63b05a9 | 6532 | Parameters: |
c0fbe823 CE |
6533 | -d, --domain <domain.tld> Specifies a domain, used to issue, renew or revoke etc. |
6534 | --challenge-alias <domain.tld> The challenge domain alias for DNS alias mode. | |
e3ebd582 | 6535 | See: $_DNS_ALIAS_WIKI |
a48c22d1 | 6536 | |
c0fbe823 | 6537 | --domain-alias <domain.tld> The domain alias for DNS alias mode. |
e3ebd582 | 6538 | See: $_DNS_ALIAS_WIKI |
a48c22d1 | 6539 | |
c0fbe823 | 6540 | --preferred-chain <chain> If the CA offers multiple certificate chains, prefer the chain with an issuer matching this Subject Common Name. |
e3ebd582 | 6541 | If no match, the default offered chain will be used. (default: empty) |
6542 | See: $_PREFERRED_CHAIN_WIKI | |
a48c22d1 | 6543 | |
c0fbe823 CE |
6544 | -f, --force Force install, force cert renewal or override sudo restrictions. |
6545 | --staging, --test Use staging server, for testing. | |
6546 | --debug [0|1|2|3] Output debug info. Defaults to 1 if argument is omitted. | |
e3ebd582 | 6547 | --output-insecure Output all the sensitive messages. |
6548 | By default all the credentials/sensitive messages are hidden from the output/debug/log for security. | |
b086afb2 | 6549 | -w, --webroot <directory> Specifies the web root folder for web root mode. |
a63b05a9 | 6550 | --standalone Use standalone mode. |
08681f4a | 6551 | --alpn Use standalone alpn mode. |
e3ebd582 | 6552 | --stateless Use stateless mode. |
6553 | See: $_STATELESS_WIKI | |
a48c22d1 | 6554 | |
a63b05a9 | 6555 | --apache Use apache mode. |
c0fbe823 | 6556 | --dns [dns_hook] Use dns manual mode or dns api. Defaults to manual mode when argument is omitted. |
e3ebd582 | 6557 | See: $_DNS_API_WIKI |
a48c22d1 | 6558 | |
c0fbe823 | 6559 | --dnssleep <seconds> The time in seconds to wait for all the txt records to propagate in dns api mode. |
e3ebd582 | 6560 | It's not necessary to use this by default, $PROJECT_NAME polls dns status by DOH automatically. |
c0fbe823 CE |
6561 | -k, --keylength <bits> Specifies the domain key length: 2048, 3072, 4096, 8192 or ec-256, ec-384, ec-521. |
6562 | -ak, --accountkeylength <bits> Specifies the account key length: 2048, 3072, 4096 | |
b086afb2 | 6563 | --log [file] Specifies the log file. Defaults to \"$DEFAULT_LOG_FILE\" if argument is omitted. |
c0fbe823 CE |
6564 | --log-level <1|2> Specifies the log level, default is 1. |
6565 | --syslog <0|3|6|7> Syslog level, 0: disable syslog, 3: error, 6: info, 7: debug. | |
6566 | --eab-kid <eab_key_id> Key Identifier for External Account Binding. | |
6567 | --eab-hmac-key <eab_hmac_key> HMAC key for External Account Binding. | |
2d5f1438 OB |
6568 | |
6569 | ||
7903fcb4 | 6570 | These parameters are to install the cert to nginx/apache or any other server after issue/renew a cert: |
3c07f57a | 6571 | |
b086afb2 CE |
6572 | --cert-file <file> Path to copy the cert file to after issue/renew.. |
6573 | --key-file <file> Path to copy the key file to after issue/renew. | |
6574 | --ca-file <file> Path to copy the intermediate cert file to after issue/renew. | |
6575 | --fullchain-file <file> Path to copy the fullchain cert file to after issue/renew. | |
c0fbe823 | 6576 | --reloadcmd <command> Command to execute after issue/renew to reload the server. |
a63b05a9 | 6577 | |
c0fbe823 | 6578 | --server <server_uri> ACME Directory Resource URI. (default: $DEFAULT_CA) |
e3ebd582 | 6579 | See: $_SERVER_WIKI |
6580 | ||
c0fbe823 CE |
6581 | --accountconf <file> Specifies a customized account config file. |
6582 | --home <directory> Specifies the home dir for $PROJECT_NAME. | |
6583 | --cert-home <directory> Specifies the home dir to save all the certs, only valid for '--install' command. | |
6584 | --config-home <directory> Specifies the home dir to save all the configurations. | |
6585 | --useragent <string> Specifies the user agent string. it will be saved for future use too. | |
58c4eaaf | 6586 | -m, --email <email> Specifies the account email, only valid for the '--install' and '--update-account' command. |
c0fbe823 CE |
6587 | --accountkey <file> Specifies the account key path, only valid for the '--install' command. |
6588 | --days <ndays> Specifies the days to renew the cert when using '--issue' command. The default value is $DEFAULT_RENEW days. | |
6589 | --httpport <port> Specifies the standalone listening port. Only valid if the server is behind a reverse proxy or load balancer. | |
6590 | --tlsport <port> Specifies the standalone tls listening port. Only valid if the server is behind a reverse proxy or load balancer. | |
6591 | --local-address <ip> Specifies the standalone/tls server listening address, in case you have multiple ip addresses. | |
dcf4f8f6 | 6592 | --listraw Only used for '--list' command, list the certs in raw format. |
d81369d6 | 6593 | -se, --stop-renew-on-error Only valid for '--renew-all' command. Stop if one cert has error in renewal. |
13d7cae9 | 6594 | --insecure Do not check the server certificate, in some devices, the api server's certificate may not be trusted. |
c0fbe823 CE |
6595 | --ca-bundle <file> Specifies the path to the CA certificate bundle to verify api server's certificate. |
6596 | --ca-path <directory> Specifies directory containing CA certificates in PEM format, used by wget or curl. | |
58c4eaaf | 6597 | --no-cron Only valid for '--install' command, which means: do not install the default cron job. |
e3ebd582 | 6598 | In this case, the certs will not be renewed automatically. |
58c4eaaf | 6599 | --no-profile Only valid for '--install' command, which means: do not install aliases to user profile. |
2910be82 | 6600 | --no-color Do not output color text. |
e32b3aac | 6601 | --force-color Force output of color text. Useful for non-interactive use with the aha tool for HTML E-Mails. |
b67d663a | 6602 | --ecc Specifies to use the ECC cert. Valid for '--install-cert', '--renew', '--revoke', '--to-pkcs12' and '--create-csr' |
c0fbe823 CE |
6603 | --csr <file> Specifies the input csr. |
6604 | --pre-hook <command> Command to be run before obtaining any certificates. | |
6605 | --post-hook <command> Command to be run after attempting to obtain/renew certificates. Runs regardless of whether obtain/renew succeeded or failed. | |
6606 | --renew-hook <command> Command to be run after each successfully renewed certificate. | |
6607 | --deploy-hook <hookname> The hook file to deploy cert | |
6608 | --ocsp, --ocsp-must-staple Generate OCSP-Must-Staple extension. | |
6609 | --always-force-new-domain-key Generate new domain key on renewal. Otherwise, the domain key is not changed by default. | |
6610 | --auto-upgrade [0|1] Valid for '--upgrade' command, indicating whether to upgrade automatically in future. Defaults to 1 if argument is omitted. | |
6ae0f7f5 | 6611 | --listen-v4 Force standalone/tls server to listen at ipv4. |
6612 | --listen-v6 Force standalone/tls server to listen at ipv6. | |
c0fbe823 | 6613 | --openssl-bin <file> Specifies a custom openssl bin location. |
9b124070 | 6614 | --use-wget Force to use wget, if you have both curl and wget installed. |
07fdb087 | 6615 | --yes-I-know-dns-manual-mode-enough-go-ahead-please Force use of dns manual mode. |
e3ebd582 | 6616 | See: $_DNS_MANUAL_WIKI |
a48c22d1 | 6617 | |
c0fbe823 CE |
6618 | -b, --branch <branch> Only valid for '--upgrade' command, specifies the branch name to upgrade to. |
6619 | --notify-level <0|1|2|3> Set the notification level: Default value is $NOTIFY_LEVEL_DEFAULT. | |
6620 | 0: disabled, no notification will be sent. | |
6621 | 1: send notifications only when there is an error. | |
6622 | 2: send notifications when a cert is successfully renewed, or there is an error. | |
6623 | 3: send notifications when a cert is skipped, renewed, or error. | |
6624 | --notify-mode <0|1> Set notification mode. Default value is $NOTIFY_MODE_DEFAULT. | |
6625 | 0: Bulk mode. Send all the domain's notifications in one message(mail). | |
6626 | 1: Cert mode. Send a message for every single cert. | |
6627 | --notify-hook <hookname> Set the notify hook | |
6628 | --revoke-reason <0-10> The reason for revocation, can be used in conjunction with the '--revoke' command. | |
e3ebd582 | 6629 | See: $_REVOKE_WIKI |
6630 | ||
dd6c5c9e CE |
6631 | --password <password> Add a password to exported pfx file. Use with --to-pkcs12. |
6632 | ||
b50e701c | 6633 | |
6634 | " | |
4c3b3608 | 6635 | } |
6636 | ||
58c4eaaf | 6637 | installOnline() { |
4a0f23e2 | 6638 | _info "Installing from online archive." |
58c4eaaf | 6639 | |
6640 | _branch="$BRANCH" | |
6641 | if [ -z "$_branch" ]; then | |
6642 | _branch="master" | |
4a0f23e2 | 6643 | fi |
a8df88ab | 6644 | |
58c4eaaf | 6645 | target="$PROJECT/archive/$_branch.tar.gz" |
4a0f23e2 | 6646 | _info "Downloading $target" |
58c4eaaf | 6647 | localname="$_branch.tar.gz" |
4c2a3841 | 6648 | if ! _get "$target" >$localname; then |
df9547ae | 6649 | _err "Download error." |
4a0f23e2 | 6650 | return 1 |
6651 | fi | |
0bbe6eef | 6652 | ( |
4c2a3841 | 6653 | _info "Extracting $localname" |
3a3b0dd5 | 6654 | if ! (tar xzf $localname || gtar xzf $localname); then |
6655 | _err "Extraction error." | |
6656 | exit 1 | |
6657 | fi | |
4c2a3841 | 6658 | |
58c4eaaf | 6659 | cd "$PROJECT_NAME-$_branch" |
4c2a3841 | 6660 | chmod +x $PROJECT_ENTRY |
58c4eaaf | 6661 | if ./$PROJECT_ENTRY --install "$@"; then |
4c2a3841 | 6662 | _info "Install success!" |
ac3667c7 | 6663 | _initpath |
cb7e3857 | 6664 | _saveaccountconf "UPGRADE_HASH" "$(_getUpgradeHash)" |
4c2a3841 | 6665 | fi |
6666 | ||
6667 | cd .. | |
6668 | ||
58c4eaaf | 6669 | rm -rf "$PROJECT_NAME-$_branch" |
4c2a3841 | 6670 | rm -f "$localname" |
0bbe6eef | 6671 | ) |
4a0f23e2 | 6672 | } |
6673 | ||
cb7e3857 OB |
6674 | _getRepoHash() { |
6675 | _hash_path=$1 | |
6676 | shift | |
6677 | _hash_url="https://api.github.com/repos/acmesh-official/$PROJECT_NAME/git/refs/$_hash_path" | |
6678 | _get $_hash_url | tr -d "\r\n" | tr '{},' '\n' | grep '"sha":' | cut -d '"' -f 4 | |
6679 | } | |
6680 | ||
6681 | _getUpgradeHash() { | |
f716f606 | 6682 | _b="$BRANCH" |
6683 | if [ -z "$_b" ]; then | |
6684 | _b="master" | |
6685 | fi | |
cb7e3857 OB |
6686 | _hash=$(_getRepoHash "heads/$_b") |
6687 | if [ -z "$_hash" ]; then _hash=$(_getRepoHash "tags/$_b"); fi | |
6688 | echo $_hash | |
7a3c61b7 | 6689 | } |
6690 | ||
52677b0a | 6691 | upgrade() { |
6692 | if ( | |
267f283a | 6693 | _initpath |
cb7e3857 | 6694 | [ -z "$FORCE" ] && [ "$(_getUpgradeHash)" = "$(_readaccountconf "UPGRADE_HASH")" ] && _info "Already uptodate!" && exit 0 |
267f283a | 6695 | export LE_WORKING_DIR |
d0b748a4 | 6696 | cd "$LE_WORKING_DIR" |
58c4eaaf | 6697 | installOnline "--nocron" "--noprofile" |
4c2a3841 | 6698 | ); then |
52677b0a | 6699 | _info "Upgrade success!" |
096d8992 | 6700 | exit 0 |
52677b0a | 6701 | else |
6702 | _err "Upgrade failed!" | |
096d8992 | 6703 | exit 1 |
52677b0a | 6704 | fi |
6705 | } | |
a63b05a9 | 6706 | |
5ea6e9c9 | 6707 | _processAccountConf() { |
4c2a3841 | 6708 | if [ "$_useragent" ]; then |
5ea6e9c9 | 6709 | _saveaccountconf "USER_AGENT" "$_useragent" |
4c2a3841 | 6710 | elif [ "$USER_AGENT" ] && [ "$USER_AGENT" != "$DEFAULT_USER_AGENT" ]; then |
d0871bda | 6711 | _saveaccountconf "USER_AGENT" "$USER_AGENT" |
5ea6e9c9 | 6712 | fi |
4c2a3841 | 6713 | |
a746139c | 6714 | if [ "$_openssl_bin" ]; then |
851fedf7 | 6715 | _saveaccountconf "ACME_OPENSSL_BIN" "$_openssl_bin" |
6716 | elif [ "$ACME_OPENSSL_BIN" ] && [ "$ACME_OPENSSL_BIN" != "$DEFAULT_OPENSSL_BIN" ]; then | |
6717 | _saveaccountconf "ACME_OPENSSL_BIN" "$ACME_OPENSSL_BIN" | |
a746139c | 6718 | fi |
6719 | ||
4c2a3841 | 6720 | if [ "$_auto_upgrade" ]; then |
6bf281f9 | 6721 | _saveaccountconf "AUTO_UPGRADE" "$_auto_upgrade" |
4c2a3841 | 6722 | elif [ "$AUTO_UPGRADE" ]; then |
6bf281f9 | 6723 | _saveaccountconf "AUTO_UPGRADE" "$AUTO_UPGRADE" |
6724 | fi | |
4c2a3841 | 6725 | |
9b124070 | 6726 | if [ "$_use_wget" ]; then |
6727 | _saveaccountconf "ACME_USE_WGET" "$_use_wget" | |
6728 | elif [ "$ACME_USE_WGET" ]; then | |
6729 | _saveaccountconf "ACME_USE_WGET" "$ACME_USE_WGET" | |
6730 | fi | |
6731 | ||
5ea6e9c9 | 6732 | } |
6733 | ||
5bdfdfef | 6734 | _checkSudo() { |
6735 | if [ "$SUDO_GID" ] && [ "$SUDO_COMMAND" ] && [ "$SUDO_USER" ] && [ "$SUDO_UID" ]; then | |
6736 | if [ "$SUDO_USER" = "root" ] && [ "$SUDO_UID" = "0" ]; then | |
6737 | #it's root using sudo, no matter it's using sudo or not, just fine | |
6738 | return 0 | |
6739 | fi | |
79ad0ff5 | 6740 | if [ -n "$SUDO_COMMAND" ]; then |
996f5337 | 6741 | #it's a normal user doing "sudo su", or `sudo -i` or `sudo -s`, or `sudo su acmeuser1` |
6742 | _endswith "$SUDO_COMMAND" /bin/su || _contains "$SUDO_COMMAND" "/bin/su " || grep "^$SUDO_COMMAND\$" /etc/shells >/dev/null 2>&1 | |
79ad0ff5 | 6743 | return $? |
5bdfdfef | 6744 | fi |
6745 | #otherwise | |
6746 | return 1 | |
6747 | fi | |
6748 | return 0 | |
6749 | } | |
6750 | ||
737e9e48 | 6751 | #server |
6752 | _selectServer() { | |
6753 | _server="$1" | |
6754 | _server_lower="$(echo "$_server" | _lower_case)" | |
6755 | _sindex=0 | |
6756 | for snames in $CA_NAMES; do | |
6757 | snames="$(echo "$snames" | _lower_case)" | |
6758 | _sindex="$(_math $_sindex + 1)" | |
6759 | _debug2 "_selectServer try snames" "$snames" | |
6760 | for sname in $(echo "$snames" | tr ',' ' '); do | |
6761 | if [ "$_server_lower" = "$sname" ]; then | |
6762 | _debug2 "_selectServer match $sname" | |
6763 | _serverdir="$(_getfield "$CA_SERVERS" $_sindex)" | |
6764 | _debug "Selected server: $_serverdir" | |
6765 | ACME_DIRECTORY="$_serverdir" | |
6766 | export ACME_DIRECTORY | |
6767 | return | |
6768 | fi | |
6769 | done | |
6770 | done | |
6771 | ACME_DIRECTORY="$_server" | |
6772 | export ACME_DIRECTORY | |
6773 | } | |
6774 | ||
269847d1 | 6775 | #url |
6776 | _getCAShortName() { | |
6777 | caurl="$1" | |
95ef046d | 6778 | if [ -z "$caurl" ]; then |
6779 | caurl="$DEFAULT_CA" | |
6780 | fi | |
269847d1 | 6781 | caurl_lower="$(echo $caurl | _lower_case)" |
6782 | _sindex=0 | |
6783 | for surl in $(echo "$CA_SERVERS" | _lower_case | tr , ' '); do | |
6784 | _sindex="$(_math $_sindex + 1)" | |
6785 | if [ "$caurl_lower" = "$surl" ]; then | |
6786 | _nindex=0 | |
6787 | for snames in $CA_NAMES; do | |
6788 | _nindex="$(_math $_nindex + 1)" | |
6789 | if [ $_nindex -ge $_sindex ]; then | |
6790 | _getfield "$snames" 1 | |
6791 | return | |
6792 | fi | |
6793 | done | |
6794 | fi | |
6795 | done | |
6796 | echo "$caurl" | |
6797 | } | |
6798 | ||
737e9e48 | 6799 | #set default ca to $ACME_DIRECTORY |
6800 | setdefaultca() { | |
6801 | if [ -z "$ACME_DIRECTORY" ]; then | |
6802 | _err "Please give a --server parameter." | |
6803 | return 1 | |
6804 | fi | |
6805 | _saveaccountconf "DEFAULT_ACME_SERVER" "$ACME_DIRECTORY" | |
df22f680 | 6806 | _info "Changed default CA to: $(__green "$ACME_DIRECTORY")" |
737e9e48 | 6807 | } |
6808 | ||
a63b05a9 | 6809 | _process() { |
6810 | _CMD="" | |
6811 | _domain="" | |
3f4513b3 | 6812 | _altdomains="$NO_VALUE" |
a63b05a9 | 6813 | _webroot="" |
875625b1 | 6814 | _challenge_alias="" |
bdbf323f | 6815 | _keylength="" |
6816 | _accountkeylength="" | |
5c539af7 | 6817 | _cert_file="" |
6818 | _key_file="" | |
6819 | _ca_file="" | |
6820 | _fullchain_file="" | |
4d2f38b0 | 6821 | _reloadcmd="" |
a63b05a9 | 6822 | _password="" |
635695ec | 6823 | _accountconf="" |
6824 | _useragent="" | |
b5eb4b90 | 6825 | _accountemail="" |
6826 | _accountkey="" | |
b2817897 | 6827 | _certhome="" |
27dbe77f | 6828 | _confighome="" |
39c8f79f | 6829 | _httpport="" |
e22bcf7c | 6830 | _tlsport="" |
0e38c60d | 6831 | _dnssleep="" |
dcf4f8f6 | 6832 | _listraw="" |
cc179731 | 6833 | _stopRenewOnError="" |
e3698edd | 6834 | #_insecure="" |
78009539 | 6835 | _ca_bundle="" |
2aa75f03 | 6836 | _ca_path="" |
c8e9a31e | 6837 | _nocron="" |
61556a54 | 6838 | _noprofile="" |
43822d37 | 6839 | _ecc="" |
10afcaca | 6840 | _csr="" |
b0070f03 | 6841 | _pre_hook="" |
6842 | _post_hook="" | |
6843 | _renew_hook="" | |
a61fe418 | 6844 | _deploy_hook="" |
5ea6e9c9 | 6845 | _logfile="" |
d0871bda | 6846 | _log="" |
0463b5d6 | 6847 | _local_address="" |
a73c5b33 | 6848 | _log_level="" |
6bf281f9 | 6849 | _auto_upgrade="" |
6ae0f7f5 | 6850 | _listen_v4="" |
6851 | _listen_v6="" | |
a746139c | 6852 | _openssl_bin="" |
e2edf208 | 6853 | _syslog="" |
9b124070 | 6854 | _use_wget="" |
98394f99 | 6855 | _server="" |
b50e701c | 6856 | _notify_hook="" |
6857 | _notify_level="" | |
6858 | _notify_mode="" | |
1041c9f9 | 6859 | _revoke_reason="" |
f96d91cb | 6860 | _eab_kid="" |
6861 | _eab_hmac_key="" | |
e3ebd582 | 6862 | _preferred_chain="" |
4c2a3841 | 6863 | while [ ${#} -gt 0 ]; do |
a63b05a9 | 6864 | case "${1}" in |
4c2a3841 | 6865 | |
19c43451 | 6866 | --help | -h) |
6867 | showhelp | |
6868 | return | |
6869 | ;; | |
6870 | --version | -v) | |
6871 | version | |
6872 | return | |
6873 | ;; | |
6874 | --install) | |
6875 | _CMD="install" | |
6876 | ;; | |
58c4eaaf | 6877 | --install-online) |
6878 | shift | |
6879 | installOnline "$@" | |
6880 | return | |
6881 | ;; | |
19c43451 | 6882 | --uninstall) |
6883 | _CMD="uninstall" | |
6884 | ;; | |
6885 | --upgrade) | |
6886 | _CMD="upgrade" | |
6887 | ;; | |
6888 | --issue) | |
6889 | _CMD="issue" | |
6890 | ;; | |
6891 | --deploy) | |
6892 | _CMD="deploy" | |
6893 | ;; | |
7decf768 | 6894 | --sign-csr | --signcsr) |
19c43451 | 6895 | _CMD="signcsr" |
6896 | ;; | |
7decf768 | 6897 | --show-csr | --showcsr) |
19c43451 | 6898 | _CMD="showcsr" |
6899 | ;; | |
7decf768 | 6900 | -i | --install-cert | --installcert) |
19c43451 | 6901 | _CMD="installcert" |
6902 | ;; | |
6903 | --renew | -r) | |
6904 | _CMD="renew" | |
6905 | ;; | |
7decf768 | 6906 | --renew-all | --renewAll | --renewall) |
19c43451 | 6907 | _CMD="renewAll" |
6908 | ;; | |
6909 | --revoke) | |
6910 | _CMD="revoke" | |
6911 | ;; | |
6912 | --remove) | |
6913 | _CMD="remove" | |
6914 | ;; | |
6915 | --list) | |
6916 | _CMD="list" | |
6917 | ;; | |
7decf768 | 6918 | --install-cronjob | --installcronjob) |
19c43451 | 6919 | _CMD="installcronjob" |
6920 | ;; | |
7decf768 | 6921 | --uninstall-cronjob | --uninstallcronjob) |
19c43451 | 6922 | _CMD="uninstallcronjob" |
6923 | ;; | |
6924 | --cron) | |
6925 | _CMD="cron" | |
6926 | ;; | |
1521199e | 6927 | --to-pkcs12 | --to-pkcs | --toPkcs) |
19c43451 | 6928 | _CMD="toPkcs" |
6929 | ;; | |
7decf768 | 6930 | --to-pkcs8 | --toPkcs8) |
19c43451 | 6931 | _CMD="toPkcs8" |
6932 | ;; | |
7decf768 | 6933 | --create-account-key | --createAccountKey | --createaccountkey | -cak) |
19c43451 | 6934 | _CMD="createAccountKey" |
6935 | ;; | |
7decf768 | 6936 | --create-domain-key | --createDomainKey | --createdomainkey | -cdk) |
19c43451 | 6937 | _CMD="createDomainKey" |
6938 | ;; | |
7decf768 | 6939 | -ccr | --create-csr | --createCSR | --createcsr) |
19c43451 | 6940 | _CMD="createCSR" |
6941 | ;; | |
6942 | --deactivate) | |
6943 | _CMD="deactivate" | |
6944 | ;; | |
7decf768 | 6945 | --update-account | --updateaccount) |
19c43451 | 6946 | _CMD="updateaccount" |
6947 | ;; | |
7decf768 | 6948 | --register-account | --registeraccount) |
19c43451 | 6949 | _CMD="registeraccount" |
6950 | ;; | |
6951 | --deactivate-account) | |
6952 | _CMD="deactivateaccount" | |
6953 | ;; | |
6954 | --set-notify) | |
6955 | _CMD="setnotify" | |
6956 | ;; | |
6957 | --set-default-ca) | |
6958 | _CMD="setdefaultca" | |
6959 | ;; | |
7decf768 | 6960 | -d | --domain) |
19c43451 | 6961 | _dvalue="$2" |
4c2a3841 | 6962 | |
19c43451 | 6963 | if [ "$_dvalue" ]; then |
6964 | if _startswith "$_dvalue" "-"; then | |
6965 | _err "'$_dvalue' is not a valid domain for parameter '$1'" | |
6966 | return 1 | |
6967 | fi | |
6968 | if _is_idn "$_dvalue" && ! _exists idn; then | |
6969 | _err "It seems that $_dvalue is an IDN( Internationalized Domain Names), please install 'idn' command first." | |
6970 | return 1 | |
6971 | fi | |
4c2a3841 | 6972 | |
19c43451 | 6973 | if _startswith "$_dvalue" "*."; then |
6974 | _debug "Wildcard domain" | |
6975 | export ACME_VERSION=2 | |
6976 | fi | |
6977 | if [ -z "$_domain" ]; then | |
6978 | _domain="$_dvalue" | |
6979 | else | |
6980 | if [ "$_altdomains" = "$NO_VALUE" ]; then | |
6981 | _altdomains="$_dvalue" | |
a63b05a9 | 6982 | else |
19c43451 | 6983 | _altdomains="$_altdomains,$_dvalue" |
a63b05a9 | 6984 | fi |
6985 | fi | |
19c43451 | 6986 | fi |
4c2a3841 | 6987 | |
19c43451 | 6988 | shift |
6989 | ;; | |
a63b05a9 | 6990 | |
7decf768 | 6991 | -f | --force) |
19c43451 | 6992 | FORCE="1" |
6993 | ;; | |
6994 | --staging | --test) | |
6995 | STAGE="1" | |
6996 | ;; | |
6997 | --server) | |
6998 | _server="$2" | |
6999 | _selectServer "$_server" | |
7000 | shift | |
7001 | ;; | |
7002 | --debug) | |
7003 | if [ -z "$2" ] || _startswith "$2" "-"; then | |
7004 | DEBUG="$DEBUG_LEVEL_DEFAULT" | |
7005 | else | |
7006 | DEBUG="$2" | |
64821ad4 | 7007 | shift |
19c43451 | 7008 | fi |
7009 | ;; | |
7010 | --output-insecure) | |
7011 | export OUTPUT_INSECURE=1 | |
7012 | ;; | |
7decf768 | 7013 | -w | --webroot) |
19c43451 | 7014 | wvalue="$2" |
7015 | if [ -z "$_webroot" ]; then | |
7016 | _webroot="$wvalue" | |
7017 | else | |
7018 | _webroot="$_webroot,$wvalue" | |
7019 | fi | |
7020 | shift | |
7021 | ;; | |
7022 | --challenge-alias) | |
7023 | cvalue="$2" | |
7024 | _challenge_alias="$_challenge_alias$cvalue," | |
7025 | shift | |
7026 | ;; | |
7027 | --domain-alias) | |
7028 | cvalue="$DNS_ALIAS_PREFIX$2" | |
7029 | _challenge_alias="$_challenge_alias$cvalue," | |
7030 | shift | |
7031 | ;; | |
7032 | --standalone) | |
7033 | wvalue="$NO_VALUE" | |
7034 | if [ -z "$_webroot" ]; then | |
7035 | _webroot="$wvalue" | |
7036 | else | |
7037 | _webroot="$_webroot,$wvalue" | |
7038 | fi | |
7039 | ;; | |
7040 | --alpn) | |
7041 | wvalue="$W_ALPN" | |
7042 | if [ -z "$_webroot" ]; then | |
7043 | _webroot="$wvalue" | |
7044 | else | |
7045 | _webroot="$_webroot,$wvalue" | |
7046 | fi | |
7047 | ;; | |
7048 | --stateless) | |
7049 | wvalue="$MODE_STATELESS" | |
7050 | if [ -z "$_webroot" ]; then | |
7051 | _webroot="$wvalue" | |
7052 | else | |
7053 | _webroot="$_webroot,$wvalue" | |
7054 | fi | |
7055 | ;; | |
7056 | --local-address) | |
7057 | lvalue="$2" | |
7058 | _local_address="$_local_address$lvalue," | |
7059 | shift | |
7060 | ;; | |
7061 | --apache) | |
7062 | wvalue="apache" | |
7063 | if [ -z "$_webroot" ]; then | |
7064 | _webroot="$wvalue" | |
7065 | else | |
7066 | _webroot="$_webroot,$wvalue" | |
7067 | fi | |
7068 | ;; | |
7069 | --nginx) | |
7070 | wvalue="$NGINX" | |
7071 | if [ "$2" ] && ! _startswith "$2" "-"; then | |
7072 | wvalue="$NGINX$2" | |
0463b5d6 | 7073 | shift |
19c43451 | 7074 | fi |
7075 | if [ -z "$_webroot" ]; then | |
7076 | _webroot="$wvalue" | |
7077 | else | |
7078 | _webroot="$_webroot,$wvalue" | |
7079 | fi | |
7080 | ;; | |
7081 | --dns) | |
7082 | wvalue="$W_DNS" | |
7083 | if [ "$2" ] && ! _startswith "$2" "-"; then | |
7084 | wvalue="$2" | |
0e38c60d | 7085 | shift |
19c43451 | 7086 | fi |
7087 | if [ -z "$_webroot" ]; then | |
7088 | _webroot="$wvalue" | |
7089 | else | |
7090 | _webroot="$_webroot,$wvalue" | |
7091 | fi | |
7092 | ;; | |
7093 | --dnssleep) | |
7094 | _dnssleep="$2" | |
7095 | Le_DNSSleep="$_dnssleep" | |
7096 | shift | |
7097 | ;; | |
4c2a3841 | 7098 | |
19c43451 | 7099 | --keylength | -k) |
7100 | _keylength="$2" | |
7101 | shift | |
7102 | ;; | |
7decf768 | 7103 | -ak | --accountkeylength) |
19c43451 | 7104 | _accountkeylength="$2" |
7105 | shift | |
7106 | ;; | |
a63b05a9 | 7107 | |
19c43451 | 7108 | --cert-file | --certpath) |
7109 | _cert_file="$2" | |
7110 | shift | |
7111 | ;; | |
7112 | --key-file | --keypath) | |
7113 | _key_file="$2" | |
7114 | shift | |
7115 | ;; | |
7116 | --ca-file | --capath) | |
7117 | _ca_file="$2" | |
7118 | shift | |
7119 | ;; | |
7120 | --fullchain-file | --fullchainpath) | |
7121 | _fullchain_file="$2" | |
7122 | shift | |
7123 | ;; | |
7124 | --reloadcmd | --reloadCmd) | |
7125 | _reloadcmd="$2" | |
7126 | shift | |
7127 | ;; | |
7128 | --password) | |
7129 | _password="$2" | |
7130 | shift | |
7131 | ;; | |
7132 | --accountconf) | |
7133 | _accountconf="$2" | |
7134 | ACCOUNT_CONF_PATH="$_accountconf" | |
7135 | shift | |
7136 | ;; | |
7137 | --home) | |
7138 | LE_WORKING_DIR="$2" | |
7139 | shift | |
7140 | ;; | |
7decf768 | 7141 | --cert-home | --certhome) |
19c43451 | 7142 | _certhome="$2" |
7143 | CERT_HOME="$_certhome" | |
7144 | shift | |
7145 | ;; | |
7146 | --config-home) | |
7147 | _confighome="$2" | |
7148 | LE_CONFIG_HOME="$_confighome" | |
7149 | shift | |
7150 | ;; | |
7151 | --useragent) | |
7152 | _useragent="$2" | |
7153 | USER_AGENT="$_useragent" | |
7154 | shift | |
7155 | ;; | |
58c4eaaf | 7156 | -m | --email | --accountemail) |
19c43451 | 7157 | _accountemail="$2" |
58c4eaaf | 7158 | export ACCOUNT_EMAIL="$_accountemail" |
19c43451 | 7159 | shift |
7160 | ;; | |
7161 | --accountkey) | |
7162 | _accountkey="$2" | |
7163 | ACCOUNT_KEY_PATH="$_accountkey" | |
7164 | shift | |
7165 | ;; | |
7166 | --days) | |
7167 | _days="$2" | |
7168 | Le_RenewalDays="$_days" | |
7169 | shift | |
7170 | ;; | |
7171 | --httpport) | |
7172 | _httpport="$2" | |
7173 | Le_HTTPPort="$_httpport" | |
7174 | shift | |
7175 | ;; | |
7176 | --tlsport) | |
7177 | _tlsport="$2" | |
7178 | Le_TLSPort="$_tlsport" | |
7179 | shift | |
7180 | ;; | |
7181 | --listraw) | |
7182 | _listraw="raw" | |
7183 | ;; | |
7decf768 | 7184 | -se | --stop-renew-on-error | --stopRenewOnError | --stoprenewonerror) |
19c43451 | 7185 | _stopRenewOnError="1" |
7186 | ;; | |
7187 | --insecure) | |
7188 | #_insecure="1" | |
7189 | HTTPS_INSECURE="1" | |
7190 | ;; | |
7191 | --ca-bundle) | |
7192 | _ca_bundle="$(_readlink "$2")" | |
7193 | CA_BUNDLE="$_ca_bundle" | |
7194 | shift | |
7195 | ;; | |
7196 | --ca-path) | |
7197 | _ca_path="$2" | |
7198 | CA_PATH="$_ca_path" | |
7199 | shift | |
7200 | ;; | |
58c4eaaf | 7201 | --no-cron | --nocron) |
19c43451 | 7202 | _nocron="1" |
7203 | ;; | |
58c4eaaf | 7204 | --no-profile | --noprofile) |
19c43451 | 7205 | _noprofile="1" |
7206 | ;; | |
2910be82 | 7207 | --no-color) |
19c43451 | 7208 | export ACME_NO_COLOR=1 |
7209 | ;; | |
7210 | --force-color) | |
7211 | export ACME_FORCE_COLOR=1 | |
7212 | ;; | |
7213 | --ecc) | |
7214 | _ecc="isEcc" | |
7215 | ;; | |
7216 | --csr) | |
7217 | _csr="$2" | |
7218 | shift | |
7219 | ;; | |
7220 | --pre-hook) | |
7221 | _pre_hook="$2" | |
7222 | shift | |
7223 | ;; | |
7224 | --post-hook) | |
7225 | _post_hook="$2" | |
7226 | shift | |
7227 | ;; | |
7228 | --renew-hook) | |
7229 | _renew_hook="$2" | |
7230 | shift | |
7231 | ;; | |
7232 | --deploy-hook) | |
7233 | if [ -z "$2" ] || _startswith "$2" "-"; then | |
7234 | _usage "Please specify a value for '--deploy-hook'" | |
7235 | return 1 | |
7236 | fi | |
7237 | _deploy_hook="$_deploy_hook$2," | |
7238 | shift | |
7239 | ;; | |
7240 | --ocsp-must-staple | --ocsp) | |
7241 | Le_OCSP_Staple="1" | |
7242 | ;; | |
7243 | --always-force-new-domain-key) | |
7244 | if [ -z "$2" ] || _startswith "$2" "-"; then | |
7245 | Le_ForceNewDomainKey=1 | |
7246 | else | |
7247 | Le_ForceNewDomainKey="$2" | |
1041c9f9 | 7248 | shift |
19c43451 | 7249 | fi |
7250 | ;; | |
7251 | --yes-I-know-dns-manual-mode-enough-go-ahead-please) | |
7252 | export FORCE_DNS_MANUAL=1 | |
7253 | ;; | |
7254 | --log | --logfile) | |
7255 | _log="1" | |
7256 | _logfile="$2" | |
7257 | if _startswith "$_logfile" '-'; then | |
7258 | _logfile="" | |
7259 | else | |
f96d91cb | 7260 | shift |
19c43451 | 7261 | fi |
7262 | LOG_FILE="$_logfile" | |
7263 | if [ -z "$LOG_LEVEL" ]; then | |
7264 | LOG_LEVEL="$DEFAULT_LOG_LEVEL" | |
7265 | fi | |
7266 | ;; | |
7267 | --log-level) | |
7268 | _log_level="$2" | |
7269 | LOG_LEVEL="$_log_level" | |
7270 | shift | |
7271 | ;; | |
7272 | --syslog) | |
7273 | if ! _startswith "$2" '-'; then | |
7274 | _syslog="$2" | |
f96d91cb | 7275 | shift |
19c43451 | 7276 | fi |
7277 | if [ -z "$_syslog" ]; then | |
7278 | _syslog="$SYSLOG_LEVEL_DEFAULT" | |
7279 | fi | |
7280 | ;; | |
7281 | --auto-upgrade) | |
7282 | _auto_upgrade="$2" | |
7283 | if [ -z "$_auto_upgrade" ] || _startswith "$_auto_upgrade" '-'; then | |
7284 | _auto_upgrade="1" | |
7285 | else | |
e3ebd582 | 7286 | shift |
19c43451 | 7287 | fi |
7288 | AUTO_UPGRADE="$_auto_upgrade" | |
7289 | ;; | |
7290 | --listen-v4) | |
7291 | _listen_v4="1" | |
7292 | Le_Listen_V4="$_listen_v4" | |
7293 | ;; | |
7294 | --listen-v6) | |
7295 | _listen_v6="1" | |
7296 | Le_Listen_V6="$_listen_v6" | |
7297 | ;; | |
7298 | --openssl-bin) | |
7299 | _openssl_bin="$2" | |
7300 | ACME_OPENSSL_BIN="$_openssl_bin" | |
7301 | shift | |
7302 | ;; | |
7303 | --use-wget) | |
7304 | _use_wget="1" | |
7305 | ACME_USE_WGET="1" | |
7306 | ;; | |
7307 | --branch | -b) | |
7308 | export BRANCH="$2" | |
7309 | shift | |
7310 | ;; | |
7311 | --notify-hook) | |
7312 | _nhook="$2" | |
7313 | if _startswith "$_nhook" "-"; then | |
7314 | _err "'$_nhook' is not a hook name for '$1'" | |
a63b05a9 | 7315 | return 1 |
19c43451 | 7316 | fi |
7317 | if [ "$_notify_hook" ]; then | |
7318 | _notify_hook="$_notify_hook,$_nhook" | |
7319 | else | |
7320 | _notify_hook="$_nhook" | |
7321 | fi | |
7322 | shift | |
7323 | ;; | |
7324 | --notify-level) | |
7325 | _nlevel="$2" | |
7326 | if _startswith "$_nlevel" "-"; then | |
7327 | _err "'$_nlevel' is not a integer for '$1'" | |
7328 | return 1 | |
7329 | fi | |
7330 | _notify_level="$_nlevel" | |
7331 | shift | |
7332 | ;; | |
7333 | --notify-mode) | |
7334 | _nmode="$2" | |
7335 | if _startswith "$_nmode" "-"; then | |
7336 | _err "'$_nmode' is not a integer for '$1'" | |
7337 | return 1 | |
7338 | fi | |
7339 | _notify_mode="$_nmode" | |
7340 | shift | |
7341 | ;; | |
7342 | --revoke-reason) | |
7343 | _revoke_reason="$2" | |
7344 | if _startswith "$_revoke_reason" "-"; then | |
7345 | _err "'$_revoke_reason' is not a integer for '$1'" | |
7346 | return 1 | |
7347 | fi | |
7348 | shift | |
7349 | ;; | |
7350 | --eab-kid) | |
7351 | _eab_kid="$2" | |
7352 | shift | |
7353 | ;; | |
7354 | --eab-hmac-key) | |
7355 | _eab_hmac_key="$2" | |
7356 | shift | |
7357 | ;; | |
7358 | --preferred-chain) | |
7359 | _preferred_chain="$2" | |
7360 | shift | |
7361 | ;; | |
7362 | *) | |
7363 | _err "Unknown parameter : $1" | |
7364 | return 1 | |
7365 | ;; | |
a63b05a9 | 7366 | esac |
7367 | ||
7368 | shift 1 | |
7369 | done | |
7370 | ||
4c2a3841 | 7371 | if [ "${_CMD}" != "install" ]; then |
5bdfdfef | 7372 | if [ "$__INTERACTIVE" ] && ! _checkSudo; then |
7373 | if [ -z "$FORCE" ]; then | |
7374 | #Use "echo" here, instead of _info. it's too early | |
7375 | echo "It seems that you are using sudo, please read this link first:" | |
7376 | echo "$_SUDO_WIKI" | |
7377 | return 1 | |
7378 | fi | |
7379 | fi | |
5ea6e9c9 | 7380 | __initHome |
661f0583 | 7381 | if [ "$_log" ]; then |
4c2a3841 | 7382 | if [ -z "$_logfile" ]; then |
661f0583 | 7383 | _logfile="$DEFAULT_LOG_FILE" |
7384 | fi | |
d0871bda | 7385 | fi |
4c2a3841 | 7386 | if [ "$_logfile" ]; then |
5ea6e9c9 | 7387 | _saveaccountconf "LOG_FILE" "$_logfile" |
661f0583 | 7388 | LOG_FILE="$_logfile" |
5ea6e9c9 | 7389 | fi |
a73c5b33 | 7390 | |
4c2a3841 | 7391 | if [ "$_log_level" ]; then |
a73c5b33 | 7392 | _saveaccountconf "LOG_LEVEL" "$_log_level" |
7393 | LOG_LEVEL="$_log_level" | |
7394 | fi | |
4c2a3841 | 7395 | |
e2edf208 | 7396 | if [ "$_syslog" ]; then |
7397 | if _exists logger; then | |
7398 | if [ "$_syslog" = "0" ]; then | |
7399 | _clearaccountconf "SYS_LOG" | |
7400 | else | |
7401 | _saveaccountconf "SYS_LOG" "$_syslog" | |
7402 | fi | |
7403 | SYS_LOG="$_syslog" | |
7404 | else | |
7405 | _err "The 'logger' command is not found, can not enable syslog." | |
7406 | _clearaccountconf "SYS_LOG" | |
7407 | SYS_LOG="" | |
7408 | fi | |
7409 | fi | |
7410 | ||
5ea6e9c9 | 7411 | _processAccountConf |
7412 | fi | |
4c2a3841 | 7413 | |
9d548d81 | 7414 | _debug2 LE_WORKING_DIR "$LE_WORKING_DIR" |
4c2a3841 | 7415 | |
7416 | if [ "$DEBUG" ]; then | |
dcf9cb58 | 7417 | version |
98394f99 | 7418 | if [ "$_server" ]; then |
7419 | _debug "Using server: $_server" | |
7420 | fi | |
dcf9cb58 | 7421 | fi |
9a733a57 | 7422 | _debug "Running cmd: ${_CMD}" |
a63b05a9 | 7423 | case "${_CMD}" in |
58c4eaaf | 7424 | install) install "$_nocron" "$_confighome" "$_noprofile" "$_accountemail" ;; |
19c43451 | 7425 | uninstall) uninstall "$_nocron" ;; |
7426 | upgrade) upgrade ;; | |
7427 | issue) | |
7428 | issue "$_webroot" "$_domain" "$_altdomains" "$_keylength" "$_cert_file" "$_key_file" "$_ca_file" "$_reloadcmd" "$_fullchain_file" "$_pre_hook" "$_post_hook" "$_renew_hook" "$_local_address" "$_challenge_alias" "$_preferred_chain" | |
7429 | ;; | |
7430 | deploy) | |
7431 | deploy "$_domain" "$_deploy_hook" "$_ecc" | |
7432 | ;; | |
7433 | signcsr) | |
c384ed96 | 7434 | signcsr "$_csr" "$_webroot" "$_cert_file" "$_key_file" "$_ca_file" "$_reloadcmd" "$_fullchain_file" "$_pre_hook" "$_post_hook" "$_renew_hook" "$_local_address" "$_challenge_alias" "$_preferred_chain" |
19c43451 | 7435 | ;; |
7436 | showcsr) | |
7437 | showcsr "$_csr" "$_domain" | |
7438 | ;; | |
7439 | installcert) | |
7440 | installcert "$_domain" "$_cert_file" "$_key_file" "$_ca_file" "$_reloadcmd" "$_fullchain_file" "$_ecc" | |
7441 | ;; | |
7442 | renew) | |
7443 | renew "$_domain" "$_ecc" | |
7444 | ;; | |
7445 | renewAll) | |
7446 | renewAll "$_stopRenewOnError" | |
7447 | ;; | |
7448 | revoke) | |
7449 | revoke "$_domain" "$_ecc" "$_revoke_reason" | |
7450 | ;; | |
7451 | remove) | |
7452 | remove "$_domain" "$_ecc" | |
7453 | ;; | |
7454 | deactivate) | |
7455 | deactivate "$_domain,$_altdomains" | |
7456 | ;; | |
7457 | registeraccount) | |
7458 | registeraccount "$_accountkeylength" "$_eab_kid" "$_eab_hmac_key" | |
7459 | ;; | |
7460 | updateaccount) | |
7461 | updateaccount | |
7462 | ;; | |
7463 | deactivateaccount) | |
7464 | deactivateaccount | |
7465 | ;; | |
7466 | list) | |
7467 | list "$_listraw" "$_domain" | |
7468 | ;; | |
7469 | installcronjob) installcronjob "$_confighome" ;; | |
7470 | uninstallcronjob) uninstallcronjob ;; | |
7471 | cron) cron ;; | |
7472 | toPkcs) | |
7473 | toPkcs "$_domain" "$_password" "$_ecc" | |
7474 | ;; | |
7475 | toPkcs8) | |
7476 | toPkcs8 "$_domain" "$_ecc" | |
7477 | ;; | |
7478 | createAccountKey) | |
7479 | createAccountKey "$_accountkeylength" | |
7480 | ;; | |
7481 | createDomainKey) | |
7482 | createDomainKey "$_domain" "$_keylength" | |
7483 | ;; | |
7484 | createCSR) | |
7485 | createCSR "$_domain" "$_altdomains" "$_ecc" | |
7486 | ;; | |
7487 | setnotify) | |
7488 | setnotify "$_notify_hook" "$_notify_level" "$_notify_mode" | |
7489 | ;; | |
7490 | setdefaultca) | |
7491 | setdefaultca | |
7492 | ;; | |
7493 | *) | |
7494 | if [ "$_CMD" ]; then | |
7495 | _err "Invalid command: $_CMD" | |
7496 | fi | |
7497 | showhelp | |
7498 | return 1 | |
7499 | ;; | |
a63b05a9 | 7500 | esac |
d3595686 | 7501 | _ret="$?" |
4c2a3841 | 7502 | if [ "$_ret" != "0" ]; then |
d3595686 | 7503 | return $_ret |
7504 | fi | |
4c2a3841 | 7505 | |
7506 | if [ "${_CMD}" = "install" ]; then | |
7507 | if [ "$_log" ]; then | |
7508 | if [ -z "$LOG_FILE" ]; then | |
d0871bda | 7509 | LOG_FILE="$DEFAULT_LOG_FILE" |
7510 | fi | |
7511 | _saveaccountconf "LOG_FILE" "$LOG_FILE" | |
5ea6e9c9 | 7512 | fi |
4c2a3841 | 7513 | |
7514 | if [ "$_log_level" ]; then | |
a73c5b33 | 7515 | _saveaccountconf "LOG_LEVEL" "$_log_level" |
7516 | fi | |
e2edf208 | 7517 | |
7518 | if [ "$_syslog" ]; then | |
7519 | if _exists logger; then | |
7520 | if [ "$_syslog" = "0" ]; then | |
7521 | _clearaccountconf "SYS_LOG" | |
7522 | else | |
7523 | _saveaccountconf "SYS_LOG" "$_syslog" | |
7524 | fi | |
7525 | else | |
7526 | _err "The 'logger' command is not found, can not enable syslog." | |
7527 | _clearaccountconf "SYS_LOG" | |
7528 | SYS_LOG="" | |
7529 | fi | |
7530 | fi | |
7531 | ||
5ea6e9c9 | 7532 | _processAccountConf |
b5eb4b90 | 7533 | fi |
635695ec | 7534 | |
a63b05a9 | 7535 | } |
7536 | ||
319e0ae3 | 7537 | main() { |
7538 | [ -z "$1" ] && showhelp && return | |
4c2a3841 | 7539 | if _startswith "$1" '-'; then _process "$@"; else "$@"; fi |
319e0ae3 | 7540 | } |
e69a7c38 | 7541 | |
aa7b82de | 7542 | main "$@" |