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