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