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