]> git.proxmox.com Git - mirror_acme.sh.git/blame - acme.sh
Merge pull request #3409 from eastonman/master
[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
12b19165 4012_get_chain_issuers() {
e3ebd582 4013 _cfile="$1"
987571ce 4014 if _contains "$(${ACME_OPENSSL_BIN:-openssl} help crl2pkcs7 2>&1)" "Usage: crl2pkcs7" || _contains "$(${ACME_OPENSSL_BIN:-openssl} crl2pkcs7 -help 2>&1)" "Usage: crl2pkcs7" || _contains "$(${ACME_OPENSSL_BIN:-openssl} crl2pkcs7 help 2>&1)" "unknown option help"; then
12b19165 4015 ${ACME_OPENSSL_BIN:-openssl} crl2pkcs7 -nocrl -certfile $_cfile | ${ACME_OPENSSL_BIN:-openssl} pkcs7 -print_certs -text -noout | grep -i 'Issuer:' | _egrep_o "CN *=[^,]*" | cut -d = -f 2
d5d38b33 4016 else
12b19165 4017 _cindex=1
4018 for _startn in $(grep -n -- "$BEGIN_CERT" "$_cfile" | cut -d : -f 1); do
4019 _endn="$(grep -n -- "$END_CERT" "$_cfile" | cut -d : -f 1 | _head_n $_cindex | _tail_n 1)"
4020 _debug2 "_startn" "$_startn"
4021 _debug2 "_endn" "$_endn"
4022 if [ "$DEBUG" ]; then
4023 _debug2 "cert$_cindex" "$(sed -n "$_startn,${_endn}p" "$_cfile")"
4024 fi
4025 sed -n "$_startn,${_endn}p" "$_cfile" | ${ACME_OPENSSL_BIN:-openssl} x509 -text -noout | grep 'Issuer:' | _egrep_o "CN *=[^,]*" | cut -d = -f 2 | sed "s/ *\(.*\)/\1/"
4026 _cindex=$(_math $_cindex + 1)
4027 done
4028 fi
4029}
4030
4031#
4032_get_chain_subjects() {
4033 _cfile="$1"
4034 if _contains "$(${ACME_OPENSSL_BIN:-openssl} help crl2pkcs7 2>&1)" "Usage: crl2pkcs7" || _contains "$(${ACME_OPENSSL_BIN:-openssl} crl2pkcs7 -help 2>&1)" "Usage: crl2pkcs7" || _contains "$(${ACME_OPENSSL_BIN:-openssl} crl2pkcs7 help 2>&1)" "unknown option help"; then
4035 ${ACME_OPENSSL_BIN:-openssl} crl2pkcs7 -nocrl -certfile $_cfile | ${ACME_OPENSSL_BIN:-openssl} pkcs7 -print_certs -text -noout | grep -i 'Subject:' | _egrep_o "CN *=[^,]*" | cut -d = -f 2
4036 else
4037 _cindex=1
4038 for _startn in $(grep -n -- "$BEGIN_CERT" "$_cfile" | cut -d : -f 1); do
4039 _endn="$(grep -n -- "$END_CERT" "$_cfile" | cut -d : -f 1 | _head_n $_cindex | _tail_n 1)"
4040 _debug2 "_startn" "$_startn"
4041 _debug2 "_endn" "$_endn"
4042 if [ "$DEBUG" ]; then
4043 _debug2 "cert$_cindex" "$(sed -n "$_startn,${_endn}p" "$_cfile")"
4044 fi
4045 sed -n "$_startn,${_endn}p" "$_cfile" | ${ACME_OPENSSL_BIN:-openssl} x509 -text -noout | grep -i 'Subject:' | _egrep_o "CN *=[^,]*" | cut -d = -f 2 | sed "s/ *\(.*\)/\1/"
4046 _cindex=$(_math $_cindex + 1)
4047 done
d5d38b33 4048 fi
e3ebd582 4049}
4050
4051#cert issuer
4052_match_issuer() {
4053 _cfile="$1"
4054 _missuer="$2"
12b19165 4055 _fissuers="$(_get_chain_issuers $_cfile)"
d5d38b33 4056 _debug2 _fissuers "$_fissuers"
12b19165 4057 _rootissuer="$(echo "$_fissuers" | _lower_case | _tail_n 1)"
4058 _debug2 _rootissuer "$_rootissuer"
fdb96e91 4059 _missuer="$(echo "$_missuer" | _lower_case)"
12b19165 4060 _contains "$_rootissuer" "$_missuer"
e3ebd582 4061}
4062
3c07f57a 4063#webroot, domain domainlist keylength
4c3b3608 4064issue() {
4c2a3841 4065 if [ -z "$2" ]; then
2e87e64b 4066 _usage "Usage: $PROJECT_ENTRY --issue --domain <domain.tld> --webroot <directory>"
4c3b3608 4067 return 1
4068 fi
49d75a0c 4069 if [ -z "$1" ]; then
4070 _usage "Please specify at least one validation method: '--webroot', '--standalone', '--apache', '--nginx' or '--dns' etc."
4071 return 1
4072 fi
af1cc3b3 4073 _web_roots="$1"
4074 _main_domain="$2"
02140ce7 4075 _alt_domains="$3"
d2cde379 4076
af1cc3b3 4077 if _contains "$_main_domain" ","; then
4078 _main_domain=$(echo "$2,$3" | cut -d , -f 1)
02140ce7 4079 _alt_domains=$(echo "$2,$3" | cut -d , -f 2- | sed "s/,${NO_VALUE}$//")
2aff36e7 4080 fi
674b5088 4081 _debug _main_domain "$_main_domain"
4082 _debug _alt_domains "$_alt_domains"
4083
d9c9114b 4084 _key_length="$4"
85e1f4ea 4085 _real_cert="$5"
4086 _real_key="$6"
4087 _real_ca="$7"
4088 _reload_cmd="$8"
4089 _real_fullchain="$9"
4090 _pre_hook="${10}"
4091 _post_hook="${11}"
4092 _renew_hook="${12}"
4093 _local_addr="${13}"
875625b1 4094 _challenge_alias="${14}"
e3ebd582 4095 _preferred_chain="${15}"
4c2a3841 4096
bd04638d 4097 if [ -z "$_ACME_IS_RENEW" ]; then
d9c9114b 4098 _initpath "$_main_domain" "$_key_length"
43822d37 4099 mkdir -p "$DOMAIN_PATH"
4100 fi
eccec5f6 4101
a0923622 4102 if _hasfield "$_web_roots" "$W_DNS" && [ -z "$FORCE_DNS_MANUAL" ]; then
4103 _err "$_DNS_MANUAL_ERROR"
4104 return 1
4105 fi
4106
48d9a8c1 4107 _debug "Using ACME_DIRECTORY: $ACME_DIRECTORY"
4108
4109 _initAPI
4110
4c2a3841 4111 if [ -f "$DOMAIN_CONF" ]; then
61623d22 4112 Le_NextRenewTime=$(_readdomainconf Le_NextRenewTime)
a4270efa 4113 _debug Le_NextRenewTime "$Le_NextRenewTime"
95e06de5 4114 if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(_time)" -lt "$Le_NextRenewTime" ]; then
bb25febd 4115 _saved_domain=$(_readdomainconf Le_Domain)
4116 _debug _saved_domain "$_saved_domain"
4117 _saved_alt=$(_readdomainconf Le_Alt)
4118 _debug _saved_alt "$_saved_alt"
02140ce7 4119 if [ "$_saved_domain,$_saved_alt" = "$_main_domain,$_alt_domains" ]; then
bb25febd 4120 _info "Domains not changed."
4121 _info "Skip, Next renewal time is: $(__green "$(_readdomainconf Le_NextRenewTimeStr)")"
4c2a3841 4122 _info "Add '$(__red '--force')' to force to renew."
bb25febd 4123 return $RENEW_SKIP
4124 else
4125 _info "Domains have changed."
4126 fi
4c3b3608 4127 fi
4128 fi
96a46cfc 4129
af1cc3b3 4130 _savedomainconf "Le_Domain" "$_main_domain"
02140ce7 4131 _savedomainconf "Le_Alt" "$_alt_domains"
af1cc3b3 4132 _savedomainconf "Le_Webroot" "$_web_roots"
4c2a3841 4133
c7257bcf 4134 _savedomainconf "Le_PreHook" "$_pre_hook" "base64"
4135 _savedomainconf "Le_PostHook" "$_post_hook" "base64"
4136 _savedomainconf "Le_RenewHook" "$_renew_hook" "base64"
4c2a3841 4137
85e1f4ea 4138 if [ "$_local_addr" ]; then
4139 _savedomainconf "Le_LocalAddress" "$_local_addr"
72518d48 4140 else
4141 _cleardomainconf "Le_LocalAddress"
4142 fi
875625b1 4143 if [ "$_challenge_alias" ]; then
4144 _savedomainconf "Le_ChallengeAlias" "$_challenge_alias"
4145 else
4146 _cleardomainconf "Le_ChallengeAlias"
4147 fi
e3ebd582 4148 if [ "$_preferred_chain" ]; then
4149 _savedomainconf "Le_Preferred_Chain" "$_preferred_chain" "base64"
4150 else
4151 _cleardomainconf "Le_Preferred_Chain"
4152 fi
6ae0f7f5 4153
a6d22e3b 4154 Le_API="$ACME_DIRECTORY"
4155 _savedomainconf "Le_API" "$Le_API"
4156
389518e1 4157 _info "Using CA: $ACME_DIRECTORY"
02140ce7 4158 if [ "$_alt_domains" = "$NO_VALUE" ]; then
4159 _alt_domains=""
4c3b3608 4160 fi
4c2a3841 4161
d9c9114b 4162 if [ "$_key_length" = "$NO_VALUE" ]; then
4163 _key_length=""
d404e92d 4164 fi
4c2a3841 4165
85e1f4ea 4166 if ! _on_before_issue "$_web_roots" "$_main_domain" "$_alt_domains" "$_pre_hook" "$_local_addr"; then
0463b5d6 4167 _err "_on_before_issue."
4168 return 1
4c3b3608 4169 fi
0463b5d6 4170
8a29fbc8 4171 _saved_account_key_hash="$(_readcaconf "CA_KEY_HASH")"
4172 _debug2 _saved_account_key_hash "$_saved_account_key_hash"
4c2a3841 4173
e8b54a50 4174 if [ -z "$ACCOUNT_URL" ] || [ -z "$_saved_account_key_hash" ] || [ "$_saved_account_key_hash" != "$(__calcAccountKeyHash)" ]; then
57e58ce7 4175 if ! _regAccount "$_accountkeylength"; then
85e1f4ea 4176 _on_issue_err "$_post_hook"
8a29fbc8 4177 return 1
4178 fi
57e58ce7 4179 else
4180 _debug "_saved_account_key_hash is not changed, skip register account."
166096dc 4181 fi
166096dc 4182
4c2a3841 4183 if [ -f "$CSR_PATH" ] && [ ! -f "$CERT_KEY_PATH" ]; then
10afcaca 4184 _info "Signing from existing CSR."
4185 else
4186 _key=$(_readdomainconf Le_Keylength)
4187 _debug "Read key length:$_key"
c4b2e582 4188 if [ ! -f "$CERT_KEY_PATH" ] || [ "$_key_length" != "$_key" ] || [ "$Le_ForceNewDomainKey" = "1" ]; then
d9c9114b 4189 if ! createDomainKey "$_main_domain" "$_key_length"; then
10afcaca 4190 _err "Create domain key error."
4191 _clearup
85e1f4ea 4192 _on_issue_err "$_post_hook"
10afcaca 4193 return 1
4194 fi
4195 fi
4196
02140ce7 4197 if ! _createcsr "$_main_domain" "$_alt_domains" "$CERT_KEY_PATH" "$CSR_PATH" "$DOMAIN_SSL_CONF"; then
10afcaca 4198 _err "Create CSR error."
5ef501c5 4199 _clearup
85e1f4ea 4200 _on_issue_err "$_post_hook"
41e3eafa 4201 return 1
4202 fi
4c3b3608 4203 fi
10afcaca 4204
d9c9114b 4205 _savedomainconf "Le_Keylength" "$_key_length"
4c2a3841 4206
4c3b3608 4207 vlist="$Le_Vlist"
882ac74a 4208 _cleardomainconf "Le_Vlist"
cae203be 4209 _info "Getting domain auth token for each domain"
4c3b3608 4210 sep='#'
9d725af6 4211 dvsep=','
4c2a3841 4212 if [ -z "$vlist" ]; then
d2cde379 4213 if [ "$ACME_VERSION" = "2" ]; then
c1151b0d 4214 #make new order request
0093dc3d 4215 _identifiers="{\"type\":\"dns\",\"value\":\"$(_idn "$_main_domain")\"}"
38f1b4d2 4216 _w_index=1
674b5088 4217 while true; do
dd17124e 4218 d="$(echo "$_alt_domains," | cut -d , -f "$_w_index")"
674b5088 4219 _w_index="$(_math "$_w_index" + 1)"
4220 _debug d "$d"
4221 if [ -z "$d" ]; then
4222 break
c1151b0d 4223 fi
0093dc3d 4224 _identifiers="$_identifiers,{\"type\":\"dns\",\"value\":\"$(_idn "$d")\"}"
c1151b0d 4225 done
4226 _debug2 _identifiers "$_identifiers"
4227 if ! _send_signed_request "$ACME_NEW_ORDER" "{\"identifiers\": [$_identifiers]}"; then
4228 _err "Create new order error."
4229 _clearup
4230 _on_issue_err "$_post_hook"
4231 return 1
4232 fi
05aa26e6 4233 Le_LinkOrder="$(echo "$responseHeaders" | grep -i '^Location.*$' | _tail_n 1 | tr -d "\r\n " | cut -d ":" -f 2-)"
fbdc5a0e 4234 _debug Le_LinkOrder "$Le_LinkOrder"
dbc44c08 4235 Le_OrderFinalize="$(echo "$response" | _egrep_o '"finalize" *: *"[^"]*"' | cut -d '"' -f 4)"
d2cde379 4236 _debug Le_OrderFinalize "$Le_OrderFinalize"
4237 if [ -z "$Le_OrderFinalize" ]; then
78915896 4238 _err "Create new order error. Le_OrderFinalize not found. $response"
c1151b0d 4239 _clearup
4240 _on_issue_err "$_post_hook"
4241 return 1
4242 fi
4243
4244 #for dns manual mode
d2cde379 4245 _savedomainconf "Le_OrderFinalize" "$Le_OrderFinalize"
c1151b0d 4246
93de1e49 4247 _authorizations_seg="$(echo "$response" | _json_decode | _egrep_o '"authorizations" *: *\[[^\[]*\]' | cut -d '[' -f 2 | tr -d ']' | tr -d '"')"
c1151b0d 4248 _debug2 _authorizations_seg "$_authorizations_seg"
4249 if [ -z "$_authorizations_seg" ]; then
4250 _err "_authorizations_seg not found."
4251 _clearup
4252 _on_issue_err "$_post_hook"
4253 return 1
4254 fi
4255
4256 #domain and authz map
4257 _authorizations_map=""
f8d22c48 4258 for _authz_url in $(echo "$_authorizations_seg" | tr ',' ' '); do
c1151b0d 4259 _debug2 "_authz_url" "$_authz_url"
0483d841 4260 if ! _send_signed_request "$_authz_url"; then
c1151b0d 4261 _err "get to authz error."
263c38ca 4262 _err "_authorizations_seg" "$_authorizations_seg"
4263 _err "_authz_url" "$_authz_url"
c1151b0d 4264 _clearup
4265 _on_issue_err "$_post_hook"
4266 return 1
4267 fi
4268
4269 response="$(echo "$response" | _normalizeJson)"
4270 _debug2 response "$response"
4271 _d="$(echo "$response" | _egrep_o '"value" *: *"[^"]*"' | cut -d : -f 2 | tr -d ' "')"
72f54ca6 4272 if _contains "$response" "\"wildcard\" *: *true"; then
4273 _d="*.$_d"
4274 fi
c1151b0d 4275 _debug2 _d "$_d"
4276 _authorizations_map="$_d,$response
4277$_authorizations_map"
4278 done
4279 _debug2 _authorizations_map "$_authorizations_map"
4280 fi
4281
c1151b0d 4282 _index=0
a63b05a9 4283 _currentRoot=""
38f1b4d2 4284 _w_index=1
88bbe55b 4285 while true; do
88bbe55b 4286 d="$(echo "$_main_domain,$_alt_domains," | cut -d , -f "$_w_index")"
4287 _w_index="$(_math "$_w_index" + 1)"
4288 _debug d "$d"
4289 if [ -z "$d" ]; then
4290 break
4291 fi
ca7202eb 4292 _info "Getting webroot for domain" "$d"
c1151b0d 4293 _index=$(_math $_index + 1)
af1cc3b3 4294 _w="$(echo $_web_roots | cut -d , -f $_index)"
9d725af6 4295 _debug _w "$_w"
4c2a3841 4296 if [ "$_w" ]; then
a63b05a9 4297 _currentRoot="$_w"
4298 fi
4299 _debug "_currentRoot" "$_currentRoot"
4c2a3841 4300
a63b05a9 4301 vtype="$VTYPE_HTTP"
c1151b0d 4302 #todo, v2 wildcard force to use dns
3881f221 4303 if _startswith "$_currentRoot" "$W_DNS"; then
a63b05a9 4304 vtype="$VTYPE_DNS"
4305 fi
4c2a3841 4306
08681f4a 4307 if [ "$_currentRoot" = "$W_ALPN" ]; then
4308 vtype="$VTYPE_ALPN"
4309 fi
4310
c1151b0d 4311 if [ "$ACME_VERSION" = "2" ]; then
d04c6dd3 4312 _idn_d="$(_idn "$d")"
dbc43550
L
4313 _candidates="$(echo "$_authorizations_map" | grep -i "^$_idn_d,")"
4314 _debug2 _candidates "$_candidates"
4315 if [ "$(echo "$_candidates" | wc -l)" -gt 1 ]; then
4316 for _can in $_candidates; do
d04c6dd3 4317 if _startswith "$(echo "$_can" | tr '.' '|')" "$(echo "$_idn_d" | tr '.' '|'),"; then
dbc43550 4318 _candidates="$_can"
d04c6dd3 4319 break
4320 fi
4321 done
4322 fi
dbc43550 4323 response="$(echo "$_candidates" | sed "s/$_idn_d,//")"
c1151b0d 4324 _debug2 "response" "$response"
4325 if [ -z "$response" ]; then
4326 _err "get to authz error."
263c38ca 4327 _err "_authorizations_map" "$_authorizations_map"
c1151b0d 4328 _clearup
4329 _on_issue_err "$_post_hook"
4330 return 1
4331 fi
4332 else
4333 if ! __get_domain_new_authz "$d"; then
4334 _clearup
4335 _on_issue_err "$_post_hook"
4336 return 1
4337 fi
c4d8fd83 4338 fi
4339
4c2a3841 4340 if [ -z "$thumbprint" ]; then
339a8ad6 4341 thumbprint="$(__calc_account_thumbprint)"
4c3b3608 4342 fi
4343
dbc44c08 4344 entry="$(echo "$response" | _egrep_o '[^\{]*"type":"'$vtype'"[^\}]*')"
4c3b3608 4345 _debug entry "$entry"
9541ea6a 4346 keyauthorization=""
4c2a3841 4347 if [ -z "$entry" ]; then
9541ea6a 4348 if ! _startswith "$d" '*.'; then
4349 _debug "Not a wildcard domain, lets check whether the validation is already valid."
4350 if echo "$response" | grep '"status":"valid"' >/dev/null 2>&1; then
4351 _debug "$d is already valid."
4352 keyauthorization="$STATE_VERIFIED"
4353 _debug keyauthorization "$keyauthorization"
4354 fi
4355 fi
4356 if [ -z "$keyauthorization" ]; then
fc3a1817 4357 _err "Error, can not get domain token entry $d for $vtype"
9541ea6a 4358 _supported_vtypes="$(echo "$response" | _egrep_o "\"challenges\":\[[^]]*]" | tr '{' "\n" | grep type | cut -d '"' -f 4 | tr "\n" ' ')"
4359 if [ "$_supported_vtypes" ]; then
4360 _err "The supported validation types are: $_supported_vtypes, but you specified: $vtype"
4361 fi
4362 _clearup
4363 _on_issue_err "$_post_hook"
4364 return 1
b51ed9bb 4365 fi
c1151b0d 4366 fi
f8b225e7 4367
9541ea6a 4368 if [ -z "$keyauthorization" ]; then
4369 token="$(echo "$entry" | _egrep_o '"token":"[^"]*' | cut -d : -f 2 | tr -d '"')"
4370 _debug token "$token"
cae203be 4371
9541ea6a 4372 if [ -z "$token" ]; then
4373 _err "Error, can not get domain token $entry"
4374 _clearup
4375 _on_issue_err "$_post_hook"
4376 return 1
4377 fi
4378 if [ "$ACME_VERSION" = "2" ]; then
4379 uri="$(echo "$entry" | _egrep_o '"url":"[^"]*' | cut -d '"' -f 4 | _head_n 1)"
4380 else
4381 uri="$(echo "$entry" | _egrep_o '"uri":"[^"]*' | cut -d '"' -f 4)"
4382 fi
4383 _debug uri "$uri"
4c3b3608 4384
9541ea6a 4385 if [ -z "$uri" ]; then
4386 _err "Error, can not get domain uri. $entry"
4387 _clearup
4388 _on_issue_err "$_post_hook"
4389 return 1
4390 fi
4391 keyauthorization="$token.$thumbprint"
d35bf517 4392 _debug keyauthorization "$keyauthorization"
9541ea6a 4393
4394 if printf "%s" "$response" | grep '"status":"valid"' >/dev/null 2>&1; then
4395 _debug "$d is already verified."
4396 keyauthorization="$STATE_VERIFIED"
4397 _debug keyauthorization "$keyauthorization"
4398 fi
ec603bee 4399 fi
4400
a63b05a9 4401 dvlist="$d$sep$keyauthorization$sep$uri$sep$vtype$sep$_currentRoot"
4c3b3608 4402 _debug dvlist "$dvlist"
4c2a3841 4403
9d725af6 4404 vlist="$vlist$dvlist$dvsep"
4c3b3608 4405
4406 done
9d725af6 4407 _debug vlist "$vlist"
4c3b3608 4408 #add entry
b5ca9bba 4409 dns_entries=""
4c3b3608 4410 dnsadded=""
9d725af6 4411 ventries=$(echo "$vlist" | tr "$dvsep" ' ')
1f7df33e 4412 _alias_index=1
4c2a3841 4413 for ventry in $ventries; do
ca7202eb 4414 d=$(echo "$ventry" | cut -d "$sep" -f 1)
4415 keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2)
4416 vtype=$(echo "$ventry" | cut -d "$sep" -f 4)
4417 _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5)
72f54ca6 4418 _debug d "$d"
4c2a3841 4419 if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
69212114 4420 _debug "$d is already verified, skip $vtype."
fd536d37 4421 _alias_index="$(_math "$_alias_index" + 1)"
ec603bee 4422 continue
4423 fi
4424
4c2a3841 4425 if [ "$vtype" = "$VTYPE_DNS" ]; then
4c3b3608 4426 dnsadded='0'
72f54ca6 4427 _dns_root_d="$d"
4428 if _startswith "$_dns_root_d" "*."; then
4429 _dns_root_d="$(echo "$_dns_root_d" | sed 's/*.//')"
4430 fi
875625b1 4431 _d_alias="$(_getfield "$_challenge_alias" "$_alias_index")"
4432 _alias_index="$(_math "$_alias_index" + 1)"
4433 _debug "_d_alias" "$_d_alias"
4434 if [ "$_d_alias" ]; then
64821ad4 4435 if _startswith "$_d_alias" "$DNS_ALIAS_PREFIX"; then
4436 txtdomain="$(echo "$_d_alias" | sed "s/$DNS_ALIAS_PREFIX//")"
4437 else
4438 txtdomain="_acme-challenge.$_d_alias"
4439 fi
82b0ebb7 4440 dns_entry="${_dns_root_d}${dvsep}_acme-challenge.$_dns_root_d$dvsep$txtdomain$dvsep$_currentRoot"
875625b1 4441 else
4442 txtdomain="_acme-challenge.$_dns_root_d"
82b0ebb7 4443 dns_entry="${_dns_root_d}${dvsep}_acme-challenge.$_dns_root_d$dvsep$dvsep$_currentRoot"
875625b1 4444 fi
82b0ebb7 4445
4c3b3608 4446 _debug txtdomain "$txtdomain"
11927a76 4447 txt="$(printf "%s" "$keyauthorization" | _digest "sha256" | _url_replace)"
4c3b3608 4448 _debug txt "$txt"
a61fe418 4449
b50e701c 4450 d_api="$(_findHook "$_dns_root_d" $_SUB_FOLDER_DNSAPI "$_currentRoot")"
4c3b3608 4451 _debug d_api "$d_api"
82b0ebb7 4452
4453 dns_entry="$dns_entry$dvsep$txt${dvsep}$d_api"
4454 _debug2 dns_entry "$dns_entry"
4c2a3841 4455 if [ "$d_api" ]; then
a180b95c 4456 _debug "Found domain api file: $d_api"
4c3b3608 4457 else
3881f221 4458 if [ "$_currentRoot" != "$W_DNS" ]; then
4459 _err "Can not find dns api hook for: $_currentRoot"
4460 _info "You need to add the txt record manually."
4461 fi
5f8b60a0 4462 _info "$(__red "Add the following TXT record:")"
81772fb7 4463 _info "$(__red "Domain: '$(__green "$txtdomain")'")"
4464 _info "$(__red "TXT value: '$(__green "$txt")'")"
4465 _info "$(__red "Please be aware that you prepend _acme-challenge. before your domain")"
4466 _info "$(__red "so the resulting subdomain will be: $txtdomain")"
4c3b3608 4467 continue
4468 fi
4c2a3841 4469
73b8b120 4470 (
ca7202eb 4471 if ! . "$d_api"; then
73b8b120 4472 _err "Load file $d_api error. Please check your api file and try again."
4473 return 1
4474 fi
4c2a3841 4475
158f22f7 4476 addcommand="${_currentRoot}_add"
ca7202eb 4477 if ! _exists "$addcommand"; then
73b8b120 4478 _err "It seems that your api file is not correct, it must have a function named: $addcommand"
4479 return 1
4480 fi
a180b95c 4481 _info "Adding txt value: $txt for domain: $txtdomain"
ca7202eb 4482 if ! $addcommand "$txtdomain" "$txt"; then
73b8b120 4483 _err "Error add txt for domain:$txtdomain"
4484 return 1
4485 fi
a180b95c 4486 _info "The txt record is added: Success."
73b8b120 4487 )
4c2a3841 4488
4489 if [ "$?" != "0" ]; then
ea722da3 4490 _on_issue_err "$_post_hook" "$vlist"
545f2355 4491 _clearup
4c3b3608 4492 return 1
4493 fi
82b0ebb7 4494 dns_entries="$dns_entries$dns_entry
4495"
4496 _debug2 "$dns_entries"
4c3b3608 4497 dnsadded='1'
4498 fi
4499 done
4500
4c2a3841 4501 if [ "$dnsadded" = '0' ]; then
4502 _savedomainconf "Le_Vlist" "$vlist"
4c3b3608 4503 _debug "Dns record not added yet, so, save to $DOMAIN_CONF and exit."
a8b62261 4504 _err "Please add the TXT records to the domains, and re-run with --renew."
5f8b60a0 4505 _on_issue_err "$_post_hook"
545f2355 4506 _clearup
4c3b3608 4507 return 1
4508 fi
4c2a3841 4509
4c3b3608 4510 fi
4c2a3841 4511
b5ca9bba 4512 if [ "$dns_entries" ]; then
4c2a3841 4513 if [ -z "$Le_DNSSleep" ]; then
427c2780 4514 _info "Let's check each DNS record now. Sleep 20 seconds first."
b5ca9bba 4515 _sleep 20
4516 if ! _check_dns_entries; then
4517 _err "check dns error."
4518 _on_issue_err "$_post_hook"
4519 _clearup
4520 return 1
4521 fi
0e38c60d 4522 else
4c2a3841 4523 _savedomainconf "Le_DNSSleep" "$Le_DNSSleep"
b5ca9bba 4524 _info "Sleep $(__green $Le_DNSSleep) seconds for the txt records to take effect"
4525 _sleep "$Le_DNSSleep"
0e38c60d 4526 fi
4c3b3608 4527 fi
4c2a3841 4528
5d943a35 4529 NGINX_RESTORE_VLIST=""
4c3b3608 4530 _debug "ok, let's start to verify"
a63b05a9 4531
0463b5d6 4532 _ncIndex=1
9d725af6 4533 ventries=$(echo "$vlist" | tr "$dvsep" ' ')
4c2a3841 4534 for ventry in $ventries; do
ca7202eb 4535 d=$(echo "$ventry" | cut -d "$sep" -f 1)
4536 keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2)
4537 uri=$(echo "$ventry" | cut -d "$sep" -f 3)
4538 vtype=$(echo "$ventry" | cut -d "$sep" -f 4)
4539 _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5)
ec603bee 4540
4c2a3841 4541 if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
ec603bee 4542 _info "$d is already verified, skip $vtype."
4543 continue
4544 fi
4545
dd068467 4546 _info "Verifying: $d"
4c3b3608 4547 _debug "d" "$d"
4548 _debug "keyauthorization" "$keyauthorization"
4549 _debug "uri" "$uri"
4550 removelevel=""
e22bcf7c 4551 token="$(printf "%s" "$keyauthorization" | cut -d '.' -f 1)"
a63b05a9 4552
4553 _debug "_currentRoot" "$_currentRoot"
4554
4c2a3841 4555 if [ "$vtype" = "$VTYPE_HTTP" ]; then
4556 if [ "$_currentRoot" = "$NO_VALUE" ]; then
4c3b3608 4557 _info "Standalone mode server"
85e1f4ea 4558 _ncaddr="$(_getfield "$_local_addr" "$_ncIndex")"
0463b5d6 4559 _ncIndex="$(_math $_ncIndex + 1)"
3794b5cb 4560 _startserver "$keyauthorization" "$_ncaddr"
4c2a3841 4561 if [ "$?" != "0" ]; then
5ef501c5 4562 _clearup
58e4d337 4563 _on_issue_err "$_post_hook" "$vlist"
6fc1447f 4564 return 1
4565 fi
5dbf664a 4566 sleep 1
ca7202eb 4567 _debug serverproc "$serverproc"
0e44f587 4568 elif [ "$_currentRoot" = "$MODE_STATELESS" ]; then
4569 _info "Stateless mode for domain:$d"
4570 _sleep 1
9d725af6 4571 elif _startswith "$_currentRoot" "$NGINX"; then
4572 _info "Nginx mode for domain:$d"
4573 #set up nginx server
4574 FOUND_REAL_NGINX_CONF=""
4575 BACKUP_NGINX_CONF=""
4576 if ! _setNginx "$d" "$_currentRoot" "$thumbprint"; then
4577 _clearup
58e4d337 4578 _on_issue_err "$_post_hook" "$vlist"
9d725af6 4579 return 1
03f8d6e9 4580 fi
302c41ed 4581
03f8d6e9 4582 if [ "$FOUND_REAL_NGINX_CONF" ]; then
9d725af6 4583 _realConf="$FOUND_REAL_NGINX_CONF"
4584 _backup="$BACKUP_NGINX_CONF"
4585 _debug _realConf "$_realConf"
5d943a35 4586 NGINX_RESTORE_VLIST="$d$sep$_realConf$sep$_backup$dvsep$NGINX_RESTORE_VLIST"
9d725af6 4587 fi
4588 _sleep 1
4c3b3608 4589 else
4c2a3841 4590 if [ "$_currentRoot" = "apache" ]; then
6f930641 4591 wellknown_path="$ACME_DIR"
4592 else
a63b05a9 4593 wellknown_path="$_currentRoot/.well-known/acme-challenge"
4c2a3841 4594 if [ ! -d "$_currentRoot/.well-known" ]; then
6f930641 4595 removelevel='1'
4c2a3841 4596 elif [ ! -d "$_currentRoot/.well-known/acme-challenge" ]; then
6f930641 4597 removelevel='2'
4598 else
4599 removelevel='3'
4600 fi
4c3b3608 4601 fi
6f930641 4602
4c3b3608 4603 _debug wellknown_path "$wellknown_path"
6f930641 4604
4c3b3608 4605 _debug "writing token:$token to $wellknown_path/$token"
4606
4607 mkdir -p "$wellknown_path"
93fc48a2 4608
4c2a3841 4609 if ! printf "%s" "$keyauthorization" >"$wellknown_path/$token"; then
93fc48a2 4610 _err "$d:Can not write token to file : $wellknown_path/$token"
4611 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4612 _clearup
58e4d337 4613 _on_issue_err "$_post_hook" "$vlist"
93fc48a2 4614 return 1
4615 fi
4616
4c2a3841 4617 if [ ! "$usingApache" ]; then
44edb2bd 4618 if webroot_owner=$(_stat "$_currentRoot"); then
32fdc196 4619 _debug "Changing owner/group of .well-known to $webroot_owner"
c87cd0de 4620 if ! _exec "chown -R \"$webroot_owner\" \"$_currentRoot/.well-known\""; then
4621 _debug "$(cat "$_EXEC_TEMP_ERR")"
4622 _exec_err >/dev/null 2>&1
4623 fi
32fdc196 4624 else
b54ce310 4625 _debug "not changing owner/group of webroot"
32fdc196 4626 fi
df886ffa 4627 fi
4c2a3841 4628
4c3b3608 4629 fi
08681f4a 4630 elif [ "$vtype" = "$VTYPE_ALPN" ]; then
4631 acmevalidationv1="$(printf "%s" "$keyauthorization" | _digest "sha256" "hex")"
4632 _debug acmevalidationv1 "$acmevalidationv1"
4633 if ! _starttlsserver "$d" "" "$Le_TLSPort" "$keyauthorization" "$_ncaddr" "$acmevalidationv1"; then
4634 _err "Start tls server error."
4635 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4636 _clearup
4637 _on_issue_err "$_post_hook" "$vlist"
4638 return 1
4639 fi
4c3b3608 4640 fi
4c2a3841 4641
920cab6f 4642 if ! __trigger_validation "$uri" "$keyauthorization" "$vtype"; then
c4d8fd83 4643 _err "$d:Can not get challenge: $response"
4644 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4645 _clearup
58e4d337 4646 _on_issue_err "$_post_hook" "$vlist"
c4d8fd83 4647 return 1
4648 fi
4c2a3841 4649
c1151b0d 4650 if [ "$code" ] && [ "$code" != '202' ]; then
8bd12ed0 4651 if [ "$code" = '200' ]; then
c1151b0d 4652 _debug "trigger validation code: $code"
4653 else
8bd12ed0
K
4654 _err "$d:Challenge error: $response"
4655 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4656 _clearup
4657 _on_issue_err "$_post_hook" "$vlist"
4658 return 1
c1151b0d 4659 fi
4c3b3608 4660 fi
4c2a3841 4661
6fc1447f 4662 waittimes=0
4c2a3841 4663 if [ -z "$MAX_RETRY_TIMES" ]; then
6fc1447f 4664 MAX_RETRY_TIMES=30
4665 fi
4c2a3841 4666
4667 while true; do
0c538f75 4668 waittimes=$(_math "$waittimes" + 1)
4c2a3841 4669 if [ "$waittimes" -ge "$MAX_RETRY_TIMES" ]; then
6fc1447f 4670 _err "$d:Timeout"
4671 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4672 _clearup
58e4d337 4673 _on_issue_err "$_post_hook" "$vlist"
6fc1447f 4674 return 1
4675 fi
4c2a3841 4676
5dbf664a 4677 _debug "sleep 2 secs to verify"
4678 sleep 2
4c3b3608 4679 _debug "checking"
0483d841 4680 if [ "$ACME_VERSION" = "2" ]; then
4681 _send_signed_request "$uri"
4682 else
4683 response="$(_get "$uri")"
4684 fi
4c2a3841 4685 if [ "$?" != "0" ]; then
c60883ef 4686 _err "$d:Verify error:$response"
a63b05a9 4687 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4c3b3608 4688 _clearup
58e4d337 4689 _on_issue_err "$_post_hook" "$vlist"
4c3b3608 4690 return 1
4691 fi
9aaf36cd 4692 _debug2 original "$response"
4c2a3841 4693
4694 response="$(echo "$response" | _normalizeJson)"
7012b91f 4695 _debug2 response "$response"
4c2a3841 4696
4697 status=$(echo "$response" | _egrep_o '"status":"[^"]*' | cut -d : -f 2 | tr -d '"')
4698 if [ "$status" = "valid" ]; then
93f3098a 4699 _info "$(__green Success)"
ca7202eb 4700 _stopserver "$serverproc"
4c3b3608 4701 serverproc=""
a63b05a9 4702 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4c2a3841 4703 break
4c3b3608 4704 fi
4c2a3841 4705
4706 if [ "$status" = "invalid" ]; then
d0d74907 4707 error="$(echo "$response" | _egrep_o '"error":\{[^\}]*')"
4c2a3841 4708 _debug2 error "$error"
4709 errordetail="$(echo "$error" | _egrep_o '"detail": *"[^"]*' | cut -d '"' -f 4)"
4710 _debug2 errordetail "$errordetail"
4711 if [ "$errordetail" ]; then
4712 _err "$d:Verify error:$errordetail"
4713 else
4714 _err "$d:Verify error:$error"
4715 fi
4716 if [ "$DEBUG" ]; then
4717 if [ "$vtype" = "$VTYPE_HTTP" ]; then
4718 _debug "Debug: get token url."
4719 _get "http://$d/.well-known/acme-challenge/$token" "" 1
4720 fi
4721 fi
a63b05a9 4722 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4c3b3608 4723 _clearup
58e4d337 4724 _on_issue_err "$_post_hook" "$vlist"
4c2a3841 4725 return 1
4c3b3608 4726 fi
4c2a3841 4727
4728 if [ "$status" = "pending" ]; then
4c3b3608 4729 _info "Pending"
93740c99 4730 elif [ "$status" = "processing" ]; then
4731 _info "Processing"
4c3b3608 4732 else
4c2a3841 4733 _err "$d:Verify error:$response"
a63b05a9 4734 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4c3b3608 4735 _clearup
58e4d337 4736 _on_issue_err "$_post_hook" "$vlist"
4c3b3608 4737 return 1
4738 fi
4c2a3841 4739
4c3b3608 4740 done
4c2a3841 4741
4c3b3608 4742 done
4743
4744 _clearup
4745 _info "Verify finished, start to sign."
11927a76 4746 der="$(_getfile "${CSR_PATH}" "${BEGIN_CSR}" "${END_CSR}" | tr -d "\r\n" | _url_replace)"
4c2a3841 4747
c1151b0d 4748 if [ "$ACME_VERSION" = "2" ]; then
bd04638d 4749 _info "Lets finalize the order."
4750 _info "Le_OrderFinalize" "$Le_OrderFinalize"
d2cde379 4751 if ! _send_signed_request "${Le_OrderFinalize}" "{\"csr\": \"$der\"}"; then
c1151b0d 4752 _err "Sign failed."
4753 _on_issue_err "$_post_hook"
4754 return 1
4755 fi
4756 if [ "$code" != "200" ]; then
e7f7e96d 4757 _err "Sign failed, finalize code is not 200."
668c43ab 4758 _err "$response"
c1151b0d 4759 _on_issue_err "$_post_hook"
4760 return 1
4761 fi
e7f7e96d 4762 if [ -z "$Le_LinkOrder" ]; then
0c9c1ae6 4763 Le_LinkOrder="$(echo "$responseHeaders" | grep -i '^Location.*$' | _tail_n 1 | tr -d "\r\n \t" | cut -d ":" -f 2-)"
e7f7e96d 4764 fi
fbdc5a0e 4765
e7f7e96d 4766 _savedomainconf "Le_LinkOrder" "$Le_LinkOrder"
4767
4768 _link_cert_retry=0
ff9be30f 4769 _MAX_CERT_RETRY=30
a3d8b993 4770 while [ "$_link_cert_retry" -lt "$_MAX_CERT_RETRY" ]; do
e7f7e96d 4771 if _contains "$response" "\"status\":\"valid\""; then
4772 _debug "Order status is valid."
d0d74907 4773 Le_LinkCert="$(echo "$response" | _egrep_o '"certificate" *: *"[^"]*"' | cut -d '"' -f 4)"
e7f7e96d 4774 _debug Le_LinkCert "$Le_LinkCert"
4775 if [ -z "$Le_LinkCert" ]; then
4776 _err "Sign error, can not find Le_LinkCert"
4777 _err "$response"
4778 _on_issue_err "$_post_hook"
4779 return 1
4780 fi
4781 break
4782 elif _contains "$response" "\"processing\""; then
4783 _info "Order status is processing, lets sleep and retry."
1fe8235a 4784 _retryafter=$(echo "$responseHeaders" | grep -i "^Retry-After *:" | cut -d : -f 2 | tr -d ' ' | tr -d '\r')
4785 _debug "_retryafter" "$_retryafter"
4786 if [ "$_retryafter" ]; then
4787 _info "Retry after: $_retryafter"
4788 _sleep $_retryafter
4789 else
4790 _sleep 2
4791 fi
e7f7e96d 4792 else
4793 _err "Sign error, wrong status"
4794 _err "$response"
4795 _on_issue_err "$_post_hook"
4796 return 1
4797 fi
fbdc5a0e 4798 #the order is processing, so we are going to poll order status
4799 if [ -z "$Le_LinkOrder" ]; then
4800 _err "Sign error, can not get order link location header"
4801 _err "responseHeaders" "$responseHeaders"
4802 _on_issue_err "$_post_hook"
4803 return 1
4804 fi
4805 _info "Polling order status: $Le_LinkOrder"
e7f7e96d 4806 if ! _send_signed_request "$Le_LinkOrder"; then
4807 _err "Sign failed, can not post to Le_LinkOrder cert:$Le_LinkOrder."
4808 _err "$response"
4809 _on_issue_err "$_post_hook"
4810 return 1
4811 fi
4812 _link_cert_retry="$(_math $_link_cert_retry + 1)"
4813 done
4c2a3841 4814
e7f7e96d 4815 if [ -z "$Le_LinkCert" ]; then
4816 _err "Sign failed, can not get Le_LinkCert, retry time limit."
4817 _err "$response"
4818 _on_issue_err "$_post_hook"
4819 return 1
4820 fi
bd04638d 4821 _info "Downloading cert."
4822 _info "Le_LinkCert" "$Le_LinkCert"
f2acdd27 4823 if ! _send_signed_request "$Le_LinkCert"; then
668c43ab 4824 _err "Sign failed, can not download cert:$Le_LinkCert."
4825 _err "$response"
c1151b0d 4826 _on_issue_err "$_post_hook"
4827 return 1
4828 fi
4c3b3608 4829
f2acdd27 4830 echo "$response" >"$CERT_PATH"
e3ebd582 4831 _split_cert_chain "$CERT_PATH" "$CERT_FULLCHAIN_PATH" "$CA_CERT_PATH"
4832
d5d38b33 4833 if [ "$_preferred_chain" ] && [ -f "$CERT_FULLCHAIN_PATH" ]; then
12b19165 4834 if [ "$DEBUG" ]; then
4835 _debug "default chain issuers: " "$(_get_chain_issuers "$CERT_FULLCHAIN_PATH")"
4836 fi
d5d38b33 4837 if ! _match_issuer "$CERT_FULLCHAIN_PATH" "$_preferred_chain"; then
e3ebd582 4838 rels="$(echo "$responseHeaders" | tr -d ' <>' | grep -i "^link:" | grep -i 'rel="alternate"' | cut -d : -f 2- | cut -d ';' -f 1)"
4839 _debug2 "rels" "$rels"
4840 for rel in $rels; do
4841 _info "Try rel: $rel"
4842 if ! _send_signed_request "$rel"; then
4843 _err "Sign failed, can not download cert:$rel"
4844 _err "$response"
4845 continue
4846 fi
4847 _relcert="$CERT_PATH.alt"
4848 _relfullchain="$CERT_FULLCHAIN_PATH.alt"
4849 _relca="$CA_CERT_PATH.alt"
4850 echo "$response" >"$_relcert"
4851 _split_cert_chain "$_relcert" "$_relfullchain" "$_relca"
12b19165 4852 if [ "$DEBUG" ]; then
4853 _debug "rel chain issuers: " "$(_get_chain_issuers "$_relfullchain")"
4854 fi
d5d38b33 4855 if _match_issuer "$_relfullchain" "$_preferred_chain"; then
e3ebd582 4856 _info "Matched issuer in: $rel"
4857 cat $_relcert >"$CERT_PATH"
4858 cat $_relfullchain >"$CERT_FULLCHAIN_PATH"
4859 cat $_relca >"$CA_CERT_PATH"
12b19165 4860 rm -f "$_relcert"
4861 rm -f "$_relfullchain"
4862 rm -f "$_relca"
e3ebd582 4863 break
4864 fi
12b19165 4865 rm -f "$_relcert"
4866 rm -f "$_relfullchain"
4867 rm -f "$_relca"
e3ebd582 4868 done
4869 fi
1c35f46b 4870 fi
c1151b0d 4871 else
4872 if ! _send_signed_request "${ACME_NEW_ORDER}" "{\"resource\": \"$ACME_NEW_ORDER_RES\", \"csr\": \"$der\"}" "needbase64"; then
323febe8 4873 _err "Sign failed. $response"
c1151b0d 4874 _on_issue_err "$_post_hook"
4875 return 1
4876 fi
4877 _rcert="$response"
36a7a840 4878 Le_LinkCert="$(grep -i '^Location.*$' "$HTTP_HEADER" | _tail_n 1 | tr -d "\r\n" | cut -d " " -f 2)"
4c2a3841 4879 echo "$BEGIN_CERT" >"$CERT_PATH"
4c3b3608 4880
72518d48 4881 #if ! _get "$Le_LinkCert" | _base64 "multiline" >> "$CERT_PATH" ; then
4882 # _debug "Get cert failed. Let's try last response."
3c07f57a 4883 # printf -- "%s" "$_rcert" | _dbase64 "multiline" | _base64 "multiline" >> "$CERT_PATH"
72518d48 4884 #fi
4c2a3841 4885
4886 if ! printf -- "%s" "$_rcert" | _dbase64 "multiline" | _base64 "multiline" >>"$CERT_PATH"; then
72518d48 4887 _debug "Try cert link."
4c2a3841 4888 _get "$Le_LinkCert" | _base64 "multiline" >>"$CERT_PATH"
d404e92d 4889 fi
4890
4c2a3841 4891 echo "$END_CERT" >>"$CERT_PATH"
c1151b0d 4892 fi
4893
4894 _debug "Le_LinkCert" "$Le_LinkCert"
4895 _savedomainconf "Le_LinkCert" "$Le_LinkCert"
4896
183063a2 4897 if [ -z "$Le_LinkCert" ] || ! _checkcert "$CERT_PATH"; then
716f7277 4898 response="$(echo "$response" | _dbase64 "multiline" | tr -d '\0' | _normalizeJson)"
183063a2 4899 _err "Sign failed: $(echo "$response" | _egrep_o '"detail":"[^"]*"')"
4900 _on_issue_err "$_post_hook"
4901 return 1
4902 fi
4903
c1151b0d 4904 if [ "$Le_LinkCert" ]; then
43822d37 4905 _info "$(__green "Cert success.")"
4c3b3608 4906 cat "$CERT_PATH"
5980ebc7 4907
4c2a3841 4908 _info "Your cert is in $(__green " $CERT_PATH ")"
4909
4910 if [ -f "$CERT_KEY_PATH" ]; then
4911 _info "Your cert key is in $(__green " $CERT_KEY_PATH ")"
5980ebc7 4912 fi
4913
bd04638d 4914 if [ ! "$USER_PATH" ] || [ ! "$_ACME_IN_CRON" ]; then
281aa349 4915 USER_PATH="$PATH"
4916 _saveaccountconf "USER_PATH" "$USER_PATH"
4917 fi
4c3b3608 4918 fi
4c3b3608 4919
1c35f46b 4920 if [ "$ACME_VERSION" = "2" ]; then
4921 _debug "v2 chain."
4922 else
183063a2 4923 cp "$CERT_PATH" "$CERT_FULLCHAIN_PATH"
1c35f46b 4924 Le_LinkIssuer=$(grep -i '^Link' "$HTTP_HEADER" | _head_n 1 | cut -d " " -f 2 | cut -d ';' -f 1 | tr -d '<>')
d8ba26e6 4925
1c35f46b 4926 if [ "$Le_LinkIssuer" ]; then
4927 if ! _contains "$Le_LinkIssuer" ":"; then
4928 _info "$(__red "Relative issuer link found.")"
4929 Le_LinkIssuer="$_ACME_SERVER_HOST$Le_LinkIssuer"
d8ba26e6 4930 fi
1c35f46b 4931 _debug Le_LinkIssuer "$Le_LinkIssuer"
4932 _savedomainconf "Le_LinkIssuer" "$Le_LinkIssuer"
d8ba26e6 4933
1c35f46b 4934 _link_issuer_retry=0
4935 _MAX_ISSUER_RETRY=5
4936 while [ "$_link_issuer_retry" -lt "$_MAX_ISSUER_RETRY" ]; do
4937 _debug _link_issuer_retry "$_link_issuer_retry"
4938 if [ "$ACME_VERSION" = "2" ]; then
0483d841 4939 if _send_signed_request "$Le_LinkIssuer"; then
4940 echo "$response" >"$CA_CERT_PATH"
1c35f46b 4941 break
4942 fi
4943 else
4944 if _get "$Le_LinkIssuer" >"$CA_CERT_PATH.der"; then
4945 echo "$BEGIN_CERT" >"$CA_CERT_PATH"
4946 _base64 "multiline" <"$CA_CERT_PATH.der" >>"$CA_CERT_PATH"
4947 echo "$END_CERT" >>"$CA_CERT_PATH"
0f120c41 4948 if ! _checkcert "$CA_CERT_PATH"; then
183063a2 4949 _err "Can not get the ca cert."
4950 break
4951 fi
1c35f46b 4952 cat "$CA_CERT_PATH" >>"$CERT_FULLCHAIN_PATH"
4953 rm -f "$CA_CERT_PATH.der"
4954 break
4955 fi
c1151b0d 4956 fi
1c35f46b 4957 _link_issuer_retry=$(_math $_link_issuer_retry + 1)
4958 _sleep "$_link_issuer_retry"
4959 done
4960 if [ "$_link_issuer_retry" = "$_MAX_ISSUER_RETRY" ]; then
4961 _err "Max retry for issuer ca cert is reached."
d8ba26e6 4962 fi
1c35f46b 4963 else
4964 _debug "No Le_LinkIssuer header found."
d8ba26e6 4965 fi
4c3b3608 4966 fi
1c35f46b 4967 [ -f "$CA_CERT_PATH" ] && _info "The intermediate CA cert is in $(__green " $CA_CERT_PATH ")"
4968 [ -f "$CERT_FULLCHAIN_PATH" ] && _info "And the full chain certs is there: $(__green " $CERT_FULLCHAIN_PATH ")"
4c2a3841 4969
3aae1ae3 4970 Le_CertCreateTime=$(_time)
4c2a3841 4971 _savedomainconf "Le_CertCreateTime" "$Le_CertCreateTime"
4972
4973 Le_CertCreateTimeStr=$(date -u)
4974 _savedomainconf "Le_CertCreateTimeStr" "$Le_CertCreateTimeStr"
4975
ec67a1b2 4976 if [ -z "$Le_RenewalDays" ] || [ "$Le_RenewalDays" -lt "0" ]; then
4977 Le_RenewalDays="$DEFAULT_RENEW"
054cb72e 4978 else
4c2a3841 4979 _savedomainconf "Le_RenewalDays" "$Le_RenewalDays"
13d7cae9 4980 fi
4c2a3841 4981
4982 if [ "$CA_BUNDLE" ]; then
78009539
PS
4983 _saveaccountconf CA_BUNDLE "$CA_BUNDLE"
4984 else
4985 _clearaccountconf "CA_BUNDLE"
4986 fi
4987
2aa75f03 4988 if [ "$CA_PATH" ]; then
4989 _saveaccountconf CA_PATH "$CA_PATH"
4990 else
4991 _clearaccountconf "CA_PATH"
4992 fi
78009539 4993
4c2a3841 4994 if [ "$HTTPS_INSECURE" ]; then
fac1e367 4995 _saveaccountconf HTTPS_INSECURE "$HTTPS_INSECURE"
4996 else
4c2a3841 4997 _clearaccountconf "HTTPS_INSECURE"
13d7cae9 4998 fi
00a50605 4999
4c2a3841 5000 if [ "$Le_Listen_V4" ]; then
5001 _savedomainconf "Le_Listen_V4" "$Le_Listen_V4"
50827188 5002 _cleardomainconf Le_Listen_V6
4c2a3841 5003 elif [ "$Le_Listen_V6" ]; then
5004 _savedomainconf "Le_Listen_V6" "$Le_Listen_V6"
50827188 5005 _cleardomainconf Le_Listen_V4
5006 fi
f6dcd989 5007
c4b2e582 5008 if [ "$Le_ForceNewDomainKey" = "1" ]; then
5009 _savedomainconf "Le_ForceNewDomainKey" "$Le_ForceNewDomainKey"
5010 else
5011 _cleardomainconf Le_ForceNewDomainKey
5012 fi
5013
ca7202eb 5014 Le_NextRenewTime=$(_math "$Le_CertCreateTime" + "$Le_RenewalDays" \* 24 \* 60 \* 60)
4c2a3841 5015
ca7202eb 5016 Le_NextRenewTimeStr=$(_time2str "$Le_NextRenewTime")
4c2a3841 5017 _savedomainconf "Le_NextRenewTimeStr" "$Le_NextRenewTimeStr"
5018
ca7202eb 5019 Le_NextRenewTime=$(_math "$Le_NextRenewTime" - 86400)
4c2a3841 5020 _savedomainconf "Le_NextRenewTime" "$Le_NextRenewTime"
f6dcd989 5021
85e1f4ea 5022 if [ "$_real_cert$_real_key$_real_ca$_reload_cmd$_real_fullchain" ]; then
5023 _savedomainconf "Le_RealCertPath" "$_real_cert"
5024 _savedomainconf "Le_RealCACertPath" "$_real_ca"
5025 _savedomainconf "Le_RealKeyPath" "$_real_key"
7690f73e 5026 _savedomainconf "Le_ReloadCmd" "$_reload_cmd" "base64"
85e1f4ea 5027 _savedomainconf "Le_RealFullChainPath" "$_real_fullchain"
ce8dca7a 5028 if ! _installcert "$_main_domain" "$_real_cert" "$_real_key" "$_real_ca" "$_real_fullchain" "$_reload_cmd"; then
5029 return 1
5030 fi
01f54558 5031 fi
4c0d3f1b 5032
ce8dca7a 5033 if ! _on_issue_success "$_post_hook" "$_renew_hook"; then
5034 _err "Call hook error."
5035 return 1
5036 fi
4c3b3608 5037}
5038
d73438a3 5039#in_out_cert out_fullchain out_ca
e3ebd582 5040_split_cert_chain() {
5041 _certf="$1"
5042 _fullchainf="$2"
5043 _caf="$3"
5044 if [ "$(grep -- "$BEGIN_CERT" "$_certf" | wc -l)" -gt "1" ]; then
5045 _debug "Found cert chain"
5046 cat "$_certf" >"$_fullchainf"
5047 _end_n="$(grep -n -- "$END_CERT" "$_fullchainf" | _head_n 1 | cut -d : -f 1)"
5048 _debug _end_n "$_end_n"
5049 sed -n "1,${_end_n}p" "$_fullchainf" >"$_certf"
5050 _end_n="$(_math $_end_n + 1)"
5051 sed -n "${_end_n},9999p" "$_fullchainf" >"$_caf"
5052 fi
5053}
5054
43822d37 5055#domain [isEcc]
4c3b3608 5056renew() {
5057 Le_Domain="$1"
4c2a3841 5058 if [ -z "$Le_Domain" ]; then
2e87e64b 5059 _usage "Usage: $PROJECT_ENTRY --renew --domain <domain.tld> [--ecc]"
4c3b3608 5060 return 1
5061 fi
5062
43822d37 5063 _isEcc="$2"
5064
e799ef29 5065 _initpath "$Le_Domain" "$_isEcc"
43822d37 5066
e2053b22 5067 _info "$(__green "Renew: '$Le_Domain'")"
4c2a3841 5068 if [ ! -f "$DOMAIN_CONF" ]; then
2e87e64b 5069 _info "'$Le_Domain' is not an issued domain, skip."
acae0ac2 5070 return $RENEW_SKIP
4c3b3608 5071 fi
4c2a3841 5072
5073 if [ "$Le_RenewalDays" ]; then
1e6b68f5 5074 _savedomainconf Le_RenewalDays "$Le_RenewalDays"
5075 fi
5076
8663fb7e 5077 . "$DOMAIN_CONF"
c5f1cca3 5078 _debug Le_API "$Le_API"
f2add8de 5079
5080 if [ "$Le_API" = "$LETSENCRYPT_CA_V1" ]; then
5081 _cleardomainconf Le_API
5082 Le_API="$DEFAULT_CA"
5083 fi
5084 if [ "$Le_API" = "$LETSENCRYPT_STAGING_CA_V1" ]; then
5085 _cleardomainconf Le_API
5086 Le_API="$DEFAULT_STAGING_CA"
5087 fi
5088
4c2a3841 5089 if [ "$Le_API" ]; then
48d9a8c1 5090 export ACME_DIRECTORY="$Le_API"
c4236e58 5091 #reload ca configs
5092 ACCOUNT_KEY_PATH=""
5093 ACCOUNT_JSON_PATH=""
5094 CA_CONF=""
5095 _debug3 "initpath again."
5096 _initpath "$Le_Domain" "$_isEcc"
5c48e139 5097 fi
4c2a3841 5098
5099 if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(_time)" -lt "$Le_NextRenewTime" ]; then
e2053b22 5100 _info "Skip, Next renewal time is: $(__green "$Le_NextRenewTimeStr")"
5101 _info "Add '$(__red '--force')' to force to renew."
e799ef29 5102 return "$RENEW_SKIP"
4c3b3608 5103 fi
4c2a3841 5104
bd04638d 5105 if [ "$_ACME_IN_CRON" = "1" ] && [ -z "$Le_CertCreateTime" ]; then
c4d0aec5 5106 _info "Skip invalid cert for: $Le_Domain"
acae0ac2 5107 return $RENEW_SKIP
c4d0aec5 5108 fi
5109
bd04638d 5110 _ACME_IS_RENEW="1"
7690f73e 5111 Le_ReloadCmd="$(_readdomainconf Le_ReloadCmd)"
c7257bcf 5112 Le_PreHook="$(_readdomainconf Le_PreHook)"
5113 Le_PostHook="$(_readdomainconf Le_PostHook)"
5114 Le_RenewHook="$(_readdomainconf Le_RenewHook)"
b7b01999 5115 Le_Preferred_Chain="$(_readdomainconf Le_Preferred_Chain)"
e3ebd582 5116 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 5117 res="$?"
4c2a3841 5118 if [ "$res" != "0" ]; then
e799ef29 5119 return "$res"
a61fe418 5120 fi
4c2a3841 5121
5122 if [ "$Le_DeployHook" ]; then
93bce1b2 5123 _deploy "$Le_Domain" "$Le_DeployHook"
e799ef29 5124 res="$?"
a61fe418 5125 fi
4c2a3841 5126
bd04638d 5127 _ACME_IS_RENEW=""
4c3b3608 5128
e799ef29 5129 return "$res"
4c3b3608 5130}
5131
cc179731 5132#renewAll [stopRenewOnError]
4c3b3608 5133renewAll() {
5134 _initpath
cc179731 5135 _stopRenewOnError="$1"
5136 _debug "_stopRenewOnError" "$_stopRenewOnError"
5137 _ret="0"
b50e701c 5138 _success_msg=""
5139 _error_msg=""
5140 _skipped_msg=""
c6b68551 5141 _error_level=$NOTIFY_LEVEL_SKIP
5142 _notify_code=$RENEW_SKIP
f803c6c0 5143 _set_level=${NOTIFY_LEVEL:-$NOTIFY_LEVEL_DEFAULT}
5144 _debug "_set_level" "$_set_level"
e591d5cf 5145 for di in "${CERT_HOME}"/*.*/; do
5146 _debug di "$di"
44483dba 5147 if ! [ -d "$di" ]; then
2e87e64b 5148 _debug "Not a directory, skip: $di"
3498a585 5149 continue
5150 fi
e591d5cf 5151 d=$(basename "$di")
201aa244 5152 _debug d "$d"
43822d37 5153 (
201aa244 5154 if _endswith "$d" "$ECC_SUFFIX"; then
5155 _isEcc=$(echo "$d" | cut -d "$ECC_SEP" -f 2)
5156 d=$(echo "$d" | cut -d "$ECC_SEP" -f 1)
43822d37 5157 fi
5158 renew "$d" "$_isEcc"
4d2f38b0 5159 )
cc179731 5160 rc="$?"
5161 _debug "Return code: $rc"
c6b68551 5162 if [ "$rc" = "0" ]; then
5163 if [ $_error_level -gt $NOTIFY_LEVEL_RENEW ]; then
5164 _error_level="$NOTIFY_LEVEL_RENEW"
5165 _notify_code=0
5166 fi
bd04638d 5167 if [ "$_ACME_IN_CRON" ]; then
f803c6c0 5168 if [ $_set_level -ge $NOTIFY_LEVEL_RENEW ]; then
c6b68551 5169 if [ "$NOTIFY_MODE" = "$NOTIFY_MODE_CERT" ]; then
5170 _send_notify "Renew $d success" "Good, the cert is renewed." "$NOTIFY_HOOK" 0
5171 fi
5172 fi
5173 fi
5174 _success_msg="${_success_msg} $d
b50e701c 5175"
c6b68551 5176 elif [ "$rc" = "$RENEW_SKIP" ]; then
5177 if [ $_error_level -gt $NOTIFY_LEVEL_SKIP ]; then
5178 _error_level="$NOTIFY_LEVEL_SKIP"
5179 _notify_code=$RENEW_SKIP
5180 fi
bd04638d 5181 if [ "$_ACME_IN_CRON" ]; then
f803c6c0 5182 if [ $_set_level -ge $NOTIFY_LEVEL_SKIP ]; then
c6b68551 5183 if [ "$NOTIFY_MODE" = "$NOTIFY_MODE_CERT" ]; then
5184 _send_notify "Renew $d skipped" "Good, the cert is skipped." "$NOTIFY_HOOK" "$RENEW_SKIP"
5185 fi
b50e701c 5186 fi
cc179731 5187 fi
c6b68551 5188 _info "Skipped $d"
5189 _skipped_msg="${_skipped_msg} $d
5190"
b50e701c 5191 else
c6b68551 5192 if [ $_error_level -gt $NOTIFY_LEVEL_ERROR ]; then
5193 _error_level="$NOTIFY_LEVEL_ERROR"
5194 _notify_code=1
5195 fi
bd04638d 5196 if [ "$_ACME_IN_CRON" ]; then
f803c6c0 5197 if [ $_set_level -ge $NOTIFY_LEVEL_ERROR ]; then
c6b68551 5198 if [ "$NOTIFY_MODE" = "$NOTIFY_MODE_CERT" ]; then
5199 _send_notify "Renew $d error" "There is an error." "$NOTIFY_HOOK" 1
5200 fi
5201 fi
5202 fi
5203 _error_msg="${_error_msg} $d
b50e701c 5204"
c6b68551 5205 if [ "$_stopRenewOnError" ]; then
5206 _err "Error renew $d, stop now."
5207 _ret="$rc"
5208 break
5209 else
5210 _ret="$rc"
5211 _err "Error renew $d."
5212 fi
cc179731 5213 fi
4c3b3608 5214 done
c6b68551 5215 _debug _error_level "$_error_level"
a2738e85 5216 _debug _set_level "$_set_level"
bd04638d 5217 if [ "$_ACME_IN_CRON" ] && [ $_error_level -le $_set_level ]; then
b50e701c 5218 if [ -z "$NOTIFY_MODE" ] || [ "$NOTIFY_MODE" = "$NOTIFY_MODE_BULK" ]; then
5219 _msg_subject="Renew"
5220 if [ "$_error_msg" ]; then
5221 _msg_subject="${_msg_subject} Error"
c6b68551 5222 _msg_data="Error certs:
5223${_error_msg}
5224"
b50e701c 5225 fi
5226 if [ "$_success_msg" ]; then
5227 _msg_subject="${_msg_subject} Success"
c6b68551 5228 _msg_data="${_msg_data}Success certs:
5229${_success_msg}
5230"
b50e701c 5231 fi
5232 if [ "$_skipped_msg" ]; then
5233 _msg_subject="${_msg_subject} Skipped"
c6b68551 5234 _msg_data="${_msg_data}Skipped certs:
5235${_skipped_msg}
b50e701c 5236"
c6b68551 5237 fi
5238
5239 _send_notify "$_msg_subject" "$_msg_data" "$NOTIFY_HOOK" "$_notify_code"
b50e701c 5240 fi
5241 fi
5242
201aa244 5243 return "$_ret"
4c3b3608 5244}
5245
10afcaca 5246#csr webroot
4c2a3841 5247signcsr() {
10afcaca 5248 _csrfile="$1"
5249 _csrW="$2"
5250 if [ -z "$_csrfile" ] || [ -z "$_csrW" ]; then
2e87e64b 5251 _usage "Usage: $PROJECT_ENTRY --sign-csr --csr <csr-file> --webroot <directory>"
10afcaca 5252 return 1
5253 fi
5254
875625b1 5255 _real_cert="$3"
5256 _real_key="$4"
5257 _real_ca="$5"
5258 _reload_cmd="$6"
5259 _real_fullchain="$7"
5260 _pre_hook="${8}"
5261 _post_hook="${9}"
5262 _renew_hook="${10}"
5263 _local_addr="${11}"
5264 _challenge_alias="${12}"
5265
10afcaca 5266 _csrsubj=$(_readSubjectFromCSR "$_csrfile")
4c2a3841 5267 if [ "$?" != "0" ]; then
10afcaca 5268 _err "Can not read subject from csr: $_csrfile"
5269 return 1
5270 fi
ad752b31 5271 _debug _csrsubj "$_csrsubj"
2c9ed4c5 5272 if _contains "$_csrsubj" ' ' || ! _contains "$_csrsubj" '.'; then
5273 _info "It seems that the subject: $_csrsubj is not a valid domain name. Drop it."
5274 _csrsubj=""
5275 fi
10afcaca 5276
5277 _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile")
4c2a3841 5278 if [ "$?" != "0" ]; then
10afcaca 5279 _err "Can not read domain list from csr: $_csrfile"
5280 return 1
5281 fi
5282 _debug "_csrdomainlist" "$_csrdomainlist"
4c2a3841 5283
5284 if [ -z "$_csrsubj" ]; then
ad752b31 5285 _csrsubj="$(_getfield "$_csrdomainlist" 1)"
5286 _debug _csrsubj "$_csrsubj"
5287 _csrdomainlist="$(echo "$_csrdomainlist" | cut -d , -f 2-)"
5288 _debug "_csrdomainlist" "$_csrdomainlist"
5289 fi
4c2a3841 5290
5291 if [ -z "$_csrsubj" ]; then
ad752b31 5292 _err "Can not read subject from csr: $_csrfile"
5293 return 1
5294 fi
4c2a3841 5295
10afcaca 5296 _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile")
4c2a3841 5297 if [ "$?" != "0" ] || [ -z "$_csrkeylength" ]; then
10afcaca 5298 _err "Can not read key length from csr: $_csrfile"
5299 return 1
5300 fi
4c2a3841 5301
cd9fb3b6 5302 if [ -z "$ACME_VERSION" ] && _contains "$_csrsubj,$_csrdomainlist" "*."; then
5303 export ACME_VERSION=2
5304 fi
10afcaca 5305 _initpath "$_csrsubj" "$_csrkeylength"
5306 mkdir -p "$DOMAIN_PATH"
4c2a3841 5307
10afcaca 5308 _info "Copy csr to: $CSR_PATH"
5309 cp "$_csrfile" "$CSR_PATH"
4c2a3841 5310
875625b1 5311 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 5312
10afcaca 5313}
5314
5315showcsr() {
4c2a3841 5316 _csrfile="$1"
10afcaca 5317 _csrd="$2"
5318 if [ -z "$_csrfile" ] && [ -z "$_csrd" ]; then
2e87e64b 5319 _usage "Usage: $PROJECT_ENTRY --show-csr --csr <csr-file>"
10afcaca 5320 return 1
5321 fi
5322
5323 _initpath
4c2a3841 5324
10afcaca 5325 _csrsubj=$(_readSubjectFromCSR "$_csrfile")
4c2a3841 5326 if [ "$?" != "0" ] || [ -z "$_csrsubj" ]; then
10afcaca 5327 _err "Can not read subject from csr: $_csrfile"
5328 return 1
5329 fi
4c2a3841 5330
10afcaca 5331 _info "Subject=$_csrsubj"
5332
5333 _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile")
4c2a3841 5334 if [ "$?" != "0" ]; then
10afcaca 5335 _err "Can not read domain list from csr: $_csrfile"
5336 return 1
5337 fi
5338 _debug "_csrdomainlist" "$_csrdomainlist"
5339
5340 _info "SubjectAltNames=$_csrdomainlist"
5341
10afcaca 5342 _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile")
4c2a3841 5343 if [ "$?" != "0" ] || [ -z "$_csrkeylength" ]; then
10afcaca 5344 _err "Can not read key length from csr: $_csrfile"
5345 return 1
5346 fi
5347 _info "KeyLength=$_csrkeylength"
5348}
5349
95ef046d 5350#listraw domain
6d7eda3e 5351list() {
22ea4004 5352 _raw="$1"
95ef046d 5353 _domain="$2"
6d7eda3e 5354 _initpath
4c2a3841 5355
dcf4f8f6 5356 _sep="|"
4c2a3841 5357 if [ "$_raw" ]; then
95ef046d 5358 if [ -z "$_domain" ]; then
5359 printf "%s\n" "Main_Domain${_sep}KeyLength${_sep}SAN_Domains${_sep}CA${_sep}Created${_sep}Renew"
5360 fi
e591d5cf 5361 for di in "${CERT_HOME}"/*.*/; do
5362 d=$(basename "$di")
201aa244 5363 _debug d "$d"
dcf4f8f6 5364 (
201aa244 5365 if _endswith "$d" "$ECC_SUFFIX"; then
be0df07d 5366 _isEcc="ecc"
201aa244 5367 d=$(echo "$d" | cut -d "$ECC_SEP" -f 1)
43822d37 5368 fi
be0df07d 5369 DOMAIN_CONF="$di/$d.conf"
4c2a3841 5370 if [ -f "$DOMAIN_CONF" ]; then
dcf4f8f6 5371 . "$DOMAIN_CONF"
269847d1 5372 _ca="$(_getCAShortName "$Le_API")"
95ef046d 5373 if [ -z "$_domain" ]; then
5374 printf "%s\n" "$Le_Domain${_sep}\"$Le_Keylength\"${_sep}$Le_Alt${_sep}$_ca${_sep}$Le_CertCreateTimeStr${_sep}$Le_NextRenewTimeStr"
5375 else
5376 if [ "$_domain" = "$d" ]; then
5377 cat "$DOMAIN_CONF"
5378 fi
5379 fi
dcf4f8f6 5380 fi
5381 )
5382 done
5383 else
4c2a3841 5384 if _exists column; then
95ef046d 5385 list "raw" "$_domain" | column -t -s "$_sep"
22ea4004 5386 else
95ef046d 5387 list "raw" "$_domain" | tr "$_sep" '\t'
22ea4004 5388 fi
dcf4f8f6 5389 fi
6d7eda3e 5390
6d7eda3e 5391}
5392
93bce1b2 5393_deploy() {
5394 _d="$1"
5395 _hooks="$2"
5396
5397 for _d_api in $(echo "$_hooks" | tr ',' " "); do
b50e701c 5398 _deployApi="$(_findHook "$_d" $_SUB_FOLDER_DEPLOY "$_d_api")"
93bce1b2 5399 if [ -z "$_deployApi" ]; then
5400 _err "The deploy hook $_d_api is not found."
5401 return 1
5402 fi
5403 _debug _deployApi "$_deployApi"
5404
5405 if ! (
5406 if ! . "$_deployApi"; then
5407 _err "Load file $_deployApi error. Please check your api file and try again."
5408 return 1
5409 fi
5410
5411 d_command="${_d_api}_deploy"
5412 if ! _exists "$d_command"; then
5413 _err "It seems that your api file is not correct, it must have a function named: $d_command"
5414 return 1
5415 fi
5416
5417 if ! $d_command "$_d" "$CERT_KEY_PATH" "$CERT_PATH" "$CA_CERT_PATH" "$CERT_FULLCHAIN_PATH"; then
5418 _err "Error deploy for domain:$_d"
5419 return 1
5420 fi
5421 ); then
5422 _err "Deploy error."
5423 return 1
5424 else
5425 _info "$(__green Success)"
5426 fi
5427 done
5428}
5429
5430#domain hooks
a61fe418 5431deploy() {
93bce1b2 5432 _d="$1"
5433 _hooks="$2"
a61fe418 5434 _isEcc="$3"
93bce1b2 5435 if [ -z "$_hooks" ]; then
2e87e64b 5436 _usage "Usage: $PROJECT_ENTRY --deploy --domain <domain.tld> --deploy-hook <hookname> [--ecc] "
a61fe418 5437 return 1
5438 fi
5439
93bce1b2 5440 _initpath "$_d" "$_isEcc"
4c2a3841 5441 if [ ! -d "$DOMAIN_PATH" ]; then
9672c6b8 5442 _err "The domain '$_d' is not a cert name. You must use the cert name to specify the cert to install."
5443 _err "Can not find path:'$DOMAIN_PATH'"
a61fe418 5444 return 1
5445 fi
4c2a3841 5446
93bce1b2 5447 . "$DOMAIN_CONF"
4c2a3841 5448
93bce1b2 5449 _savedomainconf Le_DeployHook "$_hooks"
4c2a3841 5450
93bce1b2 5451 _deploy "$_d" "$_hooks"
a61fe418 5452}
5453
4c3b3608 5454installcert() {
85e1f4ea 5455 _main_domain="$1"
5456 if [ -z "$_main_domain" ]; then
2e87e64b 5457 _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 5458 return 1
5459 fi
5460
85e1f4ea 5461 _real_cert="$2"
5462 _real_key="$3"
5463 _real_ca="$4"
5464 _reload_cmd="$5"
5465 _real_fullchain="$6"
43822d37 5466 _isEcc="$7"
5467
85e1f4ea 5468 _initpath "$_main_domain" "$_isEcc"
4c2a3841 5469 if [ ! -d "$DOMAIN_PATH" ]; then
9672c6b8 5470 _err "The domain '$_main_domain' is not a cert name. You must use the cert name to specify the cert to install."
5471 _err "Can not find path:'$DOMAIN_PATH'"
43822d37 5472 return 1
5473 fi
5474
85e1f4ea 5475 _savedomainconf "Le_RealCertPath" "$_real_cert"
5476 _savedomainconf "Le_RealCACertPath" "$_real_ca"
5477 _savedomainconf "Le_RealKeyPath" "$_real_key"
7690f73e 5478 _savedomainconf "Le_ReloadCmd" "$_reload_cmd" "base64"
85e1f4ea 5479 _savedomainconf "Le_RealFullChainPath" "$_real_fullchain"
5480
044da37c 5481 _installcert "$_main_domain" "$_real_cert" "$_real_key" "$_real_ca" "$_real_fullchain" "$_reload_cmd"
43822d37 5482}
4c3b3608 5483
044da37c 5484#domain cert key ca fullchain reloadcmd backup-prefix
43822d37 5485_installcert() {
85e1f4ea 5486 _main_domain="$1"
5487 _real_cert="$2"
5488 _real_key="$3"
5489 _real_ca="$4"
044da37c 5490 _real_fullchain="$5"
5491 _reload_cmd="$6"
5492 _backup_prefix="$7"
4c3b3608 5493
85e1f4ea 5494 if [ "$_real_cert" = "$NO_VALUE" ]; then
5495 _real_cert=""
4d2f38b0 5496 fi
85e1f4ea 5497 if [ "$_real_key" = "$NO_VALUE" ]; then
5498 _real_key=""
4d2f38b0 5499 fi
85e1f4ea 5500 if [ "$_real_ca" = "$NO_VALUE" ]; then
5501 _real_ca=""
4d2f38b0 5502 fi
85e1f4ea 5503 if [ "$_reload_cmd" = "$NO_VALUE" ]; then
5504 _reload_cmd=""
4d2f38b0 5505 fi
85e1f4ea 5506 if [ "$_real_fullchain" = "$NO_VALUE" ]; then
5507 _real_fullchain=""
4d2f38b0 5508 fi
4c2a3841 5509
044da37c 5510 _backup_path="$DOMAIN_BACKUP_PATH/$_backup_prefix"
5511 mkdir -p "$_backup_path"
5512
85e1f4ea 5513 if [ "$_real_cert" ]; then
5514 _info "Installing cert to:$_real_cert"
bd04638d 5515 if [ -f "$_real_cert" ] && [ ! "$_ACME_IS_RENEW" ]; then
044da37c 5516 cp "$_real_cert" "$_backup_path/cert.bak"
4c3b3608 5517 fi
206be3c1 5518 cat "$CERT_PATH" >"$_real_cert" || return 1
4c3b3608 5519 fi
4c2a3841 5520
85e1f4ea 5521 if [ "$_real_ca" ]; then
5522 _info "Installing CA to:$_real_ca"
5523 if [ "$_real_ca" = "$_real_cert" ]; then
5524 echo "" >>"$_real_ca"
206be3c1 5525 cat "$CA_CERT_PATH" >>"$_real_ca" || return 1
4c3b3608 5526 else
bd04638d 5527 if [ -f "$_real_ca" ] && [ ! "$_ACME_IS_RENEW" ]; then
044da37c 5528 cp "$_real_ca" "$_backup_path/ca.bak"
78552b18 5529 fi
206be3c1 5530 cat "$CA_CERT_PATH" >"$_real_ca" || return 1
4c3b3608 5531 fi
5532 fi
5533
85e1f4ea 5534 if [ "$_real_key" ]; then
5535 _info "Installing key to:$_real_key"
bd04638d 5536 if [ -f "$_real_key" ] && [ ! "$_ACME_IS_RENEW" ]; then
044da37c 5537 cp "$_real_key" "$_backup_path/key.bak"
4c3b3608 5538 fi
82014583 5539 if [ -f "$_real_key" ]; then
206be3c1 5540 cat "$CERT_KEY_PATH" >"$_real_key" || return 1
82014583 5541 else
206be3c1 5542 cat "$CERT_KEY_PATH" >"$_real_key" || return 1
7b92371a 5543 chmod 600 "$_real_key"
82014583 5544 fi
4c3b3608 5545 fi
4c2a3841 5546
85e1f4ea 5547 if [ "$_real_fullchain" ]; then
5548 _info "Installing full chain to:$_real_fullchain"
bd04638d 5549 if [ -f "$_real_fullchain" ] && [ ! "$_ACME_IS_RENEW" ]; then
044da37c 5550 cp "$_real_fullchain" "$_backup_path/fullchain.bak"
a63b05a9 5551 fi
206be3c1 5552 cat "$CERT_FULLCHAIN_PATH" >"$_real_fullchain" || return 1
4c2a3841 5553 fi
4c3b3608 5554
85e1f4ea 5555 if [ "$_reload_cmd" ]; then
5556 _info "Run reload cmd: $_reload_cmd"
25555b8c 5557 if (
839bf0e2 5558 export CERT_PATH
5559 export CERT_KEY_PATH
5560 export CA_CERT_PATH
5561 export CERT_FULLCHAIN_PATH
b3f61297 5562 export Le_Domain="$_main_domain"
85e1f4ea 5563 cd "$DOMAIN_PATH" && eval "$_reload_cmd"
839bf0e2 5564 ); then
43822d37 5565 _info "$(__green "Reload success")"
4d2f38b0 5566 else
5567 _err "Reload error for :$Le_Domain"
5568 fi
5569 fi
5570
4c3b3608 5571}
5572
77f96b38 5573__read_password() {
5574 unset _pp
5575 prompt="Enter Password:"
0b04a7f1 5576 while IFS= read -p "$prompt" -r -s -n 1 char; do
5577 if [ "$char" = $'\0' ]; then
4ebad105 5578 break
0b04a7f1 5579 fi
5580 prompt='*'
5581 _pp="$_pp$char"
77f96b38 5582 done
5583 echo "$_pp"
5584}
5585
5586_install_win_taskscheduler() {
5587 _lesh="$1"
5588 _centry="$2"
5589 _randomminute="$3"
5590 if ! _exists cygpath; then
5591 _err "cygpath not found"
5592 return 1
5593 fi
5594 if ! _exists schtasks; then
5595 _err "schtasks.exe is not found, are you on Windows?"
5596 return 1
5597 fi
5598 _winbash="$(cygpath -w $(which bash))"
5599 _debug _winbash "$_winbash"
5600 if [ -z "$_winbash" ]; then
5601 _err "can not find bash path"
5602 return 1
5603 fi
5604 _myname="$(whoami)"
5605 _debug "_myname" "$_myname"
5606 if [ -z "$_myname" ]; then
5607 _err "can not find my user name"
5608 return 1
5609 fi
5610 _debug "_lesh" "$_lesh"
5611
5612 _info "To install scheduler task in your Windows account, you must input your windows password."
5613 _info "$PROJECT_NAME doesn't save your password."
5614 _info "Please input your Windows password for: $(__green "$_myname")"
5615 _password="$(__read_password)"
5616 #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
5617 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
5618 echo
5619
5620}
5621
5622_uninstall_win_taskscheduler() {
5623 if ! _exists schtasks; then
5624 _err "schtasks.exe is not found, are you on Windows?"
5625 return 1
5626 fi
5627 if ! echo SCHTASKS /query /tn "$_WINDOWS_SCHEDULER_NAME" | cmd.exe >/dev/null; then
5628 _debug "scheduler $_WINDOWS_SCHEDULER_NAME is not found."
5629 else
5630 _info "Removing $_WINDOWS_SCHEDULER_NAME"
0b04a7f1 5631 echo SCHTASKS /delete /f /tn "$_WINDOWS_SCHEDULER_NAME" | cmd.exe >/dev/null
77f96b38 5632 fi
5633}
5634
27dbe77f 5635#confighome
4c3b3608 5636installcronjob() {
27dbe77f 5637 _c_home="$1"
4c3b3608 5638 _initpath
415f375c 5639 _CRONTAB="crontab"
77f96b38 5640 if [ -f "$LE_WORKING_DIR/$PROJECT_ENTRY" ]; then
5641 lesh="\"$LE_WORKING_DIR\"/$PROJECT_ENTRY"
5642 else
5643 _err "Can not install cronjob, $PROJECT_ENTRY not found."
5644 return 1
5645 fi
5646 if [ "$_c_home" ]; then
5647 _c_entry="--config-home \"$_c_home\" "
5648 fi
5649 _t=$(_time)
5650 random_minute=$(_math $_t % 60)
5651
415f375c 5652 if ! _exists "$_CRONTAB" && _exists "fcrontab"; then
5653 _CRONTAB="fcrontab"
5654 fi
77f96b38 5655
415f375c 5656 if ! _exists "$_CRONTAB"; then
77f96b38 5657 if _exists cygpath && _exists schtasks.exe; then
5658 _info "It seems you are on Windows, let's install Windows scheduler task."
5659 if _install_win_taskscheduler "$lesh" "$_c_entry" "$random_minute"; then
5660 _info "Install Windows scheduler task success."
5661 return 0
5662 else
5663 _err "Install Windows scheduler task failed."
5664 return 1
5665 fi
5666 fi
415f375c 5667 _err "crontab/fcrontab doesn't exist, so, we can not install cron jobs."
77546ea5 5668 _err "All your certs will not be renewed automatically."
a7b7355d 5669 _err "You must add your own cron job to call '$PROJECT_ENTRY --cron' everyday."
77546ea5 5670 return 1
5671 fi
4c3b3608 5672 _info "Installing cron job"
415f375c 5673 if ! $_CRONTAB -l | grep "$PROJECT_ENTRY --cron"; then
e2c939fb 5674 if _exists uname && uname -a | grep SunOS >/dev/null; then
415f375c 5675 $_CRONTAB -l | {
4c2a3841 5676 cat
0533bde9 5677 echo "$random_minute 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" $_c_entry> /dev/null"
415f375c 5678 } | $_CRONTAB --
22ea4004 5679 else
415f375c 5680 $_CRONTAB -l | {
4c2a3841 5681 cat
0533bde9 5682 echo "$random_minute 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" $_c_entry> /dev/null"
415f375c 5683 } | $_CRONTAB -
22ea4004 5684 fi
4c3b3608 5685 fi
4c2a3841 5686 if [ "$?" != "0" ]; then
4c3b3608 5687 _err "Install cron job failed. You need to manually renew your certs."
5688 _err "Or you can add cronjob by yourself:"
a7b7355d 5689 _err "$lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null"
4c3b3608 5690 return 1
5691 fi
5692}
5693
5694uninstallcronjob() {
415f375c 5695 _CRONTAB="crontab"
5696 if ! _exists "$_CRONTAB" && _exists "fcrontab"; then
5697 _CRONTAB="fcrontab"
5698 fi
5699
5700 if ! _exists "$_CRONTAB"; then
77f96b38 5701 if _exists cygpath && _exists schtasks.exe; then
5702 _info "It seems you are on Windows, let's uninstall Windows scheduler task."
5703 if _uninstall_win_taskscheduler; then
5704 _info "Uninstall Windows scheduler task success."
5705 return 0
5706 else
5707 _err "Uninstall Windows scheduler task failed."
5708 return 1
5709 fi
5710 fi
37db5b81 5711 return
5712 fi
4c3b3608 5713 _info "Removing cron job"
415f375c 5714 cr="$($_CRONTAB -l | grep "$PROJECT_ENTRY --cron")"
4c2a3841 5715 if [ "$cr" ]; then
5716 if _exists uname && uname -a | grep solaris >/dev/null; then
415f375c 5717 $_CRONTAB -l | sed "/$PROJECT_ENTRY --cron/d" | $_CRONTAB --
22ea4004 5718 else
415f375c 5719 $_CRONTAB -l | sed "/$PROJECT_ENTRY --cron/d" | $_CRONTAB -
22ea4004 5720 fi
a7b7355d 5721 LE_WORKING_DIR="$(echo "$cr" | cut -d ' ' -f 9 | tr -d '"')"
4c3b3608 5722 _info LE_WORKING_DIR "$LE_WORKING_DIR"
27dbe77f 5723 if _contains "$cr" "--config-home"; then
f5b546b3 5724 LE_CONFIG_HOME="$(echo "$cr" | cut -d ' ' -f 11 | tr -d '"')"
5725 _debug LE_CONFIG_HOME "$LE_CONFIG_HOME"
27dbe77f 5726 fi
4c2a3841 5727 fi
4c3b3608 5728 _initpath
a7b7355d 5729
4c3b3608 5730}
5731
1041c9f9 5732#domain isECC revokeReason
6cb415f5 5733revoke() {
5734 Le_Domain="$1"
4c2a3841 5735 if [ -z "$Le_Domain" ]; then
2e87e64b 5736 _usage "Usage: $PROJECT_ENTRY --revoke --domain <domain.tld> [--ecc]"
6cb415f5 5737 return 1
5738 fi
4c2a3841 5739
43822d37 5740 _isEcc="$2"
1041c9f9 5741 _reason="$3"
5742 if [ -z "$_reason" ]; then
5743 _reason="0"
5744 fi
c4a375b3 5745 _initpath "$Le_Domain" "$_isEcc"
4c2a3841 5746 if [ ! -f "$DOMAIN_CONF" ]; then
6cb415f5 5747 _err "$Le_Domain is not a issued domain, skip."
4c2a3841 5748 return 1
6cb415f5 5749 fi
4c2a3841 5750
5751 if [ ! -f "$CERT_PATH" ]; then
6cb415f5 5752 _err "Cert for $Le_Domain $CERT_PATH is not found, skip."
5753 return 1
5754 fi
6cb415f5 5755
11927a76 5756 cert="$(_getfile "${CERT_PATH}" "${BEGIN_CERT}" "${END_CERT}" | tr -d "\r\n" | _url_replace)"
4c2a3841 5757
5758 if [ -z "$cert" ]; then
6cb415f5 5759 _err "Cert for $Le_Domain is empty found, skip."
5760 return 1
5761 fi
4c2a3841 5762
48d9a8c1 5763 _initAPI
5764
d2cde379 5765 if [ "$ACME_VERSION" = "2" ]; then
1041c9f9 5766 data="{\"certificate\": \"$cert\",\"reason\":$_reason}"
d2cde379 5767 else
5768 data="{\"resource\": \"revoke-cert\", \"certificate\": \"$cert\"}"
5769 fi
48d9a8c1 5770 uri="${ACME_REVOKE_CERT}"
6cb415f5 5771
4c2a3841 5772 if [ -f "$CERT_KEY_PATH" ]; then
1befee5a 5773 _info "Try domain key first."
c4a375b3 5774 if _send_signed_request "$uri" "$data" "" "$CERT_KEY_PATH"; then
4c2a3841 5775 if [ -z "$response" ]; then
1befee5a 5776 _info "Revoke success."
c4a375b3 5777 rm -f "$CERT_PATH"
1befee5a 5778 return 0
4c2a3841 5779 else
1befee5a 5780 _err "Revoke error by domain key."
5781 _err "$response"
5782 fi
6cb415f5 5783 fi
4c2a3841 5784 else
eca57bee 5785 _info "Domain key file doesn't exist."
6cb415f5 5786 fi
6cb415f5 5787
1befee5a 5788 _info "Try account key."
6cb415f5 5789
c4a375b3 5790 if _send_signed_request "$uri" "$data" "" "$ACCOUNT_KEY_PATH"; then
4c2a3841 5791 if [ -z "$response" ]; then
6cb415f5 5792 _info "Revoke success."
c4a375b3 5793 rm -f "$CERT_PATH"
6cb415f5 5794 return 0
4c2a3841 5795 else
6cb415f5 5796 _err "Revoke error."
c9c31c04 5797 _debug "$response"
6cb415f5 5798 fi
5799 fi
5800 return 1
5801}
4c3b3608 5802
78f0201d 5803#domain ecc
5804remove() {
5805 Le_Domain="$1"
5806 if [ -z "$Le_Domain" ]; then
2e87e64b 5807 _usage "Usage: $PROJECT_ENTRY --remove --domain <domain.tld> [--ecc]"
78f0201d 5808 return 1
5809 fi
5810
5811 _isEcc="$2"
5812
5813 _initpath "$Le_Domain" "$_isEcc"
5814 _removed_conf="$DOMAIN_CONF.removed"
5815 if [ ! -f "$DOMAIN_CONF" ]; then
5816 if [ -f "$_removed_conf" ]; then
5817 _err "$Le_Domain is already removed, You can remove the folder by yourself: $DOMAIN_PATH"
5818 else
5819 _err "$Le_Domain is not a issued domain, skip."
5820 fi
5821 return 1
5822 fi
5823
5824 if mv "$DOMAIN_CONF" "$_removed_conf"; then
68aea3af 5825 _info "$Le_Domain is removed, the key and cert files are in $(__green $DOMAIN_PATH)"
78f0201d 5826 _info "You can remove them by yourself."
5827 return 0
5828 else
5829 _err "Remove $Le_Domain failed."
5830 return 1
5831 fi
5832}
5833
0c00e870 5834#domain vtype
5835_deactivate() {
5836 _d_domain="$1"
5837 _d_type="$2"
5838 _initpath
4c2a3841 5839
d2cde379 5840 if [ "$ACME_VERSION" = "2" ]; then
5841 _identifiers="{\"type\":\"dns\",\"value\":\"$_d_domain\"}"
5842 if ! _send_signed_request "$ACME_NEW_ORDER" "{\"identifiers\": [$_identifiers]}"; then
5843 _err "Can not get domain new order."
5844 return 1
5845 fi
d0d74907 5846 _authorizations_seg="$(echo "$response" | _egrep_o '"authorizations" *: *\[[^\]*\]' | cut -d '[' -f 2 | tr -d ']' | tr -d '"')"
d2cde379 5847 _debug2 _authorizations_seg "$_authorizations_seg"
5848 if [ -z "$_authorizations_seg" ]; then
5849 _err "_authorizations_seg not found."
5850 _clearup
5851 _on_issue_err "$_post_hook"
5852 return 1
5853 fi
4c2a3841 5854
d2cde379 5855 authzUri="$_authorizations_seg"
5856 _debug2 "authzUri" "$authzUri"
0483d841 5857 if ! _send_signed_request "$authzUri"; then
d2cde379 5858 _err "get to authz error."
263c38ca 5859 _err "_authorizations_seg" "$_authorizations_seg"
5860 _err "authzUri" "$authzUri"
d2cde379 5861 _clearup
5862 _on_issue_err "$_post_hook"
5863 return 1
5864 fi
4c2a3841 5865
d2cde379 5866 response="$(echo "$response" | _normalizeJson)"
5867 _debug2 response "$response"
5868 _URL_NAME="url"
5869 else
5870 if ! __get_domain_new_authz "$_d_domain"; then
5871 _err "Can not get domain new authz token."
5872 return 1
5873 fi
5874
0712e989 5875 authzUri="$(echo "$responseHeaders" | grep "^Location:" | _head_n 1 | cut -d ':' -f 2- | tr -d "\r\n")"
d2cde379 5876 _debug "authzUri" "$authzUri"
5877 if [ "$code" ] && [ ! "$code" = '201' ]; then
5878 _err "new-authz error: $response"
5879 return 1
5880 fi
5881 _URL_NAME="uri"
14d7bfda 5882 fi
0c00e870 5883
cc8f2afc 5884 entries="$(echo "$response" | tr '][' '==' | _egrep_o "challenges\": *=[^=]*=" | tr '}{' '\n' | grep "\"status\": *\"valid\"")"
14d7bfda 5885 if [ -z "$entries" ]; then
5886 _info "No valid entries found."
5887 if [ -z "$thumbprint" ]; then
5888 thumbprint="$(__calc_account_thumbprint)"
5889 fi
5890 _debug "Trigger validation."
d2cde379 5891 vtype="$VTYPE_DNS"
d0d74907 5892 entry="$(echo "$response" | _egrep_o '[^\{]*"type":"'$vtype'"[^\}]*')"
14d7bfda 5893 _debug entry "$entry"
5894 if [ -z "$entry" ]; then
5895 _err "Error, can not get domain token $d"
0c00e870 5896 return 1
5897 fi
d0d74907 5898 token="$(echo "$entry" | _egrep_o '"token":"[^"]*' | cut -d : -f 2 | tr -d '"')"
14d7bfda 5899 _debug token "$token"
4c2a3841 5900
d0d74907 5901 uri="$(echo "$entry" | _egrep_o "\"$_URL_NAME\":\"[^\"]*" | cut -d : -f 2,3 | tr -d '"')"
14d7bfda 5902 _debug uri "$uri"
5903
5904 keyauthorization="$token.$thumbprint"
5905 _debug keyauthorization "$keyauthorization"
5906 __trigger_validation "$uri" "$keyauthorization"
5907
5908 fi
5909
5910 _d_i=0
5911 _d_max_retry=$(echo "$entries" | wc -l)
5912 while [ "$_d_i" -lt "$_d_max_retry" ]; do
5913 _info "Deactivate: $_d_domain"
5914 _d_i="$(_math $_d_i + 1)"
5915 entry="$(echo "$entries" | sed -n "${_d_i}p")"
0c00e870 5916 _debug entry "$entry"
4c2a3841 5917
5918 if [ -z "$entry" ]; then
fb2029e7 5919 _info "No more valid entry found."
0c00e870 5920 break
5921 fi
4c2a3841 5922
d0d74907 5923 _vtype="$(echo "$entry" | _egrep_o '"type": *"[^"]*"' | cut -d : -f 2 | tr -d '"')"
c4a375b3 5924 _debug _vtype "$_vtype"
0c00e870 5925 _info "Found $_vtype"
5926
199ca77c 5927 uri="$(echo "$entry" | _egrep_o "\"$_URL_NAME\":\"[^\"]*\"" | tr -d '" ' | cut -d : -f 2-)"
c4a375b3 5928 _debug uri "$uri"
4c2a3841 5929
5930 if [ "$_d_type" ] && [ "$_d_type" != "$_vtype" ]; then
0c00e870 5931 _info "Skip $_vtype"
5932 continue
5933 fi
4c2a3841 5934
0c00e870 5935 _info "Deactivate: $_vtype"
4c2a3841 5936
d2cde379 5937 if [ "$ACME_VERSION" = "2" ]; then
5938 _djson="{\"status\":\"deactivated\"}"
5939 else
5940 _djson="{\"resource\": \"authz\", \"status\":\"deactivated\"}"
5941 fi
5942
5943 if _send_signed_request "$authzUri" "$_djson" && _contains "$response" '"deactivated"'; then
14d7bfda 5944 _info "Deactivate: $_vtype success."
5945 else
0c00e870 5946 _err "Can not deactivate $_vtype."
14d7bfda 5947 break
0c00e870 5948 fi
4c2a3841 5949
0c00e870 5950 done
5951 _debug "$_d_i"
14d7bfda 5952 if [ "$_d_i" -eq "$_d_max_retry" ]; then
0c00e870 5953 _info "Deactivated success!"
5954 else
5955 _err "Deactivate failed."
5956 fi
5957
5958}
5959
5960deactivate() {
3f4513b3 5961 _d_domain_list="$1"
0c00e870 5962 _d_type="$2"
5963 _initpath
a3bdaa85 5964 _initAPI
3f4513b3 5965 _debug _d_domain_list "$_d_domain_list"
4c2a3841 5966 if [ -z "$(echo $_d_domain_list | cut -d , -f 1)" ]; then
2e87e64b 5967 _usage "Usage: $PROJECT_ENTRY --deactivate --domain <domain.tld> [--domain <domain2.tld> ...]"
0c00e870 5968 return 1
5969 fi
4c2a3841 5970 for _d_dm in $(echo "$_d_domain_list" | tr ',' ' '); do
5971 if [ -z "$_d_dm" ] || [ "$_d_dm" = "$NO_VALUE" ]; then
3f4513b3 5972 continue
5973 fi
c4a375b3 5974 if ! _deactivate "$_d_dm" "$_d_type"; then
86c017ec 5975 return 1
5976 fi
3f4513b3 5977 done
0c00e870 5978}
5979
4c3b3608 5980# Detect profile file if not specified as environment variable
5981_detect_profile() {
4c2a3841 5982 if [ -n "$PROFILE" -a -f "$PROFILE" ]; then
4c3b3608 5983 echo "$PROFILE"
5984 return
5985 fi
5986
4c3b3608 5987 DETECTED_PROFILE=''
4c3b3608 5988 SHELLTYPE="$(basename "/$SHELL")"
5989
4c2a3841 5990 if [ "$SHELLTYPE" = "bash" ]; then
5991 if [ -f "$HOME/.bashrc" ]; then
4c3b3608 5992 DETECTED_PROFILE="$HOME/.bashrc"
4c2a3841 5993 elif [ -f "$HOME/.bash_profile" ]; then
4c3b3608 5994 DETECTED_PROFILE="$HOME/.bash_profile"
5995 fi
4c2a3841 5996 elif [ "$SHELLTYPE" = "zsh" ]; then
4c3b3608 5997 DETECTED_PROFILE="$HOME/.zshrc"
5998 fi
5999
4c2a3841 6000 if [ -z "$DETECTED_PROFILE" ]; then
6001 if [ -f "$HOME/.profile" ]; then
4c3b3608 6002 DETECTED_PROFILE="$HOME/.profile"
4c2a3841 6003 elif [ -f "$HOME/.bashrc" ]; then
4c3b3608 6004 DETECTED_PROFILE="$HOME/.bashrc"
4c2a3841 6005 elif [ -f "$HOME/.bash_profile" ]; then
4c3b3608 6006 DETECTED_PROFILE="$HOME/.bash_profile"
4c2a3841 6007 elif [ -f "$HOME/.zshrc" ]; then
4c3b3608 6008 DETECTED_PROFILE="$HOME/.zshrc"
6009 fi
6010 fi
6011
1be222f6 6012 echo "$DETECTED_PROFILE"
4c3b3608 6013}
6014
6015_initconf() {
6016 _initpath
4c2a3841 6017 if [ ! -f "$ACCOUNT_CONF_PATH" ]; then
0ca5b799 6018 echo "
d404e92d 6019
d0871bda 6020#LOG_FILE=\"$DEFAULT_LOG_FILE\"
6b500036 6021#LOG_LEVEL=1
5ea6e9c9 6022
251d1c5c 6023#AUTO_UPGRADE=\"1\"
89002ed2 6024
569d6c55 6025#NO_TIMESTAMP=1
5b771039 6026
d5ec5f80 6027 " >"$ACCOUNT_CONF_PATH"
4c3b3608 6028 fi
6029}
6030
c8e9a31e 6031# nocron
c60883ef 6032_precheck() {
c8e9a31e 6033 _nocron="$1"
4c2a3841 6034
6035 if ! _exists "curl" && ! _exists "wget"; then
c60883ef 6036 _err "Please install curl or wget first, we need to access http resources."
4c3b3608 6037 return 1
6038 fi
4c2a3841 6039
6040 if [ -z "$_nocron" ]; then
415f375c 6041 if ! _exists "crontab" && ! _exists "fcrontab"; then
77f96b38 6042 if _exists cygpath && _exists schtasks.exe; then
6043 _info "It seems you are on Windows, we will install Windows scheduler task."
6044 else
6045 _err "It is recommended to install crontab first. try to install 'cron, crontab, crontabs or vixie-cron'."
6046 _err "We need to set cron job to renew the certs automatically."
6047 _err "Otherwise, your certs will not be able to be renewed automatically."
6048 if [ -z "$FORCE" ]; then
6049 _err "Please add '--force' and try install again to go without crontab."
6050 _err "./$PROJECT_ENTRY --install --force"
6051 return 1
6052 fi
c8e9a31e 6053 fi
77546ea5 6054 fi
4c3b3608 6055 fi
4c2a3841 6056
d8ba26e6 6057 if ! _exists "${ACME_OPENSSL_BIN:-openssl}"; then
851fedf7 6058 _err "Please install openssl first. ACME_OPENSSL_BIN=$ACME_OPENSSL_BIN"
c60883ef 6059 _err "We need openssl to generate keys."
4c3b3608 6060 return 1
6061 fi
4c2a3841 6062
3794b5cb 6063 if ! _exists "socat"; then
6064 _err "It is recommended to install socat first."
6065 _err "We use socat for standalone server if you use standalone mode."
c60883ef 6066 _err "If you don't use standalone mode, just ignore this warning."
6067 fi
4c2a3841 6068
c60883ef 6069 return 0
6070}
6071
0a7c9364 6072_setShebang() {
6073 _file="$1"
6074 _shebang="$2"
4c2a3841 6075 if [ -z "$_shebang" ]; then
43822d37 6076 _usage "Usage: file shebang"
0a7c9364 6077 return 1
6078 fi
6079 cp "$_file" "$_file.tmp"
4c2a3841 6080 echo "$_shebang" >"$_file"
6081 sed -n 2,99999p "$_file.tmp" >>"$_file"
6082 rm -f "$_file.tmp"
0a7c9364 6083}
6084
27dbe77f 6085#confighome
94dc5f33 6086_installalias() {
27dbe77f 6087 _c_home="$1"
94dc5f33 6088 _initpath
6089
6090 _envfile="$LE_WORKING_DIR/$PROJECT_ENTRY.env"
4c2a3841 6091 if [ "$_upgrading" ] && [ "$_upgrading" = "1" ]; then
44edb2bd 6092 echo "$(cat "$_envfile")" | sed "s|^LE_WORKING_DIR.*$||" >"$_envfile"
6093 echo "$(cat "$_envfile")" | sed "s|^alias le.*$||" >"$_envfile"
6094 echo "$(cat "$_envfile")" | sed "s|^alias le.sh.*$||" >"$_envfile"
94dc5f33 6095 fi
6096
27dbe77f 6097 if [ "$_c_home" ]; then
be83a6a3 6098 _c_entry=" --config-home '$_c_home'"
27dbe77f 6099 fi
6100
1786a5e5 6101 _setopt "$_envfile" "export LE_WORKING_DIR" "=" "\"$LE_WORKING_DIR\""
f5b546b3 6102 if [ "$_c_home" ]; then
6103 _setopt "$_envfile" "export LE_CONFIG_HOME" "=" "\"$LE_CONFIG_HOME\""
d04434e3 6104 else
6105 _sed_i "/^export LE_CONFIG_HOME/d" "$_envfile"
f5b546b3 6106 fi
be83a6a3 6107 _setopt "$_envfile" "alias $PROJECT_ENTRY" "=" "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\""
94dc5f33 6108
6109 _profile="$(_detect_profile)"
4c2a3841 6110 if [ "$_profile" ]; then
94dc5f33 6111 _debug "Found profile: $_profile"
aba5c634 6112 _info "Installing alias to '$_profile'"
94dc5f33 6113 _setopt "$_profile" ". \"$_envfile\""
6114 _info "OK, Close and reopen your terminal to start using $PROJECT_NAME"
6115 else
6116 _info "No profile is found, you will need to go into $LE_WORKING_DIR to use $PROJECT_NAME"
6117 fi
94dc5f33 6118
6119 #for csh
6120 _cshfile="$LE_WORKING_DIR/$PROJECT_ENTRY.csh"
94dc5f33 6121 _csh_profile="$HOME/.cshrc"
4c2a3841 6122 if [ -f "$_csh_profile" ]; then
aba5c634 6123 _info "Installing alias to '$_csh_profile'"
6626371d 6124 _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\""
f5b546b3 6125 if [ "$_c_home" ]; then
6126 _setopt "$_cshfile" "setenv LE_CONFIG_HOME" " " "\"$LE_CONFIG_HOME\""
d04434e3 6127 else
6128 _sed_i "/^setenv LE_CONFIG_HOME/d" "$_cshfile"
f5b546b3 6129 fi
be83a6a3 6130 _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\""
4c2a3841 6131 _setopt "$_csh_profile" "source \"$_cshfile\""
94dc5f33 6132 fi
4c2a3841 6133
acafa585 6134 #for tcsh
6135 _tcsh_profile="$HOME/.tcshrc"
4c2a3841 6136 if [ -f "$_tcsh_profile" ]; then
aba5c634 6137 _info "Installing alias to '$_tcsh_profile'"
acafa585 6138 _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\""
f5b546b3 6139 if [ "$_c_home" ]; then
6140 _setopt "$_cshfile" "setenv LE_CONFIG_HOME" " " "\"$LE_CONFIG_HOME\""
6141 fi
be83a6a3 6142 _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\""
4c2a3841 6143 _setopt "$_tcsh_profile" "source \"$_cshfile\""
acafa585 6144 fi
94dc5f33 6145
6146}
6147
58c4eaaf 6148# nocron confighome noprofile accountemail
c60883ef 6149install() {
f3e4cea3 6150
4c2a3841 6151 if [ -z "$LE_WORKING_DIR" ]; then
f3e4cea3 6152 LE_WORKING_DIR="$DEFAULT_INSTALL_HOME"
6153 fi
4c2a3841 6154
c8e9a31e 6155 _nocron="$1"
27dbe77f 6156 _c_home="$2"
86ef0a26 6157 _noprofile="$3"
58c4eaaf 6158 _accountemail="$4"
6159
4c2a3841 6160 if ! _initpath; then
c60883ef 6161 _err "Install failed."
4c3b3608 6162 return 1
6163 fi
4c2a3841 6164 if [ "$_nocron" ]; then
52677b0a 6165 _debug "Skip install cron job"
6166 fi
4c2a3841 6167
bd04638d 6168 if [ "$_ACME_IN_CRON" != "1" ]; then
4356eefb 6169 if ! _precheck "$_nocron"; then
6170 _err "Pre-check failed, can not install."
6171 return 1
6172 fi
4c3b3608 6173 fi
4c2a3841 6174
8e845d9f 6175 if [ -z "$_c_home" ] && [ "$LE_CONFIG_HOME" != "$LE_WORKING_DIR" ]; then
6176 _info "Using config home: $LE_CONFIG_HOME"
6177 _c_home="$LE_CONFIG_HOME"
6178 fi
6179
6cc11ffb 6180 #convert from le
4c2a3841 6181 if [ -d "$HOME/.le" ]; then
6182 for envfile in "le.env" "le.sh.env"; do
6183 if [ -f "$HOME/.le/$envfile" ]; then
6184 if grep "le.sh" "$HOME/.le/$envfile" >/dev/null; then
6185 _upgrading="1"
6186 _info "You are upgrading from le.sh"
6187 _info "Renaming \"$HOME/.le\" to $LE_WORKING_DIR"
6188 mv "$HOME/.le" "$LE_WORKING_DIR"
6189 mv "$LE_WORKING_DIR/$envfile" "$LE_WORKING_DIR/$PROJECT_ENTRY.env"
6190 break
6cc11ffb 6191 fi
6192 fi
6193 done
6194 fi
6195
4c3b3608 6196 _info "Installing to $LE_WORKING_DIR"
635695ec 6197
d04434e3 6198 if [ ! -d "$LE_WORKING_DIR" ]; then
6199 if ! mkdir -p "$LE_WORKING_DIR"; then
6200 _err "Can not create working dir: $LE_WORKING_DIR"
6201 return 1
6202 fi
6203
6204 chmod 700 "$LE_WORKING_DIR"
4a0f23e2 6205 fi
4c2a3841 6206
d04434e3 6207 if [ ! -d "$LE_CONFIG_HOME" ]; then
6208 if ! mkdir -p "$LE_CONFIG_HOME"; then
6209 _err "Can not create config dir: $LE_CONFIG_HOME"
6210 return 1
6211 fi
762978f8 6212
d04434e3 6213 chmod 700 "$LE_CONFIG_HOME"
27dbe77f 6214 fi
6215
d5ec5f80 6216 cp "$PROJECT_ENTRY" "$LE_WORKING_DIR/" && chmod +x "$LE_WORKING_DIR/$PROJECT_ENTRY"
4c3b3608 6217
4c2a3841 6218 if [ "$?" != "0" ]; then
a7b7355d 6219 _err "Install failed, can not copy $PROJECT_ENTRY"
4c3b3608 6220 return 1
6221 fi
6222
a7b7355d 6223 _info "Installed to $LE_WORKING_DIR/$PROJECT_ENTRY"
4c3b3608 6224
bd04638d 6225 if [ "$_ACME_IN_CRON" != "1" ] && [ -z "$_noprofile" ]; then
4356eefb 6226 _installalias "$_c_home"
6227 fi
4c3b3608 6228
4c2a3841 6229 for subf in $_SUB_FOLDERS; do
6230 if [ -d "$subf" ]; then
d5ec5f80 6231 mkdir -p "$LE_WORKING_DIR/$subf"
6232 cp "$subf"/* "$LE_WORKING_DIR"/"$subf"/
a61fe418 6233 fi
6234 done
6235
4c2a3841 6236 if [ ! -f "$ACCOUNT_CONF_PATH" ]; then
4c3b3608 6237 _initconf
6238 fi
6cc11ffb 6239
4c2a3841 6240 if [ "$_DEFAULT_ACCOUNT_CONF_PATH" != "$ACCOUNT_CONF_PATH" ]; then
635695ec 6241 _setopt "$_DEFAULT_ACCOUNT_CONF_PATH" "ACCOUNT_CONF_PATH" "=" "\"$ACCOUNT_CONF_PATH\""
6cc11ffb 6242 fi
6243
4c2a3841 6244 if [ "$_DEFAULT_CERT_HOME" != "$CERT_HOME" ]; then
b2817897 6245 _saveaccountconf "CERT_HOME" "$CERT_HOME"
6246 fi
6247
4c2a3841 6248 if [ "$_DEFAULT_ACCOUNT_KEY_PATH" != "$ACCOUNT_KEY_PATH" ]; then
b2817897 6249 _saveaccountconf "ACCOUNT_KEY_PATH" "$ACCOUNT_KEY_PATH"
6250 fi
4c2a3841 6251
6252 if [ -z "$_nocron" ]; then
27dbe77f 6253 installcronjob "$_c_home"
c8e9a31e 6254 fi
0a7c9364 6255
4c2a3841 6256 if [ -z "$NO_DETECT_SH" ]; then
641989fd 6257 #Modify shebang
4c2a3841 6258 if _exists bash; then
694af4ae 6259 _bash_path="$(bash -c "command -v bash 2>/dev/null")"
6260 if [ -z "$_bash_path" ]; then
6261 _bash_path="$(bash -c 'echo $SHELL')"
6262 fi
6263 fi
6264 if [ "$_bash_path" ]; then
329174b6 6265 _info "Good, bash is found, so change the shebang to use bash as preferred."
694af4ae 6266 _shebang='#!'"$_bash_path"
641989fd 6267 _setShebang "$LE_WORKING_DIR/$PROJECT_ENTRY" "$_shebang"
4c2a3841 6268 for subf in $_SUB_FOLDERS; do
6269 if [ -d "$LE_WORKING_DIR/$subf" ]; then
6270 for _apifile in "$LE_WORKING_DIR/$subf/"*.sh; do
a61fe418 6271 _setShebang "$_apifile" "$_shebang"
6272 done
6273 fi
6274 done
0a7c9364 6275 fi
6276 fi
6277
58c4eaaf 6278 if [ "$_accountemail" ]; then
6279 _saveaccountconf "ACCOUNT_EMAIL" "$_accountemail"
6280 fi
6281
4c3b3608 6282 _info OK
6283}
6284
52677b0a 6285# nocron
4c3b3608 6286uninstall() {
52677b0a 6287 _nocron="$1"
4c2a3841 6288 if [ -z "$_nocron" ]; then
52677b0a 6289 uninstallcronjob
6290 fi
4c3b3608 6291 _initpath
6292
9aa3be7f 6293 _uninstallalias
4c2a3841 6294
d5ec5f80 6295 rm -f "$LE_WORKING_DIR/$PROJECT_ENTRY"
f5b546b3 6296 _info "The keys and certs are in \"$(__green "$LE_CONFIG_HOME")\", you can remove them by yourself."
9aa3be7f 6297
6298}
6299
6300_uninstallalias() {
6301 _initpath
6302
4c3b3608 6303 _profile="$(_detect_profile)"
4c2a3841 6304 if [ "$_profile" ]; then
9aa3be7f 6305 _info "Uninstalling alias from: '$_profile'"
d5ec5f80 6306 text="$(cat "$_profile")"
4c2a3841 6307 echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.env\"$||" >"$_profile"
4c3b3608 6308 fi
6309
94dc5f33 6310 _csh_profile="$HOME/.cshrc"
4c2a3841 6311 if [ -f "$_csh_profile" ]; then
9aa3be7f 6312 _info "Uninstalling alias from: '$_csh_profile'"
d5ec5f80 6313 text="$(cat "$_csh_profile")"
4c2a3841 6314 echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" >"$_csh_profile"
94dc5f33 6315 fi
4c2a3841 6316
acafa585 6317 _tcsh_profile="$HOME/.tcshrc"
4c2a3841 6318 if [ -f "$_tcsh_profile" ]; then
9aa3be7f 6319 _info "Uninstalling alias from: '$_csh_profile'"
d5ec5f80 6320 text="$(cat "$_tcsh_profile")"
4c2a3841 6321 echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" >"$_tcsh_profile"
acafa585 6322 fi
4c3b3608 6323
6324}
6325
6326cron() {
bd04638d 6327 export _ACME_IN_CRON=1
89002ed2 6328 _initpath
d8ba26e6 6329 _info "$(__green "===Starting cron===")"
4c2a3841 6330 if [ "$AUTO_UPGRADE" = "1" ]; then
89002ed2 6331 export LE_WORKING_DIR
6332 (
4c2a3841 6333 if ! upgrade; then
6334 _err "Cron:Upgrade failed!"
6335 return 1
6336 fi
89002ed2 6337 )
d5ec5f80 6338 . "$LE_WORKING_DIR/$PROJECT_ENTRY" >/dev/null
1ab63043 6339
4c2a3841 6340 if [ -t 1 ]; then
1ab63043 6341 __INTERACTIVE="1"
6342 fi
4c2a3841 6343
89002ed2 6344 _info "Auto upgraded to: $VER"
6345 fi
4c3b3608 6346 renewAll
cc179731 6347 _ret="$?"
bd04638d 6348 _ACME_IN_CRON=""
d8ba26e6 6349 _info "$(__green "===End cron===")"
0ba95a3d 6350 exit $_ret
4c3b3608 6351}
6352
6353version() {
a63b05a9 6354 echo "$PROJECT"
6355 echo "v$VER"
4c3b3608 6356}
6357
b50e701c 6358# subject content hooks code
6359_send_notify() {
6360 _nsubject="$1"
6361 _ncontent="$2"
6362 _nhooks="$3"
6363 _nerror="$4"
6364
6365 if [ "$NOTIFY_LEVEL" = "$NOTIFY_LEVEL_DISABLE" ]; then
6366 _debug "The NOTIFY_LEVEL is $NOTIFY_LEVEL, disabled, just return."
6367 return 0
6368 fi
6369
6370 if [ -z "$_nhooks" ]; then
6371 _debug "The NOTIFY_HOOK is empty, just return."
6372 return 0
6373 fi
6374
6375 _send_err=0
6376 for _n_hook in $(echo "$_nhooks" | tr ',' " "); do
6377 _n_hook_file="$(_findHook "" $_SUB_FOLDER_NOTIFY "$_n_hook")"
143eac09 6378 _info "Sending via: $_n_hook"
6379 _debug "Found $_n_hook_file for $_n_hook"
75191e71 6380 if [ -z "$_n_hook_file" ]; then
6381 _err "Can not find the hook file for $_n_hook"
6382 continue
6383 fi
b50e701c 6384 if ! (
6385 if ! . "$_n_hook_file"; then
6386 _err "Load file $_n_hook_file error. Please check your api file and try again."
6387 return 1
6388 fi
6389
6390 d_command="${_n_hook}_send"
6391 if ! _exists "$d_command"; then
6392 _err "It seems that your api file is not correct, it must have a function named: $d_command"
6393 return 1
6394 fi
6395
6396 if ! $d_command "$_nsubject" "$_ncontent" "$_nerror"; then
6397 _err "Error send message by $d_command"
6398 return 1
6399 fi
6400
6401 return 0
6402 ); then
6403 _err "Set $_n_hook_file error."
6404 _send_err=1
6405 else
6406 _info "$_n_hook $(__green Success)"
6407 fi
6408 done
6409 return $_send_err
6410
6411}
6412
6413# hook
6414_set_notify_hook() {
6415 _nhooks="$1"
6416
5698bec6 6417 _test_subject="Hello, this is a notification from $PROJECT_NAME"
143eac09 6418 _test_content="If you receive this message, your notification works."
b50e701c 6419
6420 _send_notify "$_test_subject" "$_test_content" "$_nhooks" 0
6421
6422}
6423
6424#[hook] [level] [mode]
6425setnotify() {
6426 _nhook="$1"
6427 _nlevel="$2"
6428 _nmode="$3"
6429
6430 _initpath
6431
6432 if [ -z "$_nhook$_nlevel$_nmode" ]; then
2e87e64b 6433 _usage "Usage: $PROJECT_ENTRY --set-notify [--notify-hook <hookname>] [--notify-level <0|1|2|3>] [--notify-mode <0|1>]"
b50e701c 6434 _usage "$_NOTIFY_WIKI"
6435 return 1
6436 fi
6437
6438 if [ "$_nlevel" ]; then
6439 _info "Set notify level to: $_nlevel"
6440 export "NOTIFY_LEVEL=$_nlevel"
6441 _saveaccountconf "NOTIFY_LEVEL" "$NOTIFY_LEVEL"
6442 fi
6443
6444 if [ "$_nmode" ]; then
6445 _info "Set notify mode to: $_nmode"
6446 export "NOTIFY_MODE=$_nmode"
6447 _saveaccountconf "NOTIFY_MODE" "$NOTIFY_MODE"
6448 fi
6449
6450 if [ "$_nhook" ]; then
6451 _info "Set notify hook to: $_nhook"
6452 if [ "$_nhook" = "$NO_VALUE" ]; then
6453 _info "Clear notify hook"
6454 _clearaccountconf "NOTIFY_HOOK"
6455 else
6456 if _set_notify_hook "$_nhook"; then
6457 export NOTIFY_HOOK="$_nhook"
6458 _saveaccountconf "NOTIFY_HOOK" "$NOTIFY_HOOK"
6459 return 0
6460 else
6461 _err "Can not set notify hook to: $_nhook"
6462 return 1
6463 fi
6464 fi
6465 fi
6466
6467}
6468
4c3b3608 6469showhelp() {
d0871bda 6470 _initpath
4c3b3608 6471 version
2e87e64b 6472 echo "Usage: $PROJECT_ENTRY <command> ... [parameters ...]
a63b05a9 6473Commands:
c0fbe823
CE
6474 -h, --help Show this help message.
6475 -v, --version Show version info.
a7b7355d 6476 --install Install $PROJECT_NAME to your system.
6477 --uninstall Uninstall $PROJECT_NAME, and uninstall the cron job.
d8beaf72 6478 --upgrade Upgrade $PROJECT_NAME to the latest code from $PROJECT.
a63b05a9 6479 --issue Issue a cert.
a61fe418 6480 --deploy Deploy the cert to your server.
d81369d6 6481 -i, --install-cert Install the issued cert to apache/nginx or any other server.
c0fbe823 6482 -r, --renew Renew a cert.
27dbe77f 6483 --renew-all Renew all the certs.
a63b05a9 6484 --revoke Revoke a cert.
47b49f1b 6485 --remove Remove the cert from list of certs known to $PROJECT_NAME.
10afcaca 6486 --list List all the certs.
58150f5d 6487 --to-pkcs12 Export the certificate and key to a pfx file.
d81369d6 6488 --to-pkcs8 Convert to pkcs8 format.
7decf768
CE
6489 --sign-csr Issue a cert from an existing csr.
6490 --show-csr Show the content of a csr.
6491 -ccr, --create-csr Create CSR, professional use.
6492 --create-domain-key Create an domain private key, professional use.
27dbe77f 6493 --update-account Update account info.
6494 --register-account Register account key.
422dd1fa 6495 --deactivate-account Deactivate the account.
0984585d 6496 --create-account-key Create an account private key, professional use.
7decf768
CE
6497 --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.
6498 --uninstall-cronjob Uninstall the cron job. The 'uninstall' command can do this automatically.
6499 --cron Run cron job to renew all the certs.
b50e701c 6500 --set-notify Set the cron notification hook, level or mode.
7decf768 6501 --deactivate Deactivate the domain authz, professional use.
8d0e4851 6502 --set-default-ca Used with '--server', Set the default CA to use.
6503 See: $_SERVER_WIKI
b50e701c 6504
3c07f57a 6505
a63b05a9 6506Parameters:
c0fbe823
CE
6507 -d, --domain <domain.tld> Specifies a domain, used to issue, renew or revoke etc.
6508 --challenge-alias <domain.tld> The challenge domain alias for DNS alias mode.
e3ebd582 6509 See: $_DNS_ALIAS_WIKI
a48c22d1 6510
c0fbe823 6511 --domain-alias <domain.tld> The domain alias for DNS alias mode.
e3ebd582 6512 See: $_DNS_ALIAS_WIKI
a48c22d1 6513
c0fbe823 6514 --preferred-chain <chain> If the CA offers multiple certificate chains, prefer the chain with an issuer matching this Subject Common Name.
e3ebd582 6515 If no match, the default offered chain will be used. (default: empty)
6516 See: $_PREFERRED_CHAIN_WIKI
a48c22d1 6517
c0fbe823
CE
6518 -f, --force Force install, force cert renewal or override sudo restrictions.
6519 --staging, --test Use staging server, for testing.
6520 --debug [0|1|2|3] Output debug info. Defaults to 1 if argument is omitted.
e3ebd582 6521 --output-insecure Output all the sensitive messages.
6522 By default all the credentials/sensitive messages are hidden from the output/debug/log for security.
b086afb2 6523 -w, --webroot <directory> Specifies the web root folder for web root mode.
a63b05a9 6524 --standalone Use standalone mode.
08681f4a 6525 --alpn Use standalone alpn mode.
e3ebd582 6526 --stateless Use stateless mode.
6527 See: $_STATELESS_WIKI
a48c22d1 6528
a63b05a9 6529 --apache Use apache mode.
c0fbe823 6530 --dns [dns_hook] Use dns manual mode or dns api. Defaults to manual mode when argument is omitted.
e3ebd582 6531 See: $_DNS_API_WIKI
a48c22d1 6532
c0fbe823 6533 --dnssleep <seconds> The time in seconds to wait for all the txt records to propagate in dns api mode.
e3ebd582 6534 It's not necessary to use this by default, $PROJECT_NAME polls dns status by DOH automatically.
c0fbe823
CE
6535 -k, --keylength <bits> Specifies the domain key length: 2048, 3072, 4096, 8192 or ec-256, ec-384, ec-521.
6536 -ak, --accountkeylength <bits> Specifies the account key length: 2048, 3072, 4096
b086afb2 6537 --log [file] Specifies the log file. Defaults to \"$DEFAULT_LOG_FILE\" if argument is omitted.
c0fbe823
CE
6538 --log-level <1|2> Specifies the log level, default is 1.
6539 --syslog <0|3|6|7> Syslog level, 0: disable syslog, 3: error, 6: info, 7: debug.
6540 --eab-kid <eab_key_id> Key Identifier for External Account Binding.
6541 --eab-hmac-key <eab_hmac_key> HMAC key for External Account Binding.
2d5f1438
OB
6542
6543
7903fcb4 6544 These parameters are to install the cert to nginx/apache or any other server after issue/renew a cert:
3c07f57a 6545
b086afb2
CE
6546 --cert-file <file> Path to copy the cert file to after issue/renew..
6547 --key-file <file> Path to copy the key file to after issue/renew.
6548 --ca-file <file> Path to copy the intermediate cert file to after issue/renew.
6549 --fullchain-file <file> Path to copy the fullchain cert file to after issue/renew.
c0fbe823 6550 --reloadcmd <command> Command to execute after issue/renew to reload the server.
a63b05a9 6551
c0fbe823 6552 --server <server_uri> ACME Directory Resource URI. (default: $DEFAULT_CA)
e3ebd582 6553 See: $_SERVER_WIKI
6554
c0fbe823
CE
6555 --accountconf <file> Specifies a customized account config file.
6556 --home <directory> Specifies the home dir for $PROJECT_NAME.
6557 --cert-home <directory> Specifies the home dir to save all the certs, only valid for '--install' command.
6558 --config-home <directory> Specifies the home dir to save all the configurations.
6559 --useragent <string> Specifies the user agent string. it will be saved for future use too.
58c4eaaf 6560 -m, --email <email> Specifies the account email, only valid for the '--install' and '--update-account' command.
c0fbe823
CE
6561 --accountkey <file> Specifies the account key path, only valid for the '--install' command.
6562 --days <ndays> Specifies the days to renew the cert when using '--issue' command. The default value is $DEFAULT_RENEW days.
6563 --httpport <port> Specifies the standalone listening port. Only valid if the server is behind a reverse proxy or load balancer.
6564 --tlsport <port> Specifies the standalone tls listening port. Only valid if the server is behind a reverse proxy or load balancer.
6565 --local-address <ip> Specifies the standalone/tls server listening address, in case you have multiple ip addresses.
dcf4f8f6 6566 --listraw Only used for '--list' command, list the certs in raw format.
d81369d6 6567 -se, --stop-renew-on-error Only valid for '--renew-all' command. Stop if one cert has error in renewal.
13d7cae9 6568 --insecure Do not check the server certificate, in some devices, the api server's certificate may not be trusted.
c0fbe823
CE
6569 --ca-bundle <file> Specifies the path to the CA certificate bundle to verify api server's certificate.
6570 --ca-path <directory> Specifies directory containing CA certificates in PEM format, used by wget or curl.
58c4eaaf 6571 --no-cron Only valid for '--install' command, which means: do not install the default cron job.
e3ebd582 6572 In this case, the certs will not be renewed automatically.
58c4eaaf 6573 --no-profile Only valid for '--install' command, which means: do not install aliases to user profile.
2910be82 6574 --no-color Do not output color text.
e32b3aac 6575 --force-color Force output of color text. Useful for non-interactive use with the aha tool for HTML E-Mails.
b67d663a 6576 --ecc Specifies to use the ECC cert. Valid for '--install-cert', '--renew', '--revoke', '--to-pkcs12' and '--create-csr'
c0fbe823
CE
6577 --csr <file> Specifies the input csr.
6578 --pre-hook <command> Command to be run before obtaining any certificates.
6579 --post-hook <command> Command to be run after attempting to obtain/renew certificates. Runs regardless of whether obtain/renew succeeded or failed.
6580 --renew-hook <command> Command to be run after each successfully renewed certificate.
6581 --deploy-hook <hookname> The hook file to deploy cert
6582 --ocsp, --ocsp-must-staple Generate OCSP-Must-Staple extension.
6583 --always-force-new-domain-key Generate new domain key on renewal. Otherwise, the domain key is not changed by default.
6584 --auto-upgrade [0|1] Valid for '--upgrade' command, indicating whether to upgrade automatically in future. Defaults to 1 if argument is omitted.
6ae0f7f5 6585 --listen-v4 Force standalone/tls server to listen at ipv4.
6586 --listen-v6 Force standalone/tls server to listen at ipv6.
c0fbe823 6587 --openssl-bin <file> Specifies a custom openssl bin location.
9b124070 6588 --use-wget Force to use wget, if you have both curl and wget installed.
07fdb087 6589 --yes-I-know-dns-manual-mode-enough-go-ahead-please Force use of dns manual mode.
e3ebd582 6590 See: $_DNS_MANUAL_WIKI
a48c22d1 6591
c0fbe823
CE
6592 -b, --branch <branch> Only valid for '--upgrade' command, specifies the branch name to upgrade to.
6593 --notify-level <0|1|2|3> Set the notification level: Default value is $NOTIFY_LEVEL_DEFAULT.
6594 0: disabled, no notification will be sent.
6595 1: send notifications only when there is an error.
6596 2: send notifications when a cert is successfully renewed, or there is an error.
6597 3: send notifications when a cert is skipped, renewed, or error.
6598 --notify-mode <0|1> Set notification mode. Default value is $NOTIFY_MODE_DEFAULT.
6599 0: Bulk mode. Send all the domain's notifications in one message(mail).
6600 1: Cert mode. Send a message for every single cert.
6601 --notify-hook <hookname> Set the notify hook
6602 --revoke-reason <0-10> The reason for revocation, can be used in conjunction with the '--revoke' command.
e3ebd582 6603 See: $_REVOKE_WIKI
6604
dd6c5c9e
CE
6605 --password <password> Add a password to exported pfx file. Use with --to-pkcs12.
6606
b50e701c 6607
6608"
4c3b3608 6609}
6610
58c4eaaf 6611installOnline() {
4a0f23e2 6612 _info "Installing from online archive."
58c4eaaf 6613
6614 _branch="$BRANCH"
6615 if [ -z "$_branch" ]; then
6616 _branch="master"
4a0f23e2 6617 fi
a8df88ab 6618
58c4eaaf 6619 target="$PROJECT/archive/$_branch.tar.gz"
4a0f23e2 6620 _info "Downloading $target"
58c4eaaf 6621 localname="$_branch.tar.gz"
4c2a3841 6622 if ! _get "$target" >$localname; then
df9547ae 6623 _err "Download error."
4a0f23e2 6624 return 1
6625 fi
0bbe6eef 6626 (
4c2a3841 6627 _info "Extracting $localname"
3a3b0dd5 6628 if ! (tar xzf $localname || gtar xzf $localname); then
6629 _err "Extraction error."
6630 exit 1
6631 fi
4c2a3841 6632
58c4eaaf 6633 cd "$PROJECT_NAME-$_branch"
4c2a3841 6634 chmod +x $PROJECT_ENTRY
58c4eaaf 6635 if ./$PROJECT_ENTRY --install "$@"; then
4c2a3841 6636 _info "Install success!"
ac3667c7 6637 _initpath
cb7e3857 6638 _saveaccountconf "UPGRADE_HASH" "$(_getUpgradeHash)"
4c2a3841 6639 fi
6640
6641 cd ..
6642
58c4eaaf 6643 rm -rf "$PROJECT_NAME-$_branch"
4c2a3841 6644 rm -f "$localname"
0bbe6eef 6645 )
4a0f23e2 6646}
6647
cb7e3857
OB
6648_getRepoHash() {
6649 _hash_path=$1
6650 shift
6651 _hash_url="https://api.github.com/repos/acmesh-official/$PROJECT_NAME/git/refs/$_hash_path"
6652 _get $_hash_url | tr -d "\r\n" | tr '{},' '\n' | grep '"sha":' | cut -d '"' -f 4
6653}
6654
6655_getUpgradeHash() {
f716f606 6656 _b="$BRANCH"
6657 if [ -z "$_b" ]; then
6658 _b="master"
6659 fi
cb7e3857
OB
6660 _hash=$(_getRepoHash "heads/$_b")
6661 if [ -z "$_hash" ]; then _hash=$(_getRepoHash "tags/$_b"); fi
6662 echo $_hash
7a3c61b7 6663}
6664
52677b0a 6665upgrade() {
6666 if (
267f283a 6667 _initpath
cb7e3857 6668 [ -z "$FORCE" ] && [ "$(_getUpgradeHash)" = "$(_readaccountconf "UPGRADE_HASH")" ] && _info "Already uptodate!" && exit 0
267f283a 6669 export LE_WORKING_DIR
d0b748a4 6670 cd "$LE_WORKING_DIR"
58c4eaaf 6671 installOnline "--nocron" "--noprofile"
4c2a3841 6672 ); then
52677b0a 6673 _info "Upgrade success!"
096d8992 6674 exit 0
52677b0a 6675 else
6676 _err "Upgrade failed!"
096d8992 6677 exit 1
52677b0a 6678 fi
6679}
a63b05a9 6680
5ea6e9c9 6681_processAccountConf() {
4c2a3841 6682 if [ "$_useragent" ]; then
5ea6e9c9 6683 _saveaccountconf "USER_AGENT" "$_useragent"
4c2a3841 6684 elif [ "$USER_AGENT" ] && [ "$USER_AGENT" != "$DEFAULT_USER_AGENT" ]; then
d0871bda 6685 _saveaccountconf "USER_AGENT" "$USER_AGENT"
5ea6e9c9 6686 fi
4c2a3841 6687
a746139c 6688 if [ "$_openssl_bin" ]; then
851fedf7 6689 _saveaccountconf "ACME_OPENSSL_BIN" "$_openssl_bin"
6690 elif [ "$ACME_OPENSSL_BIN" ] && [ "$ACME_OPENSSL_BIN" != "$DEFAULT_OPENSSL_BIN" ]; then
6691 _saveaccountconf "ACME_OPENSSL_BIN" "$ACME_OPENSSL_BIN"
a746139c 6692 fi
6693
4c2a3841 6694 if [ "$_auto_upgrade" ]; then
6bf281f9 6695 _saveaccountconf "AUTO_UPGRADE" "$_auto_upgrade"
4c2a3841 6696 elif [ "$AUTO_UPGRADE" ]; then
6bf281f9 6697 _saveaccountconf "AUTO_UPGRADE" "$AUTO_UPGRADE"
6698 fi
4c2a3841 6699
9b124070 6700 if [ "$_use_wget" ]; then
6701 _saveaccountconf "ACME_USE_WGET" "$_use_wget"
6702 elif [ "$ACME_USE_WGET" ]; then
6703 _saveaccountconf "ACME_USE_WGET" "$ACME_USE_WGET"
6704 fi
6705
5ea6e9c9 6706}
6707
5bdfdfef 6708_checkSudo() {
6709 if [ "$SUDO_GID" ] && [ "$SUDO_COMMAND" ] && [ "$SUDO_USER" ] && [ "$SUDO_UID" ]; then
6710 if [ "$SUDO_USER" = "root" ] && [ "$SUDO_UID" = "0" ]; then
6711 #it's root using sudo, no matter it's using sudo or not, just fine
6712 return 0
6713 fi
79ad0ff5 6714 if [ -n "$SUDO_COMMAND" ]; then
996f5337 6715 #it's a normal user doing "sudo su", or `sudo -i` or `sudo -s`, or `sudo su acmeuser1`
6716 _endswith "$SUDO_COMMAND" /bin/su || _contains "$SUDO_COMMAND" "/bin/su " || grep "^$SUDO_COMMAND\$" /etc/shells >/dev/null 2>&1
79ad0ff5 6717 return $?
5bdfdfef 6718 fi
6719 #otherwise
6720 return 1
6721 fi
6722 return 0
6723}
6724
737e9e48 6725#server
6726_selectServer() {
6727 _server="$1"
6728 _server_lower="$(echo "$_server" | _lower_case)"
6729 _sindex=0
6730 for snames in $CA_NAMES; do
6731 snames="$(echo "$snames" | _lower_case)"
6732 _sindex="$(_math $_sindex + 1)"
6733 _debug2 "_selectServer try snames" "$snames"
6734 for sname in $(echo "$snames" | tr ',' ' '); do
6735 if [ "$_server_lower" = "$sname" ]; then
6736 _debug2 "_selectServer match $sname"
6737 _serverdir="$(_getfield "$CA_SERVERS" $_sindex)"
6738 _debug "Selected server: $_serverdir"
6739 ACME_DIRECTORY="$_serverdir"
6740 export ACME_DIRECTORY
6741 return
6742 fi
6743 done
6744 done
6745 ACME_DIRECTORY="$_server"
6746 export ACME_DIRECTORY
6747}
6748
269847d1 6749#url
6750_getCAShortName() {
6751 caurl="$1"
95ef046d 6752 if [ -z "$caurl" ]; then
6753 caurl="$DEFAULT_CA"
6754 fi
269847d1 6755 caurl_lower="$(echo $caurl | _lower_case)"
6756 _sindex=0
6757 for surl in $(echo "$CA_SERVERS" | _lower_case | tr , ' '); do
6758 _sindex="$(_math $_sindex + 1)"
6759 if [ "$caurl_lower" = "$surl" ]; then
6760 _nindex=0
6761 for snames in $CA_NAMES; do
6762 _nindex="$(_math $_nindex + 1)"
6763 if [ $_nindex -ge $_sindex ]; then
6764 _getfield "$snames" 1
6765 return
6766 fi
6767 done
6768 fi
6769 done
6770 echo "$caurl"
6771}
6772
737e9e48 6773#set default ca to $ACME_DIRECTORY
6774setdefaultca() {
6775 if [ -z "$ACME_DIRECTORY" ]; then
6776 _err "Please give a --server parameter."
6777 return 1
6778 fi
6779 _saveaccountconf "DEFAULT_ACME_SERVER" "$ACME_DIRECTORY"
df22f680 6780 _info "Changed default CA to: $(__green "$ACME_DIRECTORY")"
737e9e48 6781}
6782
a63b05a9 6783_process() {
6784 _CMD=""
6785 _domain=""
3f4513b3 6786 _altdomains="$NO_VALUE"
a63b05a9 6787 _webroot=""
875625b1 6788 _challenge_alias=""
bdbf323f 6789 _keylength=""
6790 _accountkeylength=""
5c539af7 6791 _cert_file=""
6792 _key_file=""
6793 _ca_file=""
6794 _fullchain_file=""
4d2f38b0 6795 _reloadcmd=""
a63b05a9 6796 _password=""
635695ec 6797 _accountconf=""
6798 _useragent=""
b5eb4b90 6799 _accountemail=""
6800 _accountkey=""
b2817897 6801 _certhome=""
27dbe77f 6802 _confighome=""
39c8f79f 6803 _httpport=""
e22bcf7c 6804 _tlsport=""
0e38c60d 6805 _dnssleep=""
dcf4f8f6 6806 _listraw=""
cc179731 6807 _stopRenewOnError=""
e3698edd 6808 #_insecure=""
78009539 6809 _ca_bundle=""
2aa75f03 6810 _ca_path=""
c8e9a31e 6811 _nocron=""
61556a54 6812 _noprofile=""
43822d37 6813 _ecc=""
10afcaca 6814 _csr=""
b0070f03 6815 _pre_hook=""
6816 _post_hook=""
6817 _renew_hook=""
a61fe418 6818 _deploy_hook=""
5ea6e9c9 6819 _logfile=""
d0871bda 6820 _log=""
0463b5d6 6821 _local_address=""
a73c5b33 6822 _log_level=""
6bf281f9 6823 _auto_upgrade=""
6ae0f7f5 6824 _listen_v4=""
6825 _listen_v6=""
a746139c 6826 _openssl_bin=""
e2edf208 6827 _syslog=""
9b124070 6828 _use_wget=""
98394f99 6829 _server=""
b50e701c 6830 _notify_hook=""
6831 _notify_level=""
6832 _notify_mode=""
1041c9f9 6833 _revoke_reason=""
f96d91cb 6834 _eab_kid=""
6835 _eab_hmac_key=""
e3ebd582 6836 _preferred_chain=""
4c2a3841 6837 while [ ${#} -gt 0 ]; do
a63b05a9 6838 case "${1}" in
4c2a3841 6839
19c43451 6840 --help | -h)
6841 showhelp
6842 return
6843 ;;
6844 --version | -v)
6845 version
6846 return
6847 ;;
6848 --install)
6849 _CMD="install"
6850 ;;
58c4eaaf 6851 --install-online)
6852 shift
6853 installOnline "$@"
6854 return
6855 ;;
19c43451 6856 --uninstall)
6857 _CMD="uninstall"
6858 ;;
6859 --upgrade)
6860 _CMD="upgrade"
6861 ;;
6862 --issue)
6863 _CMD="issue"
6864 ;;
6865 --deploy)
6866 _CMD="deploy"
6867 ;;
7decf768 6868 --sign-csr | --signcsr)
19c43451 6869 _CMD="signcsr"
6870 ;;
7decf768 6871 --show-csr | --showcsr)
19c43451 6872 _CMD="showcsr"
6873 ;;
7decf768 6874 -i | --install-cert | --installcert)
19c43451 6875 _CMD="installcert"
6876 ;;
6877 --renew | -r)
6878 _CMD="renew"
6879 ;;
7decf768 6880 --renew-all | --renewAll | --renewall)
19c43451 6881 _CMD="renewAll"
6882 ;;
6883 --revoke)
6884 _CMD="revoke"
6885 ;;
6886 --remove)
6887 _CMD="remove"
6888 ;;
6889 --list)
6890 _CMD="list"
6891 ;;
7decf768 6892 --install-cronjob | --installcronjob)
19c43451 6893 _CMD="installcronjob"
6894 ;;
7decf768 6895 --uninstall-cronjob | --uninstallcronjob)
19c43451 6896 _CMD="uninstallcronjob"
6897 ;;
6898 --cron)
6899 _CMD="cron"
6900 ;;
1521199e 6901 --to-pkcs12 | --to-pkcs | --toPkcs)
19c43451 6902 _CMD="toPkcs"
6903 ;;
7decf768 6904 --to-pkcs8 | --toPkcs8)
19c43451 6905 _CMD="toPkcs8"
6906 ;;
7decf768 6907 --create-account-key | --createAccountKey | --createaccountkey | -cak)
19c43451 6908 _CMD="createAccountKey"
6909 ;;
7decf768 6910 --create-domain-key | --createDomainKey | --createdomainkey | -cdk)
19c43451 6911 _CMD="createDomainKey"
6912 ;;
7decf768 6913 -ccr | --create-csr | --createCSR | --createcsr)
19c43451 6914 _CMD="createCSR"
6915 ;;
6916 --deactivate)
6917 _CMD="deactivate"
6918 ;;
7decf768 6919 --update-account | --updateaccount)
19c43451 6920 _CMD="updateaccount"
6921 ;;
7decf768 6922 --register-account | --registeraccount)
19c43451 6923 _CMD="registeraccount"
6924 ;;
6925 --deactivate-account)
6926 _CMD="deactivateaccount"
6927 ;;
6928 --set-notify)
6929 _CMD="setnotify"
6930 ;;
6931 --set-default-ca)
6932 _CMD="setdefaultca"
6933 ;;
7decf768 6934 -d | --domain)
19c43451 6935 _dvalue="$2"
4c2a3841 6936
19c43451 6937 if [ "$_dvalue" ]; then
6938 if _startswith "$_dvalue" "-"; then
6939 _err "'$_dvalue' is not a valid domain for parameter '$1'"
6940 return 1
6941 fi
6942 if _is_idn "$_dvalue" && ! _exists idn; then
6943 _err "It seems that $_dvalue is an IDN( Internationalized Domain Names), please install 'idn' command first."
6944 return 1
6945 fi
4c2a3841 6946
19c43451 6947 if _startswith "$_dvalue" "*."; then
6948 _debug "Wildcard domain"
6949 export ACME_VERSION=2
6950 fi
6951 if [ -z "$_domain" ]; then
6952 _domain="$_dvalue"
6953 else
6954 if [ "$_altdomains" = "$NO_VALUE" ]; then
6955 _altdomains="$_dvalue"
a63b05a9 6956 else
19c43451 6957 _altdomains="$_altdomains,$_dvalue"
a63b05a9 6958 fi
6959 fi
19c43451 6960 fi
4c2a3841 6961
19c43451 6962 shift
6963 ;;
a63b05a9 6964
7decf768 6965 -f | --force)
19c43451 6966 FORCE="1"
6967 ;;
6968 --staging | --test)
6969 STAGE="1"
6970 ;;
6971 --server)
6972 _server="$2"
6973 _selectServer "$_server"
6974 shift
6975 ;;
6976 --debug)
6977 if [ -z "$2" ] || _startswith "$2" "-"; then
6978 DEBUG="$DEBUG_LEVEL_DEFAULT"
6979 else
6980 DEBUG="$2"
64821ad4 6981 shift
19c43451 6982 fi
6983 ;;
6984 --output-insecure)
6985 export OUTPUT_INSECURE=1
6986 ;;
7decf768 6987 -w | --webroot)
19c43451 6988 wvalue="$2"
6989 if [ -z "$_webroot" ]; then
6990 _webroot="$wvalue"
6991 else
6992 _webroot="$_webroot,$wvalue"
6993 fi
6994 shift
6995 ;;
6996 --challenge-alias)
6997 cvalue="$2"
6998 _challenge_alias="$_challenge_alias$cvalue,"
6999 shift
7000 ;;
7001 --domain-alias)
7002 cvalue="$DNS_ALIAS_PREFIX$2"
7003 _challenge_alias="$_challenge_alias$cvalue,"
7004 shift
7005 ;;
7006 --standalone)
7007 wvalue="$NO_VALUE"
7008 if [ -z "$_webroot" ]; then
7009 _webroot="$wvalue"
7010 else
7011 _webroot="$_webroot,$wvalue"
7012 fi
7013 ;;
7014 --alpn)
7015 wvalue="$W_ALPN"
7016 if [ -z "$_webroot" ]; then
7017 _webroot="$wvalue"
7018 else
7019 _webroot="$_webroot,$wvalue"
7020 fi
7021 ;;
7022 --stateless)
7023 wvalue="$MODE_STATELESS"
7024 if [ -z "$_webroot" ]; then
7025 _webroot="$wvalue"
7026 else
7027 _webroot="$_webroot,$wvalue"
7028 fi
7029 ;;
7030 --local-address)
7031 lvalue="$2"
7032 _local_address="$_local_address$lvalue,"
7033 shift
7034 ;;
7035 --apache)
7036 wvalue="apache"
7037 if [ -z "$_webroot" ]; then
7038 _webroot="$wvalue"
7039 else
7040 _webroot="$_webroot,$wvalue"
7041 fi
7042 ;;
7043 --nginx)
7044 wvalue="$NGINX"
7045 if [ "$2" ] && ! _startswith "$2" "-"; then
7046 wvalue="$NGINX$2"
0463b5d6 7047 shift
19c43451 7048 fi
7049 if [ -z "$_webroot" ]; then
7050 _webroot="$wvalue"
7051 else
7052 _webroot="$_webroot,$wvalue"
7053 fi
7054 ;;
7055 --dns)
7056 wvalue="$W_DNS"
7057 if [ "$2" ] && ! _startswith "$2" "-"; then
7058 wvalue="$2"
0e38c60d 7059 shift
19c43451 7060 fi
7061 if [ -z "$_webroot" ]; then
7062 _webroot="$wvalue"
7063 else
7064 _webroot="$_webroot,$wvalue"
7065 fi
7066 ;;
7067 --dnssleep)
7068 _dnssleep="$2"
7069 Le_DNSSleep="$_dnssleep"
7070 shift
7071 ;;
4c2a3841 7072
19c43451 7073 --keylength | -k)
7074 _keylength="$2"
7075 shift
7076 ;;
7decf768 7077 -ak | --accountkeylength)
19c43451 7078 _accountkeylength="$2"
7079 shift
7080 ;;
a63b05a9 7081
19c43451 7082 --cert-file | --certpath)
7083 _cert_file="$2"
7084 shift
7085 ;;
7086 --key-file | --keypath)
7087 _key_file="$2"
7088 shift
7089 ;;
7090 --ca-file | --capath)
7091 _ca_file="$2"
7092 shift
7093 ;;
7094 --fullchain-file | --fullchainpath)
7095 _fullchain_file="$2"
7096 shift
7097 ;;
7098 --reloadcmd | --reloadCmd)
7099 _reloadcmd="$2"
7100 shift
7101 ;;
7102 --password)
7103 _password="$2"
7104 shift
7105 ;;
7106 --accountconf)
7107 _accountconf="$2"
7108 ACCOUNT_CONF_PATH="$_accountconf"
7109 shift
7110 ;;
7111 --home)
7112 LE_WORKING_DIR="$2"
7113 shift
7114 ;;
7decf768 7115 --cert-home | --certhome)
19c43451 7116 _certhome="$2"
7117 CERT_HOME="$_certhome"
7118 shift
7119 ;;
7120 --config-home)
7121 _confighome="$2"
7122 LE_CONFIG_HOME="$_confighome"
7123 shift
7124 ;;
7125 --useragent)
7126 _useragent="$2"
7127 USER_AGENT="$_useragent"
7128 shift
7129 ;;
58c4eaaf 7130 -m | --email | --accountemail)
19c43451 7131 _accountemail="$2"
58c4eaaf 7132 export ACCOUNT_EMAIL="$_accountemail"
19c43451 7133 shift
7134 ;;
7135 --accountkey)
7136 _accountkey="$2"
7137 ACCOUNT_KEY_PATH="$_accountkey"
7138 shift
7139 ;;
7140 --days)
7141 _days="$2"
7142 Le_RenewalDays="$_days"
7143 shift
7144 ;;
7145 --httpport)
7146 _httpport="$2"
7147 Le_HTTPPort="$_httpport"
7148 shift
7149 ;;
7150 --tlsport)
7151 _tlsport="$2"
7152 Le_TLSPort="$_tlsport"
7153 shift
7154 ;;
7155 --listraw)
7156 _listraw="raw"
7157 ;;
7decf768 7158 -se | --stop-renew-on-error | --stopRenewOnError | --stoprenewonerror)
19c43451 7159 _stopRenewOnError="1"
7160 ;;
7161 --insecure)
7162 #_insecure="1"
7163 HTTPS_INSECURE="1"
7164 ;;
7165 --ca-bundle)
7166 _ca_bundle="$(_readlink "$2")"
7167 CA_BUNDLE="$_ca_bundle"
7168 shift
7169 ;;
7170 --ca-path)
7171 _ca_path="$2"
7172 CA_PATH="$_ca_path"
7173 shift
7174 ;;
58c4eaaf 7175 --no-cron | --nocron)
19c43451 7176 _nocron="1"
7177 ;;
58c4eaaf 7178 --no-profile | --noprofile)
19c43451 7179 _noprofile="1"
7180 ;;
2910be82 7181 --no-color)
19c43451 7182 export ACME_NO_COLOR=1
7183 ;;
7184 --force-color)
7185 export ACME_FORCE_COLOR=1
7186 ;;
7187 --ecc)
7188 _ecc="isEcc"
7189 ;;
7190 --csr)
7191 _csr="$2"
7192 shift
7193 ;;
7194 --pre-hook)
7195 _pre_hook="$2"
7196 shift
7197 ;;
7198 --post-hook)
7199 _post_hook="$2"
7200 shift
7201 ;;
7202 --renew-hook)
7203 _renew_hook="$2"
7204 shift
7205 ;;
7206 --deploy-hook)
7207 if [ -z "$2" ] || _startswith "$2" "-"; then
7208 _usage "Please specify a value for '--deploy-hook'"
7209 return 1
7210 fi
7211 _deploy_hook="$_deploy_hook$2,"
7212 shift
7213 ;;
7214 --ocsp-must-staple | --ocsp)
7215 Le_OCSP_Staple="1"
7216 ;;
7217 --always-force-new-domain-key)
7218 if [ -z "$2" ] || _startswith "$2" "-"; then
7219 Le_ForceNewDomainKey=1
7220 else
7221 Le_ForceNewDomainKey="$2"
1041c9f9 7222 shift
19c43451 7223 fi
7224 ;;
7225 --yes-I-know-dns-manual-mode-enough-go-ahead-please)
7226 export FORCE_DNS_MANUAL=1
7227 ;;
7228 --log | --logfile)
7229 _log="1"
7230 _logfile="$2"
7231 if _startswith "$_logfile" '-'; then
7232 _logfile=""
7233 else
f96d91cb 7234 shift
19c43451 7235 fi
7236 LOG_FILE="$_logfile"
7237 if [ -z "$LOG_LEVEL" ]; then
7238 LOG_LEVEL="$DEFAULT_LOG_LEVEL"
7239 fi
7240 ;;
7241 --log-level)
7242 _log_level="$2"
7243 LOG_LEVEL="$_log_level"
7244 shift
7245 ;;
7246 --syslog)
7247 if ! _startswith "$2" '-'; then
7248 _syslog="$2"
f96d91cb 7249 shift
19c43451 7250 fi
7251 if [ -z "$_syslog" ]; then
7252 _syslog="$SYSLOG_LEVEL_DEFAULT"
7253 fi
7254 ;;
7255 --auto-upgrade)
7256 _auto_upgrade="$2"
7257 if [ -z "$_auto_upgrade" ] || _startswith "$_auto_upgrade" '-'; then
7258 _auto_upgrade="1"
7259 else
e3ebd582 7260 shift
19c43451 7261 fi
7262 AUTO_UPGRADE="$_auto_upgrade"
7263 ;;
7264 --listen-v4)
7265 _listen_v4="1"
7266 Le_Listen_V4="$_listen_v4"
7267 ;;
7268 --listen-v6)
7269 _listen_v6="1"
7270 Le_Listen_V6="$_listen_v6"
7271 ;;
7272 --openssl-bin)
7273 _openssl_bin="$2"
7274 ACME_OPENSSL_BIN="$_openssl_bin"
7275 shift
7276 ;;
7277 --use-wget)
7278 _use_wget="1"
7279 ACME_USE_WGET="1"
7280 ;;
7281 --branch | -b)
7282 export BRANCH="$2"
7283 shift
7284 ;;
7285 --notify-hook)
7286 _nhook="$2"
7287 if _startswith "$_nhook" "-"; then
7288 _err "'$_nhook' is not a hook name for '$1'"
a63b05a9 7289 return 1
19c43451 7290 fi
7291 if [ "$_notify_hook" ]; then
7292 _notify_hook="$_notify_hook,$_nhook"
7293 else
7294 _notify_hook="$_nhook"
7295 fi
7296 shift
7297 ;;
7298 --notify-level)
7299 _nlevel="$2"
7300 if _startswith "$_nlevel" "-"; then
7301 _err "'$_nlevel' is not a integer for '$1'"
7302 return 1
7303 fi
7304 _notify_level="$_nlevel"
7305 shift
7306 ;;
7307 --notify-mode)
7308 _nmode="$2"
7309 if _startswith "$_nmode" "-"; then
7310 _err "'$_nmode' is not a integer for '$1'"
7311 return 1
7312 fi
7313 _notify_mode="$_nmode"
7314 shift
7315 ;;
7316 --revoke-reason)
7317 _revoke_reason="$2"
7318 if _startswith "$_revoke_reason" "-"; then
7319 _err "'$_revoke_reason' is not a integer for '$1'"
7320 return 1
7321 fi
7322 shift
7323 ;;
7324 --eab-kid)
7325 _eab_kid="$2"
7326 shift
7327 ;;
7328 --eab-hmac-key)
7329 _eab_hmac_key="$2"
7330 shift
7331 ;;
7332 --preferred-chain)
7333 _preferred_chain="$2"
7334 shift
7335 ;;
7336 *)
7337 _err "Unknown parameter : $1"
7338 return 1
7339 ;;
a63b05a9 7340 esac
7341
7342 shift 1
7343 done
7344
4c2a3841 7345 if [ "${_CMD}" != "install" ]; then
5bdfdfef 7346 if [ "$__INTERACTIVE" ] && ! _checkSudo; then
7347 if [ -z "$FORCE" ]; then
7348 #Use "echo" here, instead of _info. it's too early
7349 echo "It seems that you are using sudo, please read this link first:"
7350 echo "$_SUDO_WIKI"
7351 return 1
7352 fi
7353 fi
5ea6e9c9 7354 __initHome
661f0583 7355 if [ "$_log" ]; then
4c2a3841 7356 if [ -z "$_logfile" ]; then
661f0583 7357 _logfile="$DEFAULT_LOG_FILE"
7358 fi
d0871bda 7359 fi
4c2a3841 7360 if [ "$_logfile" ]; then
5ea6e9c9 7361 _saveaccountconf "LOG_FILE" "$_logfile"
661f0583 7362 LOG_FILE="$_logfile"
5ea6e9c9 7363 fi
a73c5b33 7364
4c2a3841 7365 if [ "$_log_level" ]; then
a73c5b33 7366 _saveaccountconf "LOG_LEVEL" "$_log_level"
7367 LOG_LEVEL="$_log_level"
7368 fi
4c2a3841 7369
e2edf208 7370 if [ "$_syslog" ]; then
7371 if _exists logger; then
7372 if [ "$_syslog" = "0" ]; then
7373 _clearaccountconf "SYS_LOG"
7374 else
7375 _saveaccountconf "SYS_LOG" "$_syslog"
7376 fi
7377 SYS_LOG="$_syslog"
7378 else
7379 _err "The 'logger' command is not found, can not enable syslog."
7380 _clearaccountconf "SYS_LOG"
7381 SYS_LOG=""
7382 fi
7383 fi
7384
5ea6e9c9 7385 _processAccountConf
7386 fi
4c2a3841 7387
9d548d81 7388 _debug2 LE_WORKING_DIR "$LE_WORKING_DIR"
4c2a3841 7389
7390 if [ "$DEBUG" ]; then
dcf9cb58 7391 version
98394f99 7392 if [ "$_server" ]; then
7393 _debug "Using server: $_server"
7394 fi
dcf9cb58 7395 fi
9a733a57 7396 _debug "Running cmd: ${_CMD}"
a63b05a9 7397 case "${_CMD}" in
58c4eaaf 7398 install) install "$_nocron" "$_confighome" "$_noprofile" "$_accountemail" ;;
19c43451 7399 uninstall) uninstall "$_nocron" ;;
7400 upgrade) upgrade ;;
7401 issue)
7402 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"
7403 ;;
7404 deploy)
7405 deploy "$_domain" "$_deploy_hook" "$_ecc"
7406 ;;
7407 signcsr)
7408 signcsr "$_csr" "$_webroot" "$_cert_file" "$_key_file" "$_ca_file" "$_reloadcmd" "$_fullchain_file" "$_pre_hook" "$_post_hook" "$_renew_hook" "$_local_address" "$_challenge_alias"
7409 ;;
7410 showcsr)
7411 showcsr "$_csr" "$_domain"
7412 ;;
7413 installcert)
7414 installcert "$_domain" "$_cert_file" "$_key_file" "$_ca_file" "$_reloadcmd" "$_fullchain_file" "$_ecc"
7415 ;;
7416 renew)
7417 renew "$_domain" "$_ecc"
7418 ;;
7419 renewAll)
7420 renewAll "$_stopRenewOnError"
7421 ;;
7422 revoke)
7423 revoke "$_domain" "$_ecc" "$_revoke_reason"
7424 ;;
7425 remove)
7426 remove "$_domain" "$_ecc"
7427 ;;
7428 deactivate)
7429 deactivate "$_domain,$_altdomains"
7430 ;;
7431 registeraccount)
7432 registeraccount "$_accountkeylength" "$_eab_kid" "$_eab_hmac_key"
7433 ;;
7434 updateaccount)
7435 updateaccount
7436 ;;
7437 deactivateaccount)
7438 deactivateaccount
7439 ;;
7440 list)
7441 list "$_listraw" "$_domain"
7442 ;;
7443 installcronjob) installcronjob "$_confighome" ;;
7444 uninstallcronjob) uninstallcronjob ;;
7445 cron) cron ;;
7446 toPkcs)
7447 toPkcs "$_domain" "$_password" "$_ecc"
7448 ;;
7449 toPkcs8)
7450 toPkcs8 "$_domain" "$_ecc"
7451 ;;
7452 createAccountKey)
7453 createAccountKey "$_accountkeylength"
7454 ;;
7455 createDomainKey)
7456 createDomainKey "$_domain" "$_keylength"
7457 ;;
7458 createCSR)
7459 createCSR "$_domain" "$_altdomains" "$_ecc"
7460 ;;
7461 setnotify)
7462 setnotify "$_notify_hook" "$_notify_level" "$_notify_mode"
7463 ;;
7464 setdefaultca)
7465 setdefaultca
7466 ;;
7467 *)
7468 if [ "$_CMD" ]; then
7469 _err "Invalid command: $_CMD"
7470 fi
7471 showhelp
7472 return 1
7473 ;;
a63b05a9 7474 esac
d3595686 7475 _ret="$?"
4c2a3841 7476 if [ "$_ret" != "0" ]; then
d3595686 7477 return $_ret
7478 fi
4c2a3841 7479
7480 if [ "${_CMD}" = "install" ]; then
7481 if [ "$_log" ]; then
7482 if [ -z "$LOG_FILE" ]; then
d0871bda 7483 LOG_FILE="$DEFAULT_LOG_FILE"
7484 fi
7485 _saveaccountconf "LOG_FILE" "$LOG_FILE"
5ea6e9c9 7486 fi
4c2a3841 7487
7488 if [ "$_log_level" ]; then
a73c5b33 7489 _saveaccountconf "LOG_LEVEL" "$_log_level"
7490 fi
e2edf208 7491
7492 if [ "$_syslog" ]; then
7493 if _exists logger; then
7494 if [ "$_syslog" = "0" ]; then
7495 _clearaccountconf "SYS_LOG"
7496 else
7497 _saveaccountconf "SYS_LOG" "$_syslog"
7498 fi
7499 else
7500 _err "The 'logger' command is not found, can not enable syslog."
7501 _clearaccountconf "SYS_LOG"
7502 SYS_LOG=""
7503 fi
7504 fi
7505
5ea6e9c9 7506 _processAccountConf
b5eb4b90 7507 fi
635695ec 7508
a63b05a9 7509}
7510
319e0ae3 7511main() {
7512 [ -z "$1" ] && showhelp && return
4c2a3841 7513 if _startswith "$1" '-'; then _process "$@"; else "$@"; fi
319e0ae3 7514}
e69a7c38 7515
aa7b82de 7516main "$@"