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