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