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