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