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