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