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