]> git.proxmox.com Git - mirror_acme.sh.git/blame - acme.sh
fix https://github.com/Neilpang/acme.sh/issues/1411
[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"
6a66ba8a 1620 _debug2 "_postContentType" "$_postContentType"
4c2a3841 1621
fac1e367 1622 _inithttp
4c2a3841 1623
9b124070 1624 if [ "$_ACME_CURL" ] && [ "${ACME_USE_WGET:-0}" = "0" ]; then
1befee5a 1625 _CURL="$_ACME_CURL"
7834c252 1626 if [ "$HTTPS_INSECURE" ]; then
1627 _CURL="$_CURL --insecure "
1628 fi
45e386b2 1629 if [ "$_postContentType" ]; then
1630 _CURL="$_CURL -H \"Content-Type: $_postContentType\" "
1631 fi
ec9fc8cb 1632 _debug "_CURL" "$_CURL"
4c2a3841 1633 if [ "$needbase64" ]; then
c1151b0d 1634 response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$_post_url" | _base64)"
c60883ef 1635 else
c1151b0d 1636 response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$_post_url")"
c60883ef 1637 fi
16679b57 1638 _ret="$?"
4c2a3841 1639 if [ "$_ret" != "0" ]; then
87ab2d90 1640 _err "Please refer to https://curl.haxx.se/libcurl/c/libcurl-errors.html for error code: $_ret"
4c2a3841 1641 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
87ab2d90 1642 _err "Here is the curl dump log:"
1643 _err "$(cat "$_CURL_DUMP")"
1644 fi
687cfcc2 1645 fi
4c2a3841 1646 elif [ "$_ACME_WGET" ]; then
7834c252 1647 _WGET="$_ACME_WGET"
1648 if [ "$HTTPS_INSECURE" ]; then
1649 _WGET="$_WGET --no-check-certificate "
1650 fi
1651 _debug "_WGET" "$_WGET"
4c2a3841 1652 if [ "$needbase64" ]; then
1653 if [ "$httpmethod" = "POST" ]; then
ef871775 1654 if [ "$_postContentType" ]; then
1655 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)"
1656 else
1657 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)"
1658 fi
8fb9a709 1659 else
ef871775 1660 if [ "$_postContentType" ]; then
1661 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)"
1662 else
1663 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)"
1664 fi
8fb9a709 1665 fi
c60883ef 1666 else
4c2a3841 1667 if [ "$httpmethod" = "POST" ]; then
ef871775 1668 if [ "$_postContentType" ]; then
1669 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")"
1670 else
1671 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")"
1672 fi
8fb9a709 1673 else
ef871775 1674 if [ "$_postContentType" ]; then
1675 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")"
1676 else
1677 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")"
1678 fi
8fb9a709 1679 fi
c60883ef 1680 fi
16679b57 1681 _ret="$?"
4c2a3841 1682 if [ "$_ret" = "8" ]; then
9f43c270 1683 _ret=0
810c129c 1684 _debug "wget returns 8, the server returns a 'Bad request' response, lets process the response later."
9f43c270 1685 fi
4c2a3841 1686 if [ "$_ret" != "0" ]; then
1687 _err "Please refer to https://www.gnu.org/software/wget/manual/html_node/Exit-Status.html for error code: $_ret"
687cfcc2 1688 fi
c60883ef 1689 _sed_i "s/^ *//g" "$HTTP_HEADER"
d0b748a4 1690 else
1691 _ret="$?"
1692 _err "Neither curl nor wget is found, can not do $httpmethod."
c60883ef 1693 fi
16679b57 1694 _debug "_ret" "$_ret"
19539575 1695 printf "%s" "$response"
16679b57 1696 return $_ret
c60883ef 1697}
1698
75da0713 1699# url getheader timeout
c60883ef 1700_get() {
a4270efa 1701 _debug GET
c60883ef 1702 url="$1"
1703 onlyheader="$2"
75da0713 1704 t="$3"
79a267ab 1705 _debug url "$url"
72f54ca6 1706 _debug "timeout=$t"
fac1e367 1707
1708 _inithttp
1709
9b124070 1710 if [ "$_ACME_CURL" ] && [ "${ACME_USE_WGET:-0}" = "0" ]; then
1befee5a 1711 _CURL="$_ACME_CURL"
7834c252 1712 if [ "$HTTPS_INSECURE" ]; then
1713 _CURL="$_CURL --insecure "
1714 fi
4c2a3841 1715 if [ "$t" ]; then
75da0713 1716 _CURL="$_CURL --connect-timeout $t"
1717 fi
1718 _debug "_CURL" "$_CURL"
4c2a3841 1719 if [ "$onlyheader" ]; then
f9a6988e 1720 $_CURL -I --user-agent "$USER_AGENT" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$url"
c60883ef 1721 else
f9a6988e 1722 $_CURL --user-agent "$USER_AGENT" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$url"
c60883ef 1723 fi
9aaf36cd 1724 ret=$?
4c2a3841 1725 if [ "$ret" != "0" ]; then
d529eb6d 1726 _err "Please refer to https://curl.haxx.se/libcurl/c/libcurl-errors.html for error code: $ret"
4c2a3841 1727 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
fac1e367 1728 _err "Here is the curl dump log:"
1729 _err "$(cat "$_CURL_DUMP")"
1730 fi
1731 fi
4c2a3841 1732 elif [ "$_ACME_WGET" ]; then
1befee5a 1733 _WGET="$_ACME_WGET"
7834c252 1734 if [ "$HTTPS_INSECURE" ]; then
1735 _WGET="$_WGET --no-check-certificate "
1736 fi
4c2a3841 1737 if [ "$t" ]; then
75da0713 1738 _WGET="$_WGET --timeout=$t"
1739 fi
1740 _debug "_WGET" "$_WGET"
4c2a3841 1741 if [ "$onlyheader" ]; then
f9a6988e 1742 $_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 1743 else
f9a6988e 1744 $_WGET --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" -O - "$url"
c60883ef 1745 fi
9aaf36cd 1746 ret=$?
f731a4c7 1747 if [ "$ret" = "8" ]; then
39a1f1ef 1748 ret=0
810c129c 1749 _debug "wget returns 8, the server returns a 'Bad request' response, lets process the response later."
9f43c270 1750 fi
4c2a3841 1751 if [ "$ret" != "0" ]; then
1752 _err "Please refer to https://www.gnu.org/software/wget/manual/html_node/Exit-Status.html for error code: $ret"
fac1e367 1753 fi
d0b748a4 1754 else
1755 ret=$?
1756 _err "Neither curl nor wget is found, can not do GET."
9aaf36cd 1757 fi
ec9fc8cb 1758 _debug "ret" "$ret"
c60883ef 1759 return $ret
1760}
166096dc 1761
c2c8f320 1762_head_n() {
79a267ab 1763 head -n "$1"
c2c8f320 1764}
1765
1766_tail_n() {
f9a6988e 1767 if ! tail -n "$1" 2>/dev/null; then
19ab2a29 1768 #fix for solaris
f9a6988e 1769 tail -"$1"
19ab2a29 1770 fi
c2c8f320 1771}
fac1e367 1772
166096dc 1773# url payload needbase64 keyfile
4c3b3608 1774_send_signed_request() {
1775 url=$1
1776 payload=$2
1777 needbase64=$3
166096dc 1778 keyfile=$4
4c2a3841 1779 if [ -z "$keyfile" ]; then
166096dc 1780 keyfile="$ACCOUNT_KEY_PATH"
1781 fi
f9a6988e 1782 _debug url "$url"
4c3b3608 1783 _debug payload "$payload"
4c2a3841 1784
1785 if ! _calcjwk "$keyfile"; then
166096dc 1786 return 1
1787 fi
c60883ef 1788
6a66ba8a 1789 if [ "$ACME_VERSION" = "2" ]; then
1790 __request_conent_type="$CONTENT_TYPE_JSON"
1791 else
1792 __request_conent_type=""
1793 fi
11927a76 1794 payload64=$(printf "%s" "$payload" | _base64 | _url_replace)
f9a6988e 1795 _debug3 payload64 "$payload64"
4c2a3841 1796
0bc745f6 1797 MAX_REQUEST_RETRY_TIMES=5
1798 _request_retry_times=0
1799 while [ "${_request_retry_times}" -lt "$MAX_REQUEST_RETRY_TIMES" ]; do
b7924ce5 1800 _debug3 _request_retry_times "$_request_retry_times"
0bc745f6 1801 if [ -z "$_CACHED_NONCE" ]; then
8f01919f 1802 _headers=""
cae50e16 1803 if [ "$ACME_NEW_NONCE" ]; then
1804 _debug2 "Get nonce. ACME_NEW_NONCE" "$ACME_NEW_NONCE"
1805 nonceurl="$ACME_NEW_NONCE"
6a66ba8a 1806 if _post "" "$nonceurl" "" "HEAD" "$__request_conent_type"; then
cae50e16 1807 _headers="$(cat "$HTTP_HEADER")"
1808 fi
1809 fi
1810 if [ -z "$_headers" ]; then
1811 _debug2 "Get nonce. ACME_DIRECTORY" "$ACME_DIRECTORY"
1812 nonceurl="$ACME_DIRECTORY"
1813 _headers="$(_get "$nonceurl" "onlyheader")"
1814 fi
0bc745f6 1815
1816 if [ "$?" != "0" ]; then
1817 _err "Can not connect to $nonceurl to get nonce."
1818 return 1
1819 fi
4c2a3841 1820
0bc745f6 1821 _debug2 _headers "$_headers"
4c2a3841 1822
0bc745f6 1823 _CACHED_NONCE="$(echo "$_headers" | grep "Replay-Nonce:" | _head_n 1 | tr -d "\r\n " | cut -d ':' -f 2)"
1824 _debug2 _CACHED_NONCE "$_CACHED_NONCE"
1825 else
1826 _debug2 "Use _CACHED_NONCE" "$_CACHED_NONCE"
1827 fi
1828 nonce="$_CACHED_NONCE"
1829 _debug2 nonce "$nonce"
4c2a3841 1830
c1151b0d 1831 if [ "$ACME_VERSION" = "2" ]; then
d2cde379 1832 if [ "$url" = "$ACME_NEW_ACCOUNT" ] || [ "$url" = "$ACME_REVOKE_CERT" ]; then
c1151b0d 1833 protected="$JWK_HEADERPLACE_PART1$nonce\", \"url\": \"${url}$JWK_HEADERPLACE_PART2, \"jwk\": $jwk"'}'
1834 else
6b798b01 1835 protected="$JWK_HEADERPLACE_PART1$nonce\", \"url\": \"${url}$JWK_HEADERPLACE_PART2, \"kid\": \"${ACCOUNT_URL}\""'}'
c1151b0d 1836 fi
1837 else
1838 protected="$JWK_HEADERPLACE_PART1$nonce\", \"url\": \"${url}$JWK_HEADERPLACE_PART2, \"jwk\": $jwk"'}'
1839 fi
0bc745f6 1840 _debug3 protected "$protected"
a272ee4f 1841
0bc745f6 1842 protected64="$(printf "%s" "$protected" | _base64 | _url_replace)"
1843 _debug3 protected64 "$protected64"
4c2a3841 1844
0bc745f6 1845 if ! _sig_t="$(printf "%s" "$protected64.$payload64" | _sign "$keyfile" "sha256")"; then
1846 _err "Sign request failed."
1847 return 1
1848 fi
1849 _debug3 _sig_t "$_sig_t"
166096dc 1850
0bc745f6 1851 sig="$(printf "%s" "$_sig_t" | _url_replace)"
1852 _debug3 sig "$sig"
4c2a3841 1853
c1151b0d 1854 if [ "$ACME_VERSION" = "2" ]; then
1855 body="{\"protected\": \"$protected64\", \"payload\": \"$payload64\", \"signature\": \"$sig\"}"
1856 else
1857 body="{\"header\": $JWK_HEADER, \"protected\": \"$protected64\", \"payload\": \"$payload64\", \"signature\": \"$sig\"}"
1858 fi
0bc745f6 1859 _debug3 body "$body"
4c2a3841 1860
6a66ba8a 1861 response="$(_post "$body" "$url" "$needbase64" "POST" "$__request_conent_type")"
0bc745f6 1862 _CACHED_NONCE=""
bbbdcb09 1863
0bc745f6 1864 if [ "$?" != "0" ]; then
1865 _err "Can not post to $url"
1866 return 1
1867 fi
1868 _debug2 original "$response"
1869 response="$(echo "$response" | _normalizeJson)"
4c3b3608 1870
64802502 1871 responseHeaders="$(cat "$HTTP_HEADER")"
4c3b3608 1872
0bc745f6 1873 _debug2 responseHeaders "$responseHeaders"
1874 _debug2 response "$response"
1875 code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\r\n")"
1876 _debug code "$code"
4c2a3841 1877
0bc745f6 1878 _CACHED_NONCE="$(echo "$responseHeaders" | grep "Replay-Nonce:" | _head_n 1 | tr -d "\r\n " | cut -d ':' -f 2)"
4c3b3608 1879
8148bfea 1880 _body="$response"
1881 if [ "$needbase64" ]; then
e1db5db8 1882 _body="$(echo "$_body" | _dbase64 | tr -d '\0')"
6d6b2efd 1883 _debug3 _body "$_body"
8148bfea 1884 fi
36309e6d 1885
8148bfea 1886 if _contains "$_body" "JWS has invalid anti-replay nonce"; then
0bc745f6 1887 _info "It seems the CA server is busy now, let's wait and retry."
1888 _request_retry_times=$(_math "$_request_retry_times" + 1)
1889 _sleep 5
1890 continue
1891 fi
b7924ce5 1892 break
0bc745f6 1893 done
4c3b3608 1894
4c3b3608 1895}
4c3b3608 1896
1897#setopt "file" "opt" "=" "value" [";"]
1898_setopt() {
1899 __conf="$1"
1900 __opt="$2"
1901 __sep="$3"
1902 __val="$4"
1903 __end="$5"
4c2a3841 1904 if [ -z "$__opt" ]; then
1905 _usage usage: _setopt '"file" "opt" "=" "value" [";"]'
4c3b3608 1906 return
1907 fi
4c2a3841 1908 if [ ! -f "$__conf" ]; then
4c3b3608 1909 touch "$__conf"
1910 fi
1911
4c2a3841 1912 if grep -n "^$__opt$__sep" "$__conf" >/dev/null; then
22ea4004 1913 _debug3 OK
4c2a3841 1914 if _contains "$__val" "&"; then
79a267ab 1915 __val="$(echo "$__val" | sed 's/&/\\&/g')"
4c3b3608 1916 fi
79a267ab 1917 text="$(cat "$__conf")"
52f8b787 1918 printf -- "%s\n" "$text" | sed "s|^$__opt$__sep.*$|$__opt$__sep$__val$__end|" >"$__conf"
4c3b3608 1919
4c2a3841 1920 elif grep -n "^#$__opt$__sep" "$__conf" >/dev/null; then
1921 if _contains "$__val" "&"; then
79a267ab 1922 __val="$(echo "$__val" | sed 's/&/\\&/g')"
4c3b3608 1923 fi
79a267ab 1924 text="$(cat "$__conf")"
52f8b787 1925 printf -- "%s\n" "$text" | sed "s|^#$__opt$__sep.*$|$__opt$__sep$__val$__end|" >"$__conf"
4c3b3608 1926
1927 else
22ea4004 1928 _debug3 APP
4c2a3841 1929 echo "$__opt$__sep$__val$__end" >>"$__conf"
4c3b3608 1930 fi
1efb2085 1931 _debug3 "$(grep -n "^$__opt$__sep" "$__conf")"
4c3b3608 1932}
1933
8a29fbc8 1934#_save_conf file key value
1935#save to conf
1936_save_conf() {
1937 _s_c_f="$1"
1938 _sdkey="$2"
1939 _sdvalue="$3"
4c2a3841 1940 if [ "$_s_c_f" ]; then
8a29fbc8 1941 _setopt "$_s_c_f" "$_sdkey" "=" "'$_sdvalue'"
4d2f38b0 1942 else
8a29fbc8 1943 _err "config file is empty, can not save $_sdkey=$_sdvalue"
4d2f38b0 1944 fi
1945}
1946
8a29fbc8 1947#_clear_conf file key
1948_clear_conf() {
1949 _c_c_f="$1"
1950 _sdkey="$2"
4c2a3841 1951 if [ "$_c_c_f" ]; then
20ea8591 1952 _conf_data="$(cat "$_c_c_f")"
fa574fe8 1953 echo "$_conf_data" | sed "s/^$_sdkey *=.*$//" >"$_c_c_f"
4c3b3608 1954 else
8a29fbc8 1955 _err "config file is empty, can not clear"
4c3b3608 1956 fi
1957}
1958
8a29fbc8 1959#_read_conf file key
1960_read_conf() {
1961 _r_c_f="$1"
1962 _sdkey="$2"
4c2a3841 1963 if [ -f "$_r_c_f" ]; then
1964 (
79a267ab 1965 eval "$(grep "^$_sdkey *=" "$_r_c_f")"
4c2a3841 1966 eval "printf \"%s\" \"\$$_sdkey\""
1967 )
61623d22 1968 else
57e58ce7 1969 _debug "config file is empty, can not read $_sdkey"
4c3b3608 1970 fi
4c3b3608 1971}
1972
1973#_savedomainconf key value
1974#save to domain.conf
1975_savedomainconf() {
8a29fbc8 1976 _save_conf "$DOMAIN_CONF" "$1" "$2"
4d2f38b0 1977}
1978
1979#_cleardomainconf key
1980_cleardomainconf() {
8a29fbc8 1981 _clear_conf "$DOMAIN_CONF" "$1"
4c3b3608 1982}
1983
61623d22 1984#_readdomainconf key
1985_readdomainconf() {
8a29fbc8 1986 _read_conf "$DOMAIN_CONF" "$1"
61623d22 1987}
1988
4c3b3608 1989#_saveaccountconf key value
1990_saveaccountconf() {
8a29fbc8 1991 _save_conf "$ACCOUNT_CONF_PATH" "$1" "$2"
4c3b3608 1992}
1993
fcdf41ba 1994#key value
1995_saveaccountconf_mutable() {
1996 _save_conf "$ACCOUNT_CONF_PATH" "SAVED_$1" "$2"
1997 #remove later
1998 _clearaccountconf "$1"
1999}
2000
2001#key
2002_readaccountconf() {
2003 _read_conf "$ACCOUNT_CONF_PATH" "$1"
2004}
2005
2006#key
2007_readaccountconf_mutable() {
2008 _rac_key="$1"
2009 _readaccountconf "SAVED_$_rac_key"
2010}
2011
fac1e367 2012#_clearaccountconf key
2013_clearaccountconf() {
8a29fbc8 2014 _clear_conf "$ACCOUNT_CONF_PATH" "$1"
2015}
2016
2017#_savecaconf key value
2018_savecaconf() {
2019 _save_conf "$CA_CONF" "$1" "$2"
2020}
2021
2022#_readcaconf key
2023_readcaconf() {
2024 _read_conf "$CA_CONF" "$1"
2025}
2026
2027#_clearaccountconf key
2028_clearcaconf() {
2029 _clear_conf "$CA_CONF" "$1"
fac1e367 2030}
2031
0463b5d6 2032# content localaddress
4c3b3608 2033_startserver() {
2034 content="$1"
0463b5d6 2035 ncaddr="$2"
2036 _debug "ncaddr" "$ncaddr"
2037
6fc1447f 2038 _debug "startserver: $$"
4c2a3841 2039
39c8f79f 2040 _debug Le_HTTPPort "$Le_HTTPPort"
6ae0f7f5 2041 _debug Le_Listen_V4 "$Le_Listen_V4"
2042 _debug Le_Listen_V6 "$Le_Listen_V6"
4c2a3841 2043
3794b5cb 2044 _NC="socat"
4c2a3841 2045 if [ "$Le_Listen_V4" ]; then
6ae0f7f5 2046 _NC="$_NC -4"
4c2a3841 2047 elif [ "$Le_Listen_V6" ]; then
6ae0f7f5 2048 _NC="$_NC -6"
2049 fi
4c2a3841 2050
9ad7ac63 2051 if [ "$DEBUG" ] && [ "$DEBUG" -gt "1" ]; then
5c568d69 2052 _NC="$_NC -d -d -v"
2053 fi
2054
9134b6ea
HC
2055 SOCAT_OPTIONS=TCP-LISTEN:$Le_HTTPPort,crlf,reuseaddr,fork
2056
2057 #Adding bind to local-address
d84665cb 2058 if [ "$ncaddr" ]; then
9ad7ac63 2059 SOCAT_OPTIONS="$SOCAT_OPTIONS,bind=${ncaddr}"
9134b6ea
HC
2060 fi
2061
5c568d69 2062 _debug "_NC" "$_NC $SOCAT_OPTIONS"
bae50da7 2063 $_NC $SOCAT_OPTIONS SYSTEM:"sleep 1; echo HTTP/1.0 200 OK; echo ; echo $content; echo;" &
3794b5cb 2064 serverproc="$!"
4c3b3608 2065}
2066
4c2a3841 2067_stopserver() {
4c3b3608 2068 pid="$1"
6fc1447f 2069 _debug "pid" "$pid"
4c2a3841 2070 if [ -z "$pid" ]; then
6fc1447f 2071 return
2072 fi
e22bcf7c 2073
3794b5cb 2074 kill $pid
2075
4c3b3608 2076}
2077
fdcb6b72 2078# sleep sec
2079_sleep() {
2080 _sleep_sec="$1"
4c2a3841 2081 if [ "$__INTERACTIVE" ]; then
fdcb6b72 2082 _sleep_c="$_sleep_sec"
4c2a3841 2083 while [ "$_sleep_c" -ge "0" ]; do
c583d6bb 2084 printf "\r \r"
fdcb6b72 2085 __green "$_sleep_c"
79a267ab 2086 _sleep_c="$(_math "$_sleep_c" - 1)"
fdcb6b72 2087 sleep 1
2088 done
c583d6bb 2089 printf "\r"
fdcb6b72 2090 else
2091 sleep "$_sleep_sec"
2092 fi
2093}
e22bcf7c 2094
6ae0f7f5 2095# _starttlsserver san_a san_b port content _ncaddr
e22bcf7c 2096_starttlsserver() {
2097 _info "Starting tls server."
2098 san_a="$1"
2099 san_b="$2"
2100 port="$3"
2101 content="$4"
6ae0f7f5 2102 opaddr="$5"
4c2a3841 2103
e22bcf7c 2104 _debug san_a "$san_a"
2105 _debug san_b "$san_b"
2106 _debug port "$port"
4c2a3841 2107
e22bcf7c 2108 #create key TLS_KEY
4c2a3841 2109 if ! _createkey "2048" "$TLS_KEY"; then
e22bcf7c 2110 _err "Create tls validation key error."
2111 return 1
2112 fi
4c2a3841 2113
e22bcf7c 2114 #create csr
2115 alt="$san_a"
4c2a3841 2116 if [ "$san_b" ]; then
e22bcf7c 2117 alt="$alt,$san_b"
2118 fi
4c2a3841 2119 if ! _createcsr "tls.acme.sh" "$alt" "$TLS_KEY" "$TLS_CSR" "$TLS_CONF"; then
e22bcf7c 2120 _err "Create tls validation csr error."
2121 return 1
2122 fi
4c2a3841 2123
e22bcf7c 2124 #self signed
4c2a3841 2125 if ! _signcsr "$TLS_KEY" "$TLS_CSR" "$TLS_CONF" "$TLS_CERT"; then
e22bcf7c 2126 _err "Create tls validation cert error."
2127 return 1
2128 fi
4c2a3841 2129
5f6e3da7 2130 __S_OPENSSL="${ACME_OPENSSL_BIN:-openssl} s_server -www -cert $TLS_CERT -key $TLS_KEY "
2131 if [ "$opaddr" ]; then
2132 __S_OPENSSL="$__S_OPENSSL -accept $opaddr:$port"
2133 else
2134 __S_OPENSSL="$__S_OPENSSL -accept $port"
2135 fi
6ae0f7f5 2136
2137 _debug Le_Listen_V4 "$Le_Listen_V4"
2138 _debug Le_Listen_V6 "$Le_Listen_V6"
4c2a3841 2139 if [ "$Le_Listen_V4" ]; then
6ae0f7f5 2140 __S_OPENSSL="$__S_OPENSSL -4"
4c2a3841 2141 elif [ "$Le_Listen_V6" ]; then
6ae0f7f5 2142 __S_OPENSSL="$__S_OPENSSL -6"
2143 fi
4c2a3841 2144
6ae0f7f5 2145 _debug "$__S_OPENSSL"
5f6e3da7 2146 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
2147 $__S_OPENSSL -tlsextdebug &
2148 else
2149 $__S_OPENSSL >/dev/null 2>&1 &
2150 fi
331c4bb6 2151
e22bcf7c 2152 serverproc="$!"
5dbf664a 2153 sleep 1
d5ec5f80 2154 _debug serverproc "$serverproc"
e22bcf7c 2155}
2156
18e46962 2157#file
2158_readlink() {
2159 _rf="$1"
2160 if ! readlink -f "$_rf" 2>/dev/null; then
6c4cc357 2161 if _startswith "$_rf" "/"; then
2162 echo "$_rf"
7da50703 2163 return 0
2164 fi
6c4cc357 2165 echo "$(pwd)/$_rf" | _conapath
18e46962 2166 fi
2167}
2168
6c4cc357 2169_conapath() {
2170 sed "s#/\./#/#g"
2171}
2172
5ea6e9c9 2173__initHome() {
4c2a3841 2174 if [ -z "$_SCRIPT_HOME" ]; then
2175 if _exists readlink && _exists dirname; then
66990cf8 2176 _debug "Lets find script dir."
f3e4cea3 2177 _debug "_SCRIPT_" "$_SCRIPT_"
18e46962 2178 _script="$(_readlink "$_SCRIPT_")"
f3e4cea3 2179 _debug "_script" "$_script"
2180 _script_home="$(dirname "$_script")"
2181 _debug "_script_home" "$_script_home"
4c2a3841 2182 if [ -d "$_script_home" ]; then
f3e4cea3 2183 _SCRIPT_HOME="$_script_home"
2184 else
2185 _err "It seems the script home is not correct:$_script_home"
2186 fi
2187 fi
2188 fi
2189
219e9115 2190 # if [ -z "$LE_WORKING_DIR" ]; then
2191 # if [ -f "$DEFAULT_INSTALL_HOME/account.conf" ]; then
2192 # _debug "It seems that $PROJECT_NAME is already installed in $DEFAULT_INSTALL_HOME"
2193 # LE_WORKING_DIR="$DEFAULT_INSTALL_HOME"
2194 # else
2195 # LE_WORKING_DIR="$_SCRIPT_HOME"
2196 # fi
2197 # fi
4c2a3841 2198
2199 if [ -z "$LE_WORKING_DIR" ]; then
f3e4cea3 2200 _debug "Using default home:$DEFAULT_INSTALL_HOME"
2201 LE_WORKING_DIR="$DEFAULT_INSTALL_HOME"
2202 fi
7da50703 2203 export LE_WORKING_DIR
f3e4cea3 2204
f5b546b3 2205 if [ -z "$LE_CONFIG_HOME" ]; then
2206 LE_CONFIG_HOME="$LE_WORKING_DIR"
27dbe77f 2207 fi
f5b546b3 2208 _debug "Using config home:$LE_CONFIG_HOME"
2209 export LE_CONFIG_HOME
27dbe77f 2210
f5b546b3 2211 _DEFAULT_ACCOUNT_CONF_PATH="$LE_CONFIG_HOME/account.conf"
d53289d7 2212
4c2a3841 2213 if [ -z "$ACCOUNT_CONF_PATH" ]; then
2214 if [ -f "$_DEFAULT_ACCOUNT_CONF_PATH" ]; then
8663fb7e 2215 . "$_DEFAULT_ACCOUNT_CONF_PATH"
635695ec 2216 fi
d53289d7 2217 fi
4c2a3841 2218
2219 if [ -z "$ACCOUNT_CONF_PATH" ]; then
d53289d7 2220 ACCOUNT_CONF_PATH="$_DEFAULT_ACCOUNT_CONF_PATH"
4c3b3608 2221 fi
4c2a3841 2222
f5b546b3 2223 DEFAULT_LOG_FILE="$LE_CONFIG_HOME/$PROJECT_NAME.log"
4c2a3841 2224
f5b546b3 2225 DEFAULT_CA_HOME="$LE_CONFIG_HOME/ca"
4c2a3841 2226
2227 if [ -z "$LE_TEMP_DIR" ]; then
f5b546b3 2228 LE_TEMP_DIR="$LE_CONFIG_HOME/tmp"
610e0f21 2229 fi
5ea6e9c9 2230}
2231
48d9a8c1 2232#server
2233_initAPI() {
2234 _api_server="${1:-$ACME_DIRECTORY}"
2235 _debug "_init api for server: $_api_server"
4cee14f3 2236
cae50e16 2237 if [ -z "$ACME_NEW_ACCOUNT" ]; then
48d9a8c1 2238 response=$(_get "$_api_server")
2239 if [ "$?" != "0" ]; then
2240 _debug2 "response" "$response"
2241 _err "Can not init api."
2242 return 1
2243 fi
2244 _debug2 "response" "$response"
2245
2246 ACME_KEY_CHANGE=$(echo "$response" | _egrep_o 'key-change" *: *"[^"]*"' | cut -d '"' -f 3)
c1151b0d 2247 if [ -z "$ACME_KEY_CHANGE" ]; then
2248 ACME_KEY_CHANGE=$(echo "$response" | _egrep_o 'keyChange" *: *"[^"]*"' | cut -d '"' -f 3)
2249 fi
48d9a8c1 2250 export ACME_KEY_CHANGE
2251
2252 ACME_NEW_AUTHZ=$(echo "$response" | _egrep_o 'new-authz" *: *"[^"]*"' | cut -d '"' -f 3)
c1151b0d 2253 if [ -z "$ACME_NEW_AUTHZ" ]; then
2254 ACME_NEW_AUTHZ=$(echo "$response" | _egrep_o 'newAuthz" *: *"[^"]*"' | cut -d '"' -f 3)
2255 fi
48d9a8c1 2256 export ACME_NEW_AUTHZ
2257
cae50e16 2258 ACME_NEW_ORDER=$(echo "$response" | _egrep_o 'new-cert" *: *"[^"]*"' | cut -d '"' -f 3)
a71eba07 2259 ACME_NEW_ORDER_RES="new-cert"
cae50e16 2260 if [ -z "$ACME_NEW_ORDER" ]; then
2261 ACME_NEW_ORDER=$(echo "$response" | _egrep_o 'new-order" *: *"[^"]*"' | cut -d '"' -f 3)
a71eba07 2262 ACME_NEW_ORDER_RES="new-order"
c1151b0d 2263 if [ -z "$ACME_NEW_ORDER" ]; then
2264 ACME_NEW_ORDER=$(echo "$response" | _egrep_o 'newOrder" *: *"[^"]*"' | cut -d '"' -f 3)
2265 fi
cae50e16 2266 fi
2267 export ACME_NEW_ORDER
a71eba07 2268 export ACME_NEW_ORDER_RES
48d9a8c1 2269
cae50e16 2270 ACME_NEW_ACCOUNT=$(echo "$response" | _egrep_o 'new-reg" *: *"[^"]*"' | cut -d '"' -f 3)
a71eba07 2271 ACME_NEW_ACCOUNT_RES="new-reg"
cae50e16 2272 if [ -z "$ACME_NEW_ACCOUNT" ]; then
2273 ACME_NEW_ACCOUNT=$(echo "$response" | _egrep_o 'new-account" *: *"[^"]*"' | cut -d '"' -f 3)
a71eba07 2274 ACME_NEW_ACCOUNT_RES="new-account"
c1151b0d 2275 if [ -z "$ACME_NEW_ACCOUNT" ]; then
2276 ACME_NEW_ACCOUNT=$(echo "$response" | _egrep_o 'newAccount" *: *"[^"]*"' | cut -d '"' -f 3)
2277 if [ "$ACME_NEW_ACCOUNT" ]; then
2278 export ACME_VERSION=2
2279 fi
2280 fi
cae50e16 2281 fi
2282 export ACME_NEW_ACCOUNT
a71eba07 2283 export ACME_NEW_ACCOUNT_RES
48d9a8c1 2284
2285 ACME_REVOKE_CERT=$(echo "$response" | _egrep_o 'revoke-cert" *: *"[^"]*"' | cut -d '"' -f 3)
c1151b0d 2286 if [ -z "$ACME_REVOKE_CERT" ]; then
2287 ACME_REVOKE_CERT=$(echo "$response" | _egrep_o 'revokeCert" *: *"[^"]*"' | cut -d '"' -f 3)
2288 fi
48d9a8c1 2289 export ACME_REVOKE_CERT
2290
cae50e16 2291 ACME_NEW_NONCE=$(echo "$response" | _egrep_o 'new-nonce" *: *"[^"]*"' | cut -d '"' -f 3)
c1151b0d 2292 if [ -z "$ACME_NEW_NONCE" ]; then
2293 ACME_NEW_NONCE=$(echo "$response" | _egrep_o 'newNonce" *: *"[^"]*"' | cut -d '"' -f 3)
2294 fi
cae50e16 2295 export ACME_NEW_NONCE
4249e13e 2296
f87890cb 2297 ACME_AGREEMENT=$(echo "$response" | _egrep_o 'terms-of-service" *: *"[^"]*"' | cut -d '"' -f 3)
c1151b0d 2298 if [ -z "$ACME_AGREEMENT" ]; then
2299 ACME_AGREEMENT=$(echo "$response" | _egrep_o 'termsOfService" *: *"[^"]*"' | cut -d '"' -f 3)
2300 fi
f87890cb 2301 export ACME_AGREEMENT
cae50e16 2302
f87890cb 2303 _debug "ACME_KEY_CHANGE" "$ACME_KEY_CHANGE"
2304 _debug "ACME_NEW_AUTHZ" "$ACME_NEW_AUTHZ"
2305 _debug "ACME_NEW_ORDER" "$ACME_NEW_ORDER"
2306 _debug "ACME_NEW_ACCOUNT" "$ACME_NEW_ACCOUNT"
2307 _debug "ACME_REVOKE_CERT" "$ACME_REVOKE_CERT"
2308 _debug "ACME_AGREEMENT" "$ACME_AGREEMENT"
c1151b0d 2309 _debug "ACME_NEW_NONCE" "$ACME_NEW_NONCE"
2310 _debug "ACME_VERSION" "$ACME_VERSION"
48d9a8c1 2311
f87890cb 2312 fi
48d9a8c1 2313}
2314
3281043e 2315#[domain] [keylength or isEcc flag]
5ea6e9c9 2316_initpath() {
cd9fb3b6 2317 domain="$1"
2318 _ilength="$2"
5ea6e9c9 2319
2320 __initHome
2321
4c2a3841 2322 if [ -f "$ACCOUNT_CONF_PATH" ]; then
8663fb7e 2323 . "$ACCOUNT_CONF_PATH"
4c3b3608 2324 fi
2325
4c2a3841 2326 if [ "$IN_CRON" ]; then
2327 if [ ! "$_USER_PATH_EXPORTED" ]; then
281aa349 2328 _USER_PATH_EXPORTED=1
2329 export PATH="$USER_PATH:$PATH"
2330 fi
2331 fi
4c2a3841 2332
2333 if [ -z "$CA_HOME" ]; then
5c48e139 2334 CA_HOME="$DEFAULT_CA_HOME"
2335 fi
281aa349 2336
72f54ca6 2337 if [ "$ACME_VERSION" = "2" ]; then
2338 DEFAULT_CA="$LETSENCRYPT_CA_V2"
2339 DEFAULT_STAGING_CA="$LETSENCRYPT_STAGING_CA_V2"
2340 fi
2341
48d9a8c1 2342 if [ -z "$ACME_DIRECTORY" ]; then
4c2a3841 2343 if [ -z "$STAGE" ]; then
48d9a8c1 2344 ACME_DIRECTORY="$DEFAULT_CA"
4c3b3608 2345 else
c1151b0d 2346 ACME_DIRECTORY="$DEFAULT_STAGING_CA"
48d9a8c1 2347 _info "Using stage ACME_DIRECTORY: $ACME_DIRECTORY"
4c2a3841 2348 fi
4c3b3608 2349 fi
4c2a3841 2350
66444663 2351 _debug ACME_DIRECTORY "$ACME_DIRECTORY"
98394f99 2352 _ACME_SERVER_HOST="$(echo "$ACME_DIRECTORY" | cut -d : -f 2 | tr -s / | cut -d / -f 2)"
48d9a8c1 2353 _debug2 "_ACME_SERVER_HOST" "$_ACME_SERVER_HOST"
2354
2355 CA_DIR="$CA_HOME/$_ACME_SERVER_HOST"
4c2a3841 2356
5c48e139 2357 _DEFAULT_CA_CONF="$CA_DIR/ca.conf"
4c2a3841 2358
2359 if [ -z "$CA_CONF" ]; then
5c48e139 2360 CA_CONF="$_DEFAULT_CA_CONF"
2361 fi
c4236e58 2362 _debug3 CA_CONF "$CA_CONF"
4c2a3841 2363
2364 if [ -f "$CA_CONF" ]; then
5c48e139 2365 . "$CA_CONF"
2366 fi
2367
4c2a3841 2368 if [ -z "$ACME_DIR" ]; then
4c3b3608 2369 ACME_DIR="/home/.acme"
2370 fi
4c2a3841 2371
2372 if [ -z "$APACHE_CONF_BACKUP_DIR" ]; then
f5b546b3 2373 APACHE_CONF_BACKUP_DIR="$LE_CONFIG_HOME"
4c3b3608 2374 fi
4c2a3841 2375
2376 if [ -z "$USER_AGENT" ]; then
bbbdcb09 2377 USER_AGENT="$DEFAULT_USER_AGENT"
2378 fi
4c2a3841 2379
2380 if [ -z "$HTTP_HEADER" ]; then
f5b546b3 2381 HTTP_HEADER="$LE_CONFIG_HOME/http.header"
933c169d 2382 fi
b2817897 2383
5c48e139 2384 _OLD_ACCOUNT_KEY="$LE_WORKING_DIR/account.key"
2385 _OLD_ACCOUNT_JSON="$LE_WORKING_DIR/account.json"
4c2a3841 2386
5c48e139 2387 _DEFAULT_ACCOUNT_KEY_PATH="$CA_DIR/account.key"
2388 _DEFAULT_ACCOUNT_JSON_PATH="$CA_DIR/account.json"
4c2a3841 2389 if [ -z "$ACCOUNT_KEY_PATH" ]; then
b2817897 2390 ACCOUNT_KEY_PATH="$_DEFAULT_ACCOUNT_KEY_PATH"
4c3b3608 2391 fi
4c2a3841 2392
2393 if [ -z "$ACCOUNT_JSON_PATH" ]; then
5c48e139 2394 ACCOUNT_JSON_PATH="$_DEFAULT_ACCOUNT_JSON_PATH"
2395 fi
4c2a3841 2396
f5b546b3 2397 _DEFAULT_CERT_HOME="$LE_CONFIG_HOME"
4c2a3841 2398 if [ -z "$CERT_HOME" ]; then
a79b26af
RD
2399 CERT_HOME="$_DEFAULT_CERT_HOME"
2400 fi
2401
77f1ea40 2402 if [ -z "$ACME_OPENSSL_BIN" ] || [ ! -f "$ACME_OPENSSL_BIN" ] || [ ! -x "$ACME_OPENSSL_BIN" ]; then
851fedf7 2403 ACME_OPENSSL_BIN="$DEFAULT_OPENSSL_BIN"
a746139c 2404 fi
2405
cd9fb3b6 2406 if [ -z "$domain" ]; then
4c3b3608 2407 return 0
2408 fi
4c2a3841 2409
4c2a3841 2410 if [ -z "$DOMAIN_PATH" ]; then
43822d37 2411 domainhome="$CERT_HOME/$domain"
2412 domainhomeecc="$CERT_HOME/$domain$ECC_SUFFIX"
4c2a3841 2413
4c3b3608 2414 DOMAIN_PATH="$domainhome"
4c2a3841 2415
2416 if _isEccKey "$_ilength"; then
43822d37 2417 DOMAIN_PATH="$domainhomeecc"
2418 else
4c2a3841 2419 if [ ! -d "$domainhome" ] && [ -d "$domainhomeecc" ]; then
6d4e903b 2420 _info "The domain '$domain' seems to have a ECC cert already, please add '$(__red "--ecc")' parameter if you want to use that cert."
43822d37 2421 fi
2422 fi
2423 _debug DOMAIN_PATH "$DOMAIN_PATH"
4c3b3608 2424 fi
4c2a3841 2425
fd72cced 2426 if [ -z "$DOMAIN_BACKUP_PATH" ]; then
d88f8e86 2427 DOMAIN_BACKUP_PATH="$DOMAIN_PATH/backup"
fd72cced 2428 fi
2429
4c2a3841 2430 if [ -z "$DOMAIN_CONF" ]; then
43822d37 2431 DOMAIN_CONF="$DOMAIN_PATH/$domain.conf"
4c3b3608 2432 fi
4c2a3841 2433
2434 if [ -z "$DOMAIN_SSL_CONF" ]; then
0c9546cc 2435 DOMAIN_SSL_CONF="$DOMAIN_PATH/$domain.csr.conf"
4c3b3608 2436 fi
4c2a3841 2437
2438 if [ -z "$CSR_PATH" ]; then
43822d37 2439 CSR_PATH="$DOMAIN_PATH/$domain.csr"
4c3b3608 2440 fi
4c2a3841 2441 if [ -z "$CERT_KEY_PATH" ]; then
43822d37 2442 CERT_KEY_PATH="$DOMAIN_PATH/$domain.key"
4c3b3608 2443 fi
4c2a3841 2444 if [ -z "$CERT_PATH" ]; then
43822d37 2445 CERT_PATH="$DOMAIN_PATH/$domain.cer"
4c3b3608 2446 fi
4c2a3841 2447 if [ -z "$CA_CERT_PATH" ]; then
43822d37 2448 CA_CERT_PATH="$DOMAIN_PATH/ca.cer"
4c3b3608 2449 fi
4c2a3841 2450 if [ -z "$CERT_FULLCHAIN_PATH" ]; then
43822d37 2451 CERT_FULLCHAIN_PATH="$DOMAIN_PATH/fullchain.cer"
caf1fc10 2452 fi
4c2a3841 2453 if [ -z "$CERT_PFX_PATH" ]; then
43822d37 2454 CERT_PFX_PATH="$DOMAIN_PATH/$domain.pfx"
ac2d5123 2455 fi
4410226d 2456 if [ -z "$CERT_PKCS8_PATH" ]; then
2457 CERT_PKCS8_PATH="$DOMAIN_PATH/$domain.pkcs8"
2458 fi
4c2a3841 2459
2460 if [ -z "$TLS_CONF" ]; then
f94433e5 2461 TLS_CONF="$DOMAIN_PATH/tls.validation.conf"
e22bcf7c 2462 fi
4c2a3841 2463 if [ -z "$TLS_CERT" ]; then
f94433e5 2464 TLS_CERT="$DOMAIN_PATH/tls.validation.cert"
e22bcf7c 2465 fi
4c2a3841 2466 if [ -z "$TLS_KEY" ]; then
f94433e5 2467 TLS_KEY="$DOMAIN_PATH/tls.validation.key"
e22bcf7c 2468 fi
4c2a3841 2469 if [ -z "$TLS_CSR" ]; then
f94433e5 2470 TLS_CSR="$DOMAIN_PATH/tls.validation.csr"
e22bcf7c 2471 fi
4c2a3841 2472
4c3b3608 2473}
2474
610e0f21 2475_exec() {
4c2a3841 2476 if [ -z "$_EXEC_TEMP_ERR" ]; then
610e0f21 2477 _EXEC_TEMP_ERR="$(_mktemp)"
2478 fi
2479
4c2a3841 2480 if [ "$_EXEC_TEMP_ERR" ]; then
3e5b1024 2481 eval "$@ 2>>$_EXEC_TEMP_ERR"
610e0f21 2482 else
3e5b1024 2483 eval "$@"
610e0f21 2484 fi
2485}
2486
2487_exec_err() {
3e5b1024 2488 [ "$_EXEC_TEMP_ERR" ] && _err "$(cat "$_EXEC_TEMP_ERR")" && echo "" >"$_EXEC_TEMP_ERR"
610e0f21 2489}
4c3b3608 2490
2491_apachePath() {
c3dd3ef0 2492 _APACHECTL="apachectl"
4c2a3841 2493 if ! _exists apachectl; then
2494 if _exists apache2ctl; then
2495 _APACHECTL="apache2ctl"
e4a19585 2496 else
bc96082f 2497 _err "'apachectl not found. It seems that apache is not installed, or you are not root user.'"
e4a19585 2498 _err "Please use webroot mode to try again."
2499 return 1
2500 fi
80a0a7b5 2501 fi
4c2a3841 2502
2503 if ! _exec $_APACHECTL -V >/dev/null; then
610e0f21 2504 _exec_err
2505 return 1
2506 fi
4c2a3841 2507
2508 if [ "$APACHE_HTTPD_CONF" ]; then
5be1449d 2509 _saveaccountconf APACHE_HTTPD_CONF "$APACHE_HTTPD_CONF"
2510 httpdconf="$APACHE_HTTPD_CONF"
79a267ab 2511 httpdconfname="$(basename "$httpdconfname")"
d62ee940 2512 else
4c2a3841 2513 httpdconfname="$($_APACHECTL -V | grep SERVER_CONFIG_FILE= | cut -d = -f 2 | tr -d '"')"
5be1449d 2514 _debug httpdconfname "$httpdconfname"
4c2a3841 2515
2516 if [ -z "$httpdconfname" ]; then
5be1449d 2517 _err "Can not read apache config file."
2518 return 1
2519 fi
4c2a3841 2520
2521 if _startswith "$httpdconfname" '/'; then
5be1449d 2522 httpdconf="$httpdconfname"
79a267ab 2523 httpdconfname="$(basename "$httpdconfname")"
5be1449d 2524 else
4c2a3841 2525 httpdroot="$($_APACHECTL -V | grep HTTPD_ROOT= | cut -d = -f 2 | tr -d '"')"
5be1449d 2526 _debug httpdroot "$httpdroot"
2527 httpdconf="$httpdroot/$httpdconfname"
79a267ab 2528 httpdconfname="$(basename "$httpdconfname")"
5be1449d 2529 fi
d62ee940 2530 fi
78768e98 2531 _debug httpdconf "$httpdconf"
8f63baf7 2532 _debug httpdconfname "$httpdconfname"
4c2a3841 2533 if [ ! -f "$httpdconf" ]; then
78768e98 2534 _err "Apache Config file not found" "$httpdconf"
4c3b3608 2535 return 1
2536 fi
2537 return 0
2538}
2539
2540_restoreApache() {
4c2a3841 2541 if [ -z "$usingApache" ]; then
4c3b3608 2542 return 0
2543 fi
2544 _initpath
4c2a3841 2545 if ! _apachePath; then
4c3b3608 2546 return 1
2547 fi
4c2a3841 2548
2549 if [ ! -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname" ]; then
4c3b3608 2550 _debug "No config file to restore."
2551 return 0
2552 fi
4c2a3841 2553
2554 cat "$APACHE_CONF_BACKUP_DIR/$httpdconfname" >"$httpdconf"
5ef501c5 2555 _debug "Restored: $httpdconf."
4c2a3841 2556 if ! _exec $_APACHECTL -t; then
610e0f21 2557 _exec_err
4c3b3608 2558 _err "Sorry, restore apache config error, please contact me."
4c2a3841 2559 return 1
4c3b3608 2560 fi
5ef501c5 2561 _debug "Restored successfully."
4c3b3608 2562 rm -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname"
4c2a3841 2563 return 0
4c3b3608 2564}
2565
2566_setApache() {
2567 _initpath
4c2a3841 2568 if ! _apachePath; then
4c3b3608 2569 return 1
2570 fi
2571
5fc5016d 2572 #test the conf first
869578ce 2573 _info "Checking if there is an error in the apache config file before starting."
4c2a3841 2574
44edb2bd 2575 if ! _exec "$_APACHECTL" -t >/dev/null; then
610e0f21 2576 _exec_err
2577 _err "The apache config file has error, please fix it first, then try again."
869578ce 2578 _err "Don't worry, there is nothing changed to your system."
4c2a3841 2579 return 1
5fc5016d 2580 else
2581 _info "OK"
2582 fi
4c2a3841 2583
4c3b3608 2584 #backup the conf
5778811a 2585 _debug "Backup apache config file" "$httpdconf"
4c2a3841 2586 if ! cp "$httpdconf" "$APACHE_CONF_BACKUP_DIR/"; then
869578ce 2587 _err "Can not backup apache config file, so abort. Don't worry, the apache config is not changed."
61a48a5b 2588 _err "This might be a bug of $PROJECT_NAME , please report issue: $PROJECT"
8f63baf7 2589 return 1
2590 fi
4c3b3608 2591 _info "JFYI, Config file $httpdconf is backuped to $APACHE_CONF_BACKUP_DIR/$httpdconfname"
2592 _info "In case there is an error that can not be restored automatically, you may try restore it yourself."
329174b6 2593 _info "The backup file will be deleted on success, just forget it."
4c2a3841 2594
4c3b3608 2595 #add alias
4c2a3841 2596
2597 apacheVer="$($_APACHECTL -V | grep "Server version:" | cut -d : -f 2 | cut -d " " -f 2 | cut -d '/' -f 2)"
b09d597c 2598 _debug "apacheVer" "$apacheVer"
2599 apacheMajer="$(echo "$apacheVer" | cut -d . -f 1)"
2600 apacheMinor="$(echo "$apacheVer" | cut -d . -f 2)"
2601
4c2a3841 2602 if [ "$apacheVer" ] && [ "$apacheMajer$apacheMinor" -ge "24" ]; then
b09d597c 2603 echo "
4c3b3608 2604Alias /.well-known/acme-challenge $ACME_DIR
2605
2606<Directory $ACME_DIR >
2607Require all granted
b09d597c 2608</Directory>
4c2a3841 2609 " >>"$httpdconf"
b09d597c 2610 else
2611 echo "
2612Alias /.well-known/acme-challenge $ACME_DIR
2613
2614<Directory $ACME_DIR >
2615Order allow,deny
2616Allow from all
4c3b3608 2617</Directory>
4c2a3841 2618 " >>"$httpdconf"
b09d597c 2619 fi
2620
4c2a3841 2621 _msg="$($_APACHECTL -t 2>&1)"
2622 if [ "$?" != "0" ]; then
5fc5016d 2623 _err "Sorry, apache config error"
4c2a3841 2624 if _restoreApache; then
869578ce 2625 _err "The apache config file is restored."
5fc5016d 2626 else
869578ce 2627 _err "Sorry, The apache config file can not be restored, please report bug."
5fc5016d 2628 fi
4c2a3841 2629 return 1
4c3b3608 2630 fi
4c2a3841 2631
2632 if [ ! -d "$ACME_DIR" ]; then
4c3b3608 2633 mkdir -p "$ACME_DIR"
2634 chmod 755 "$ACME_DIR"
2635 fi
4c2a3841 2636
44edb2bd 2637 if ! _exec "$_APACHECTL" graceful; then
4c2a3841 2638 _exec_err
610e0f21 2639 _err "$_APACHECTL graceful error, please contact me."
4c3b3608 2640 _restoreApache
4c2a3841 2641 return 1
4c3b3608 2642 fi
2643 usingApache="1"
2644 return 0
2645}
2646
9d725af6 2647#find the real nginx conf file
2648#backup
2649#set the nginx conf
2650#returns the real nginx conf file
2651_setNginx() {
2652 _d="$1"
2653 _croot="$2"
2654 _thumbpt="$3"
37f39c08 2655
9d725af6 2656 FOUND_REAL_NGINX_CONF=""
9f90618a 2657 FOUND_REAL_NGINX_CONF_LN=""
9d725af6 2658 BACKUP_NGINX_CONF=""
2659 _debug _croot "$_croot"
2660 _start_f="$(echo "$_croot" | cut -d : -f 2)"
2661 _debug _start_f "$_start_f"
2662 if [ -z "$_start_f" ]; then
2663 _debug "find start conf from nginx command"
2664 if [ -z "$NGINX_CONF" ]; then
37f39c08 2665 if ! _exists "nginx"; then
2666 _err "nginx command is not found."
2667 return 1
2668 fi
9d725af6 2669 NGINX_CONF="$(nginx -V 2>&1 | _egrep_o "--conf-path=[^ ]* " | tr -d " ")"
2670 _debug NGINX_CONF "$NGINX_CONF"
2671 NGINX_CONF="$(echo "$NGINX_CONF" | cut -d = -f 2)"
2672 _debug NGINX_CONF "$NGINX_CONF"
2673 if [ ! -f "$NGINX_CONF" ]; then
2674 _err "'$NGINX_CONF' doesn't exist."
2675 NGINX_CONF=""
2676 return 1
2677 fi
2678 _debug "Found nginx conf file:$NGINX_CONF"
2679 fi
2680 _start_f="$NGINX_CONF"
2681 fi
03f8d6e9 2682 _debug "Start detect nginx conf for $_d from:$_start_f"
9d725af6 2683 if ! _checkConf "$_d" "$_start_f"; then
5378d9ca 2684 _err "Can not find conf file for domain $d"
9d725af6 2685 return 1
2686 fi
2687 _info "Found conf file: $FOUND_REAL_NGINX_CONF"
2688
9f90618a 2689 _ln=$FOUND_REAL_NGINX_CONF_LN
03f8d6e9 2690 _debug "_ln" "$_ln"
2691
2692 _lnn=$(_math $_ln + 1)
2693 _debug _lnn "$_lnn"
2694 _start_tag="$(sed -n "$_lnn,${_lnn}p" "$FOUND_REAL_NGINX_CONF")"
2695 _debug "_start_tag" "$_start_tag"
2696 if [ "$_start_tag" = "$NGINX_START" ]; then
2697 _info "The domain $_d is already configured, skip"
2698 FOUND_REAL_NGINX_CONF=""
2699 return 0
2700 fi
2701
9d725af6 2702 mkdir -p "$DOMAIN_BACKUP_PATH"
2703 _backup_conf="$DOMAIN_BACKUP_PATH/$_d.nginx.conf"
2704 _debug _backup_conf "$_backup_conf"
2705 BACKUP_NGINX_CONF="$_backup_conf"
2706 _info "Backup $FOUND_REAL_NGINX_CONF to $_backup_conf"
2707 if ! cp "$FOUND_REAL_NGINX_CONF" "$_backup_conf"; then
2708 _err "backup error."
2709 FOUND_REAL_NGINX_CONF=""
2710 return 1
2711 fi
2712
37f39c08 2713 if ! _exists "nginx"; then
2714 _err "nginx command is not found."
2715 return 1
2716 fi
9d725af6 2717 _info "Check the nginx conf before setting up."
2718 if ! _exec "nginx -t" >/dev/null; then
2719 _exec_err
2720 return 1
2721 fi
2722
2723 _info "OK, Set up nginx config file"
9d725af6 2724
302c41ed 2725 if ! sed -n "1,${_ln}p" "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"; then
03f8d6e9 2726 cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"
9d725af6 2727 _err "write nginx conf error, but don't worry, the file is restored to the original version."
2728 return 1
2729 fi
2730
03f8d6e9 2731 echo "$NGINX_START
9d725af6 2732location ~ \"^/\.well-known/acme-challenge/([-_a-zA-Z0-9]+)\$\" {
2733 default_type text/plain;
2734 return 200 \"\$1.$_thumbpt\";
3c07f57a 2735}
03f8d6e9 2736#NGINX_START
2737" >>"$FOUND_REAL_NGINX_CONF"
9d725af6 2738
03f8d6e9 2739 if ! sed -n "${_lnn},99999p" "$_backup_conf" >>"$FOUND_REAL_NGINX_CONF"; then
2740 cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"
9d725af6 2741 _err "write nginx conf error, but don't worry, the file is restored."
2742 return 1
2743 fi
df711b0e 2744 _debug3 "Modified config:$(cat $FOUND_REAL_NGINX_CONF)"
9d725af6 2745 _info "nginx conf is done, let's check it again."
2746 if ! _exec "nginx -t" >/dev/null; then
2747 _exec_err
2748 _err "It seems that nginx conf was broken, let's restore."
302c41ed 2749 cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"
9d725af6 2750 return 1
2751 fi
2752
2753 _info "Reload nginx"
2754 if ! _exec "nginx -s reload" >/dev/null; then
2755 _exec_err
2756 _err "It seems that nginx reload error, let's restore."
302c41ed 2757 cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"
9d725af6 2758 return 1
2759 fi
2760
2761 return 0
2762}
2763
2764#d , conf
2765_checkConf() {
2766 _d="$1"
2767 _c_file="$2"
2768 _debug "Start _checkConf from:$_c_file"
2769 if [ ! -f "$2" ] && ! echo "$2" | grep '*$' >/dev/null && echo "$2" | grep '*' >/dev/null; then
2770 _debug "wildcard"
2771 for _w_f in $2; do
7f618e7e 2772 if [ -f "$_w_f" ] && _checkConf "$1" "$_w_f"; then
9d725af6 2773 return 0
2774 fi
2775 done
2776 #not found
2777 return 1
2778 elif [ -f "$2" ]; then
2779 _debug "single"
2780 if _isRealNginxConf "$1" "$2"; then
2781 _debug "$2 is found."
2782 FOUND_REAL_NGINX_CONF="$2"
2783 return 0
2784 fi
f08a79d3 2785 if cat "$2" | tr "\t" " " | grep "^ *include *.*;" >/dev/null; then
9d725af6 2786 _debug "Try include files"
f08a79d3 2787 for included in $(cat "$2" | tr "\t" " " | grep "^ *include *.*;" | sed "s/include //" | tr -d " ;"); do
9d725af6 2788 _debug "check included $included"
2789 if _checkConf "$1" "$included"; then
2790 return 0
2791 fi
2792 done
2793 fi
2794 return 1
2795 else
2796 _debug "$2 not found."
2797 return 1
2798 fi
2799 return 1
2800}
2801
2802#d , conf
2803_isRealNginxConf() {
2804 _debug "_isRealNginxConf $1 $2"
302c41ed 2805 if [ -f "$2" ]; then
3f1a76d9 2806 for _fln in $(tr "\t" ' ' <"$2" | grep -n "^ *server_name.* $1" | cut -d : -f 1); do
302c41ed 2807 _debug _fln "$_fln"
2808 if [ "$_fln" ]; then
04a609b5 2809 _start=$(tr "\t" ' ' <"$2" | _head_n "$_fln" | grep -n "^ *server *" | grep -v server_name | _tail_n 1)
9f90618a 2810 _debug "_start" "$_start"
2811 _start_n=$(echo "$_start" | cut -d : -f 1)
2812 _start_nn=$(_math $_start_n + 1)
2813 _debug "_start_n" "$_start_n"
2814 _debug "_start_nn" "$_start_nn"
2815
2816 _left="$(sed -n "${_start_nn},99999p" "$2")"
2817 _debug2 _left "$_left"
012dd698 2818 _end="$(echo "$_left" | tr "\t" ' ' | grep -n "^ *server *" | grep -v server_name | _head_n 1)"
2819 _debug "_end" "$_end"
2820 if [ "$_end" ]; then
9f90618a 2821 _end_n=$(echo "$_end" | cut -d : -f 1)
2822 _debug "_end_n" "$_end_n"
2823 _seg_n=$(echo "$_left" | sed -n "1,${_end_n}p")
2824 else
2825 _seg_n="$_left"
2826 fi
2827
2828 _debug "_seg_n" "$_seg_n"
2829
04a609b5 2830 _skip_ssl=1
d1067c60 2831 for _listen_i in $(echo "$_seg_n" | tr "\t" ' ' | grep "^ *listen" | tr -d " "); do
04a609b5 2832 if [ "$_listen_i" ]; then
2833 if [ "$(echo "$_listen_i" | _egrep_o "listen.*ssl[ |;]")" ]; then
2834 _debug2 "$_listen_i is ssl"
2835 else
2836 _debug2 "$_listen_i is plain text"
2837 _skip_ssl=""
c05eb0b1 2838 break
2839 fi
04a609b5 2840 fi
2841 done
2842
2843 if [ "$_skip_ssl" = "1" ]; then
9f90618a 2844 _debug "ssl on, skip"
241cfc43 2845 else
2846 FOUND_REAL_NGINX_CONF_LN=$_fln
2847 _debug3 "found FOUND_REAL_NGINX_CONF_LN" "$FOUND_REAL_NGINX_CONF_LN"
2848 return 0
450efea1 2849 fi
302c41ed 2850 fi
2851 done
9d725af6 2852 fi
302c41ed 2853 return 1
9d725af6 2854}
2855
2856#restore all the nginx conf
2857_restoreNginx() {
5d943a35 2858 if [ -z "$NGINX_RESTORE_VLIST" ]; then
9d725af6 2859 _debug "No need to restore nginx, skip."
2860 return
2861 fi
2862 _debug "_restoreNginx"
5d943a35 2863 _debug "NGINX_RESTORE_VLIST" "$NGINX_RESTORE_VLIST"
9d725af6 2864
5d943a35 2865 for ng_entry in $(echo "$NGINX_RESTORE_VLIST" | tr "$dvsep" ' '); do
9d725af6 2866 _debug "ng_entry" "$ng_entry"
2867 _nd=$(echo "$ng_entry" | cut -d "$sep" -f 1)
2868 _ngconf=$(echo "$ng_entry" | cut -d "$sep" -f 2)
2869 _ngbackupconf=$(echo "$ng_entry" | cut -d "$sep" -f 3)
2870 _info "Restoring from $_ngbackupconf to $_ngconf"
302c41ed 2871 cat "$_ngbackupconf" >"$_ngconf"
9d725af6 2872 done
2873
2874 _info "Reload nginx"
2875 if ! _exec "nginx -s reload" >/dev/null; then
2876 _exec_err
2877 _err "It seems that nginx reload error, please report bug."
2878 return 1
2879 fi
2880 return 0
2881}
2882
5ef501c5 2883_clearup() {
44edb2bd 2884 _stopserver "$serverproc"
4c3b3608 2885 serverproc=""
2886 _restoreApache
9d725af6 2887 _restoreNginx
800e3f45 2888 _clearupdns
4c2a3841 2889 if [ -z "$DEBUG" ]; then
e22bcf7c 2890 rm -f "$TLS_CONF"
2891 rm -f "$TLS_CERT"
2892 rm -f "$TLS_KEY"
2893 rm -f "$TLS_CSR"
2894 fi
4c3b3608 2895}
2896
800e3f45 2897_clearupdns() {
2898 _debug "_clearupdns"
4c2a3841 2899 if [ "$dnsadded" != 1 ] || [ -z "$vlist" ]; then
65b22b49 2900 _debug "skip dns."
800e3f45 2901 return
2902 fi
875625b1 2903 _info "Removing DNS records."
4c2a3841 2904 ventries=$(echo "$vlist" | tr ',' ' ')
875625b1 2905 _alias_index=1
4c2a3841 2906 for ventry in $ventries; do
0c538f75 2907 d=$(echo "$ventry" | cut -d "$sep" -f 1)
2908 keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2)
2909 vtype=$(echo "$ventry" | cut -d "$sep" -f 4)
2910 _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5)
11927a76 2911 txt="$(printf "%s" "$keyauthorization" | _digest "sha256" | _url_replace)"
21f201e3 2912 _debug txt "$txt"
4c2a3841 2913 if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
69212114 2914 _debug "$d is already verified, skip $vtype."
800e3f45 2915 continue
2916 fi
2917
4c2a3841 2918 if [ "$vtype" != "$VTYPE_DNS" ]; then
875625b1 2919 _debug "Skip $d for $vtype"
800e3f45 2920 continue
2921 fi
4c2a3841 2922
0c538f75 2923 d_api="$(_findHook "$d" dnsapi "$_currentRoot")"
800e3f45 2924 _debug d_api "$d_api"
4c2a3841 2925
2926 if [ -z "$d_api" ]; then
800e3f45 2927 _info "Not Found domain api file: $d_api"
2928 continue
2929 fi
4c2a3841 2930
800e3f45 2931 (
d5ec5f80 2932 if ! . "$d_api"; then
800e3f45 2933 _err "Load file $d_api error. Please check your api file and try again."
2934 return 1
2935 fi
4c2a3841 2936
800e3f45 2937 rmcommand="${_currentRoot}_rm"
d5ec5f80 2938 if ! _exists "$rmcommand"; then
800e3f45 2939 _err "It seems that your api file doesn't define $rmcommand"
2940 return 1
2941 fi
4c2a3841 2942
72f54ca6 2943 _dns_root_d="$d"
2944 if _startswith "$_dns_root_d" "*."; then
2945 _dns_root_d="$(echo "$_dns_root_d" | sed 's/*.//')"
2946 fi
875625b1 2947
2948 _d_alias="$(_getfield "$_challenge_alias" "$_alias_index")"
2949 _alias_index="$(_math "$_alias_index" + 1)"
2950 _debug "_d_alias" "$_d_alias"
2951 if [ "$_d_alias" ]; then
64821ad4 2952 if _startswith "$_d_alias" "$DNS_ALIAS_PREFIX"; then
2953 txtdomain="$(echo "$_d_alias" | sed "s/$DNS_ALIAS_PREFIX//")"
2954 else
2955 txtdomain="_acme-challenge.$_d_alias"
2956 fi
875625b1 2957 else
2958 txtdomain="_acme-challenge.$_dns_root_d"
2959 fi
4c2a3841 2960
21f201e3 2961 if ! $rmcommand "$txtdomain" "$txt"; then
800e3f45 2962 _err "Error removing txt for domain:$txtdomain"
2963 return 1
2964 fi
2965 )
4c2a3841 2966
800e3f45 2967 done
2968}
2969
4c3b3608 2970# webroot removelevel tokenfile
2971_clearupwebbroot() {
2972 __webroot="$1"
4c2a3841 2973 if [ -z "$__webroot" ]; then
4c3b3608 2974 _debug "no webroot specified, skip"
2975 return 0
2976 fi
4c2a3841 2977
dcf9cb58 2978 _rmpath=""
4c2a3841 2979 if [ "$2" = '1' ]; then
dcf9cb58 2980 _rmpath="$__webroot/.well-known"
4c2a3841 2981 elif [ "$2" = '2' ]; then
dcf9cb58 2982 _rmpath="$__webroot/.well-known/acme-challenge"
4c2a3841 2983 elif [ "$2" = '3' ]; then
dcf9cb58 2984 _rmpath="$__webroot/.well-known/acme-challenge/$3"
4c3b3608 2985 else
cc179731 2986 _debug "Skip for removelevel:$2"
4c3b3608 2987 fi
4c2a3841 2988
2989 if [ "$_rmpath" ]; then
2990 if [ "$DEBUG" ]; then
dcf9cb58 2991 _debug "Debugging, skip removing: $_rmpath"
2992 else
2993 rm -rf "$_rmpath"
2994 fi
2995 fi
4c2a3841 2996
4c3b3608 2997 return 0
2998
2999}
3000
b0070f03 3001_on_before_issue() {
af1cc3b3 3002 _chk_web_roots="$1"
02140ce7 3003 _chk_main_domain="$2"
3004 _chk_alt_domains="$3"
85e1f4ea 3005 _chk_pre_hook="$4"
3006 _chk_local_addr="$5"
30c2d84c 3007 _debug _on_before_issue
38f1b4d2 3008 _debug _chk_main_domain "$_chk_main_domain"
3009 _debug _chk_alt_domains "$_chk_alt_domains"
d0f7c309 3010 #run pre hook
85e1f4ea 3011 if [ "$_chk_pre_hook" ]; then
3012 _info "Run pre hook:'$_chk_pre_hook'"
d0f7c309 3013 if ! (
85e1f4ea 3014 cd "$DOMAIN_PATH" && eval "$_chk_pre_hook"
d0f7c309 3015 ); then
3016 _err "Error when run pre hook."
3017 return 1
3018 fi
3019 fi
3020
af1cc3b3 3021 if _hasfield "$_chk_web_roots" "$NO_VALUE"; then
3794b5cb 3022 if ! _exists "socat"; then
3023 _err "Please install socat tools first."
0463b5d6 3024 return 1
3025 fi
0463b5d6 3026 fi
3027
85e1f4ea 3028 _debug Le_LocalAddress "$_chk_local_addr"
4c2a3841 3029
0463b5d6 3030 _index=1
3031 _currentRoot=""
3032 _addrIndex=1
38f1b4d2 3033 _w_index=1
931d19ee 3034 while true; do
931d19ee 3035 d="$(echo "$_chk_main_domain,$_chk_alt_domains," | cut -d , -f "$_w_index")"
3036 _w_index="$(_math "$_w_index" + 1)"
3037 _debug d "$d"
3038 if [ -z "$d" ]; then
3039 break
3040 fi
d5ec5f80 3041 _debug "Check for domain" "$d"
af1cc3b3 3042 _currentRoot="$(_getfield "$_chk_web_roots" $_index)"
0463b5d6 3043 _debug "_currentRoot" "$_currentRoot"
3044 _index=$(_math $_index + 1)
3045 _checkport=""
4c2a3841 3046 if [ "$_currentRoot" = "$NO_VALUE" ]; then
0463b5d6 3047 _info "Standalone mode."
4c2a3841 3048 if [ -z "$Le_HTTPPort" ]; then
0463b5d6 3049 Le_HTTPPort=80
3050 else
4c2a3841 3051 _savedomainconf "Le_HTTPPort" "$Le_HTTPPort"
0463b5d6 3052 fi
3053 _checkport="$Le_HTTPPort"
4c2a3841 3054 elif [ "$_currentRoot" = "$W_TLS" ]; then
0463b5d6 3055 _info "Standalone tls mode."
4c2a3841 3056 if [ -z "$Le_TLSPort" ]; then
0463b5d6 3057 Le_TLSPort=443
3058 else
4c2a3841 3059 _savedomainconf "Le_TLSPort" "$Le_TLSPort"
0463b5d6 3060 fi
3061 _checkport="$Le_TLSPort"
3062 fi
4c2a3841 3063
3064 if [ "$_checkport" ]; then
0463b5d6 3065 _debug _checkport "$_checkport"
85e1f4ea 3066 _checkaddr="$(_getfield "$_chk_local_addr" $_addrIndex)"
0463b5d6 3067 _debug _checkaddr "$_checkaddr"
4c2a3841 3068
0463b5d6 3069 _addrIndex="$(_math $_addrIndex + 1)"
4c2a3841 3070
0463b5d6 3071 _netprc="$(_ss "$_checkport" | grep "$_checkport")"
3072 netprc="$(echo "$_netprc" | grep "$_checkaddr")"
4c2a3841 3073 if [ -z "$netprc" ]; then
0463b5d6 3074 netprc="$(echo "$_netprc" | grep "$LOCAL_ANY_ADDRESS")"
3075 fi
4c2a3841 3076 if [ "$netprc" ]; then
0463b5d6 3077 _err "$netprc"
4c2a3841 3078 _err "tcp port $_checkport is already used by $(echo "$netprc" | cut -d : -f 4)"
0463b5d6 3079 _err "Please stop it first"
3080 return 1
3081 fi
3082 fi
3083 done
3084
af1cc3b3 3085 if _hasfield "$_chk_web_roots" "apache"; then
4c2a3841 3086 if ! _setApache; then
0463b5d6 3087 _err "set up apache error. Report error to me."
3088 return 1
3089 fi
3090 else
3091 usingApache=""
3092 fi
3093
b0070f03 3094}
3095
3096_on_issue_err() {
85e1f4ea 3097 _chk_post_hook="$1"
58e4d337 3098 _chk_vlist="$2"
30c2d84c 3099 _debug _on_issue_err
cd8fc359 3100
4c2a3841 3101 if [ "$LOG_FILE" ]; then
a73c5b33 3102 _err "Please check log file for more details: $LOG_FILE"
3103 else
54ae008d 3104 _err "Please add '--debug' or '--log' to check more details."
a73c5b33 3105 _err "See: $_DEBUG_WIKI"
3106 fi
4c2a3841 3107
b0070f03 3108 #run the post hook
85e1f4ea 3109 if [ "$_chk_post_hook" ]; then
3110 _info "Run post hook:'$_chk_post_hook'"
b0070f03 3111 if ! (
85e1f4ea 3112 cd "$DOMAIN_PATH" && eval "$_chk_post_hook"
4c2a3841 3113 ); then
b0070f03 3114 _err "Error when run post hook."
3115 return 1
3116 fi
3117 fi
58e4d337 3118
3119 #trigger the validation to flush the pending authz
ea722da3 3120 _debug2 "_chk_vlist" "$_chk_vlist"
58e4d337 3121 if [ "$_chk_vlist" ]; then
3122 (
c719a61e 3123 _debug2 "start to deactivate authz"
3124 ventries=$(echo "$_chk_vlist" | tr "$dvsep" ' ')
3125 for ventry in $ventries; do
3126 d=$(echo "$ventry" | cut -d "$sep" -f 1)
3127 keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2)
3128 uri=$(echo "$ventry" | cut -d "$sep" -f 3)
3129 vtype=$(echo "$ventry" | cut -d "$sep" -f 4)
3130 _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5)
f94433e5 3131 __trigger_validation "$uri" "$keyauthorization"
c719a61e 3132 done
58e4d337 3133 )
3134 fi
3135
3881f221 3136 if [ "$IS_RENEW" = "1" ] && _hasfield "$Le_Webroot" "$W_DNS"; then
309bec47 3137 _err "$_DNS_MANUAL_ERR"
3138 fi
3139
58e4d337 3140 if [ "$DEBUG" ] && [ "$DEBUG" -gt "0" ]; then
3141 _debug "$(_dlg_versions)"
3142 fi
3143
b0070f03 3144}
3145
3146_on_issue_success() {
85e1f4ea 3147 _chk_post_hook="$1"
3148 _chk_renew_hook="$2"
30c2d84c 3149 _debug _on_issue_success
b0070f03 3150 #run the post hook
85e1f4ea 3151 if [ "$_chk_post_hook" ]; then
3152 _info "Run post hook:'$_chk_post_hook'"
b0070f03 3153 if ! (
85e1f4ea 3154 cd "$DOMAIN_PATH" && eval "$_chk_post_hook"
4c2a3841 3155 ); then
b0070f03 3156 _err "Error when run post hook."
3157 return 1
3158 fi
3159 fi
4c2a3841 3160
b0070f03 3161 #run renew hook
85e1f4ea 3162 if [ "$IS_RENEW" ] && [ "$_chk_renew_hook" ]; then
3163 _info "Run renew hook:'$_chk_renew_hook'"
b0070f03 3164 if ! (
85e1f4ea 3165 cd "$DOMAIN_PATH" && eval "$_chk_renew_hook"
4c2a3841 3166 ); then
b0070f03 3167 _err "Error when run renew hook."
3168 return 1
3169 fi
4c2a3841 3170 fi
3171
3881f221 3172 if _hasfield "$Le_Webroot" "$W_DNS"; then
309bec47 3173 _err "$_DNS_MANUAL_WARN"
3174 fi
3175
b0070f03 3176}
3177
eb59817e 3178updateaccount() {
3179 _initpath
3180 _regAccount
3181}
b0070f03 3182
eb59817e 3183registeraccount() {
57e58ce7 3184 _reg_length="$1"
eb59817e 3185 _initpath
57e58ce7 3186 _regAccount "$_reg_length"
eb59817e 3187}
d404e92d 3188
8a29fbc8 3189__calcAccountKeyHash() {
ca7202eb 3190 [ -f "$ACCOUNT_KEY_PATH" ] && _digest sha256 <"$ACCOUNT_KEY_PATH"
8a29fbc8 3191}
3192
339a8ad6 3193__calc_account_thumbprint() {
3194 printf "%s" "$jwk" | tr -d ' ' | _digest "sha256" | _url_replace
3195}
3196
57e58ce7 3197#keylength
d404e92d 3198_regAccount() {
3199 _initpath
57e58ce7 3200 _reg_length="$1"
f87890cb 3201 _debug3 _regAccount "$_regAccount"
c1151b0d 3202 _initAPI
3203
1bbc33a0 3204 mkdir -p "$CA_DIR"
5c48e139 3205 if [ ! -f "$ACCOUNT_KEY_PATH" ] && [ -f "$_OLD_ACCOUNT_KEY" ]; then
3206 _info "mv $_OLD_ACCOUNT_KEY to $ACCOUNT_KEY_PATH"
3207 mv "$_OLD_ACCOUNT_KEY" "$ACCOUNT_KEY_PATH"
3208 fi
4c2a3841 3209
5c48e139 3210 if [ ! -f "$ACCOUNT_JSON_PATH" ] && [ -f "$_OLD_ACCOUNT_JSON" ]; then
3211 _info "mv $_OLD_ACCOUNT_JSON to $ACCOUNT_JSON_PATH"
3212 mv "$_OLD_ACCOUNT_JSON" "$ACCOUNT_JSON_PATH"
3213 fi
4c2a3841 3214
3215 if [ ! -f "$ACCOUNT_KEY_PATH" ]; then
3216 if ! _create_account_key "$_reg_length"; then
d404e92d 3217 _err "Create account key error."
3218 return 1
3219 fi
3220 fi
4c2a3841 3221
3222 if ! _calcjwk "$ACCOUNT_KEY_PATH"; then
d404e92d 3223 return 1
3224 fi
f8d22c48 3225
c1151b0d 3226 if [ "$ACME_VERSION" = "2" ]; then
3227 regjson='{"termsOfServiceAgreed": true}'
3228 if [ "$ACCOUNT_EMAIL" ]; then
3229 regjson='{"contact": ["mailto: '$ACCOUNT_EMAIL'"], "termsOfServiceAgreed": true}'
3230 fi
3231 else
3232 _reg_res="$ACME_NEW_ACCOUNT_RES"
3233 regjson='{"resource": "'$_reg_res'", "terms-of-service-agreed": true, "agreement": "'$ACME_AGREEMENT'"}'
3234 if [ "$ACCOUNT_EMAIL" ]; then
3235 regjson='{"resource": "'$_reg_res'", "contact": ["mailto: '$ACCOUNT_EMAIL'"], "terms-of-service-agreed": true, "agreement": "'$ACME_AGREEMENT'"}'
3236 fi
f87890cb 3237 fi
4c2a3841 3238
ca7ebd93 3239 _info "Registering account"
d404e92d 3240
f87890cb 3241 if ! _send_signed_request "${ACME_NEW_ACCOUNT}" "$regjson"; then
3242 _err "Register account Error: $response"
3243 return 1
3244 fi
d404e92d 3245
f87890cb 3246 if [ "$code" = "" ] || [ "$code" = '201' ]; then
3247 echo "$response" >"$ACCOUNT_JSON_PATH"
3248 _info "Registered"
7df20e50 3249 elif [ "$code" = '409' ] || [ "$code" = '200' ]; then
f87890cb 3250 _info "Already registered"
3251 else
3252 _err "Register account Error: $response"
3253 return 1
3254 fi
d404e92d 3255
5d8d217a 3256 _debug2 responseHeaders "$responseHeaders"
f87890cb 3257 _accUri="$(echo "$responseHeaders" | grep "^Location:" | _head_n 1 | cut -d ' ' -f 2 | tr -d "\r\n")"
3258 _debug "_accUri" "$_accUri"
7e0b334b 3259 if [ -z "$_accUri" ]; then
3260 _err "Can not find account id url."
3261 _err "$responseHeaders"
3262 return 1
3263 fi
f87890cb 3264 _savecaconf "ACCOUNT_URL" "$_accUri"
7e0b334b 3265 export ACCOUNT_URL="$_accUri"
d404e92d 3266
f87890cb 3267 CA_KEY_HASH="$(__calcAccountKeyHash)"
3268 _debug "Calc CA_KEY_HASH" "$CA_KEY_HASH"
3269 _savecaconf CA_KEY_HASH "$CA_KEY_HASH"
d404e92d 3270
f87890cb 3271 if [ "$code" = '403' ]; then
3272 _err "It seems that the account key is already deactivated, please use a new account key."
3273 return 1
3274 fi
4c2a3841 3275
f87890cb 3276 ACCOUNT_THUMBPRINT="$(__calc_account_thumbprint)"
3277 _info "ACCOUNT_THUMBPRINT" "$ACCOUNT_THUMBPRINT"
d404e92d 3278}
3279
422dd1fa 3280#Implement deactivate account
3281deactivateaccount() {
3282 _initpath
3283
3284 if [ ! -f "$ACCOUNT_KEY_PATH" ] && [ -f "$_OLD_ACCOUNT_KEY" ]; then
422dd1fa 3285 _info "mv $_OLD_ACCOUNT_KEY to $ACCOUNT_KEY_PATH"
3286 mv "$_OLD_ACCOUNT_KEY" "$ACCOUNT_KEY_PATH"
3287 fi
3288
3289 if [ ! -f "$ACCOUNT_JSON_PATH" ] && [ -f "$_OLD_ACCOUNT_JSON" ]; then
422dd1fa 3290 _info "mv $_OLD_ACCOUNT_JSON to $ACCOUNT_JSON_PATH"
3291 mv "$_OLD_ACCOUNT_JSON" "$ACCOUNT_JSON_PATH"
3292 fi
3293
3294 if [ ! -f "$ACCOUNT_KEY_PATH" ]; then
3295 _err "Account key is not found at: $ACCOUNT_KEY_PATH"
3296 return 1
3297 fi
3298
3299 _accUri=$(_readcaconf "ACCOUNT_URL")
3300 _debug _accUri "$_accUri"
3301
3302 if [ -z "$_accUri" ]; then
3303 _err "The account url is empty, please run '--update-account' first to update the account info first,"
3304 _err "Then try again."
3305 return 1
3306 fi
3307
3308 if ! _calcjwk "$ACCOUNT_KEY_PATH"; then
3309 return 1
3310 fi
3311 _initAPI
3312
d2cde379 3313 if [ "$ACME_VERSION" = "2" ]; then
3314 _djson="{\"status\":\"deactivated\"}"
3315 else
3316 _djson="{\"resource\": \"reg\", \"status\":\"deactivated\"}"
3317 fi
3318 if _send_signed_request "$_accUri" "$_djson" && _contains "$response" '"deactivated"'; then
422dd1fa 3319 _info "Deactivate account success for $_accUri."
3320 _accid=$(echo "$response" | _egrep_o "\"id\" *: *[^,]*," | cut -d : -f 2 | tr -d ' ,')
3321 elif [ "$code" = "403" ]; then
3322 _info "The account is already deactivated."
3323 _accid=$(_getfield "$_accUri" "999" "/")
3324 else
3325 _err "Deactivate: account failed for $_accUri."
3326 return 1
3327 fi
3328
3329 _debug "Account id: $_accid"
3330 if [ "$_accid" ]; then
3331 _deactivated_account_path="$CA_DIR/deactivated/$_accid"
3332 _debug _deactivated_account_path "$_deactivated_account_path"
3333 if mkdir -p "$_deactivated_account_path"; then
3334 _info "Moving deactivated account info to $_deactivated_account_path/"
3335 mv "$CA_CONF" "$_deactivated_account_path/"
3336 mv "$ACCOUNT_JSON_PATH" "$_deactivated_account_path/"
3337 mv "$ACCOUNT_KEY_PATH" "$_deactivated_account_path/"
3338 else
3339 _err "Can not create dir: $_deactivated_account_path, try to remove the deactivated account key."
3340 rm -f "$CA_CONF"
3341 rm -f "$ACCOUNT_JSON_PATH"
3342 rm -f "$ACCOUNT_KEY_PATH"
3343 fi
3344 fi
3345}
3346
a61fe418 3347# domain folder file
3348_findHook() {
3349 _hookdomain="$1"
3350 _hookcat="$2"
3351 _hookname="$3"
3352
c7b16249 3353 if [ -f "$_SCRIPT_HOME/$_hookcat/$_hookname" ]; then
3354 d_api="$_SCRIPT_HOME/$_hookcat/$_hookname"
3355 elif [ -f "$_SCRIPT_HOME/$_hookcat/$_hookname.sh" ]; then
3356 d_api="$_SCRIPT_HOME/$_hookcat/$_hookname.sh"
4c2a3841 3357 elif [ -f "$LE_WORKING_DIR/$_hookdomain/$_hookname" ]; then
a61fe418 3358 d_api="$LE_WORKING_DIR/$_hookdomain/$_hookname"
4c2a3841 3359 elif [ -f "$LE_WORKING_DIR/$_hookdomain/$_hookname.sh" ]; then
a61fe418 3360 d_api="$LE_WORKING_DIR/$_hookdomain/$_hookname.sh"
4c2a3841 3361 elif [ -f "$LE_WORKING_DIR/$_hookname" ]; then
a61fe418 3362 d_api="$LE_WORKING_DIR/$_hookname"
4c2a3841 3363 elif [ -f "$LE_WORKING_DIR/$_hookname.sh" ]; then
a61fe418 3364 d_api="$LE_WORKING_DIR/$_hookname.sh"
4c2a3841 3365 elif [ -f "$LE_WORKING_DIR/$_hookcat/$_hookname" ]; then
a61fe418 3366 d_api="$LE_WORKING_DIR/$_hookcat/$_hookname"
4c2a3841 3367 elif [ -f "$LE_WORKING_DIR/$_hookcat/$_hookname.sh" ]; then
a61fe418 3368 d_api="$LE_WORKING_DIR/$_hookcat/$_hookname.sh"
3369 fi
3370
3371 printf "%s" "$d_api"
3372}
3373
f940b2a5 3374#domain
3375__get_domain_new_authz() {
3376 _gdnd="$1"
3377 _info "Getting new-authz for domain" "$_gdnd"
40ef86f4 3378 _initAPI
f940b2a5 3379 _Max_new_authz_retry_times=5
3380 _authz_i=0
4c2a3841 3381 while [ "$_authz_i" -lt "$_Max_new_authz_retry_times" ]; do
efd96153 3382 _debug "Try new-authz for the $_authz_i time."
48d9a8c1 3383 if ! _send_signed_request "${ACME_NEW_AUTHZ}" "{\"resource\": \"new-authz\", \"identifier\": {\"type\": \"dns\", \"value\": \"$(_idn "$_gdnd")\"}}"; then
f940b2a5 3384 _err "Can not get domain new authz."
3385 return 1
3386 fi
5413bf87 3387 if _contains "$response" "No registration exists matching provided key"; then
3388 _err "It seems there is an error, but it's recovered now, please try again."
3389 _err "If you see this message for a second time, please report bug: $(__green "$PROJECT")"
3390 _clearcaconf "CA_KEY_HASH"
3391 break
3392 fi
4c2a3841 3393 if ! _contains "$response" "An error occurred while processing your request"; then
f940b2a5 3394 _info "The new-authz request is ok."
3395 break
3396 fi
3397 _authz_i="$(_math "$_authz_i" + 1)"
9e45ac93 3398 _info "The server is busy, Sleep $_authz_i to retry."
f940b2a5 3399 _sleep "$_authz_i"
4c2a3841 3400 done
f940b2a5 3401
4c2a3841 3402 if [ "$_authz_i" = "$_Max_new_authz_retry_times" ]; then
efd96153 3403 _err "new-authz retry reach the max $_Max_new_authz_retry_times times."
f940b2a5 3404 fi
4c2a3841 3405
78915896 3406 if [ "$code" ] && [ "$code" != '201' ]; then
f940b2a5 3407 _err "new-authz error: $response"
3408 return 1
3409 fi
3410
3411}
3412
58e4d337 3413#uri keyAuthorization
f94433e5 3414__trigger_validation() {
58e4d337 3415 _debug2 "tigger domain validation."
3416 _t_url="$1"
3417 _debug2 _t_url "$_t_url"
3418 _t_key_authz="$2"
3419 _debug2 _t_key_authz "$_t_key_authz"
c1151b0d 3420 if [ "$ACME_VERSION" = "2" ]; then
3421 _send_signed_request "$_t_url" "{\"keyAuthorization\": \"$_t_key_authz\"}"
3422 else
3423 _send_signed_request "$_t_url" "{\"resource\": \"challenge\", \"keyAuthorization\": \"$_t_key_authz\"}"
3424 fi
58e4d337 3425}
3426
3c07f57a 3427#webroot, domain domainlist keylength
4c3b3608 3428issue() {
4c2a3841 3429 if [ -z "$2" ]; then
43822d37 3430 _usage "Usage: $PROJECT_ENTRY --issue -d a.com -w /path/to/webroot/a.com/ "
4c3b3608 3431 return 1
3432 fi
49d75a0c 3433 if [ -z "$1" ]; then
3434 _usage "Please specify at least one validation method: '--webroot', '--standalone', '--apache', '--nginx' or '--dns' etc."
3435 return 1
3436 fi
af1cc3b3 3437 _web_roots="$1"
3438 _main_domain="$2"
02140ce7 3439 _alt_domains="$3"
d2cde379 3440
af1cc3b3 3441 if _contains "$_main_domain" ","; then
3442 _main_domain=$(echo "$2,$3" | cut -d , -f 1)
02140ce7 3443 _alt_domains=$(echo "$2,$3" | cut -d , -f 2- | sed "s/,${NO_VALUE}$//")
2aff36e7 3444 fi
674b5088 3445 _debug _main_domain "$_main_domain"
3446 _debug _alt_domains "$_alt_domains"
3447
d9c9114b 3448 _key_length="$4"
85e1f4ea 3449 _real_cert="$5"
3450 _real_key="$6"
3451 _real_ca="$7"
3452 _reload_cmd="$8"
3453 _real_fullchain="$9"
3454 _pre_hook="${10}"
3455 _post_hook="${11}"
3456 _renew_hook="${12}"
3457 _local_addr="${13}"
875625b1 3458 _challenge_alias="${14}"
eccec5f6 3459 #remove these later.
af1cc3b3 3460 if [ "$_web_roots" = "dns-cf" ]; then
3461 _web_roots="dns_cf"
eccec5f6 3462 fi
af1cc3b3 3463 if [ "$_web_roots" = "dns-dp" ]; then
3464 _web_roots="dns_dp"
eccec5f6 3465 fi
af1cc3b3 3466 if [ "$_web_roots" = "dns-cx" ]; then
3467 _web_roots="dns_cx"
eccec5f6 3468 fi
4c2a3841 3469
3470 if [ ! "$IS_RENEW" ]; then
d9c9114b 3471 _initpath "$_main_domain" "$_key_length"
43822d37 3472 mkdir -p "$DOMAIN_PATH"
3473 fi
eccec5f6 3474
48d9a8c1 3475 _debug "Using ACME_DIRECTORY: $ACME_DIRECTORY"
3476
3477 _initAPI
3478
4c2a3841 3479 if [ -f "$DOMAIN_CONF" ]; then
61623d22 3480 Le_NextRenewTime=$(_readdomainconf Le_NextRenewTime)
a4270efa 3481 _debug Le_NextRenewTime "$Le_NextRenewTime"
95e06de5 3482 if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(_time)" -lt "$Le_NextRenewTime" ]; then
bb25febd 3483 _saved_domain=$(_readdomainconf Le_Domain)
3484 _debug _saved_domain "$_saved_domain"
3485 _saved_alt=$(_readdomainconf Le_Alt)
3486 _debug _saved_alt "$_saved_alt"
02140ce7 3487 if [ "$_saved_domain,$_saved_alt" = "$_main_domain,$_alt_domains" ]; then
bb25febd 3488 _info "Domains not changed."
3489 _info "Skip, Next renewal time is: $(__green "$(_readdomainconf Le_NextRenewTimeStr)")"
4c2a3841 3490 _info "Add '$(__red '--force')' to force to renew."
bb25febd 3491 return $RENEW_SKIP
3492 else
3493 _info "Domains have changed."
3494 fi
4c3b3608 3495 fi
3496 fi
96a46cfc 3497
af1cc3b3 3498 _savedomainconf "Le_Domain" "$_main_domain"
02140ce7 3499 _savedomainconf "Le_Alt" "$_alt_domains"
af1cc3b3 3500 _savedomainconf "Le_Webroot" "$_web_roots"
4c2a3841 3501
85e1f4ea 3502 _savedomainconf "Le_PreHook" "$_pre_hook"
3503 _savedomainconf "Le_PostHook" "$_post_hook"
3504 _savedomainconf "Le_RenewHook" "$_renew_hook"
4c2a3841 3505
85e1f4ea 3506 if [ "$_local_addr" ]; then
3507 _savedomainconf "Le_LocalAddress" "$_local_addr"
72518d48 3508 else
3509 _cleardomainconf "Le_LocalAddress"
3510 fi
875625b1 3511 if [ "$_challenge_alias" ]; then
3512 _savedomainconf "Le_ChallengeAlias" "$_challenge_alias"
3513 else
3514 _cleardomainconf "Le_ChallengeAlias"
3515 fi
6ae0f7f5 3516
48d9a8c1 3517 Le_API="$ACME_DIRECTORY"
f6dcd989 3518 _savedomainconf "Le_API" "$Le_API"
4c2a3841 3519
02140ce7 3520 if [ "$_alt_domains" = "$NO_VALUE" ]; then
3521 _alt_domains=""
4c3b3608 3522 fi
4c2a3841 3523
d9c9114b 3524 if [ "$_key_length" = "$NO_VALUE" ]; then
3525 _key_length=""
d404e92d 3526 fi
4c2a3841 3527
85e1f4ea 3528 if ! _on_before_issue "$_web_roots" "$_main_domain" "$_alt_domains" "$_pre_hook" "$_local_addr"; then
0463b5d6 3529 _err "_on_before_issue."
3530 return 1
4c3b3608 3531 fi
0463b5d6 3532
8a29fbc8 3533 _saved_account_key_hash="$(_readcaconf "CA_KEY_HASH")"
3534 _debug2 _saved_account_key_hash "$_saved_account_key_hash"
4c2a3841 3535
3536 if [ -z "$_saved_account_key_hash" ] || [ "$_saved_account_key_hash" != "$(__calcAccountKeyHash)" ]; then
57e58ce7 3537 if ! _regAccount "$_accountkeylength"; then
85e1f4ea 3538 _on_issue_err "$_post_hook"
8a29fbc8 3539 return 1
3540 fi
57e58ce7 3541 else
3542 _debug "_saved_account_key_hash is not changed, skip register account."
166096dc 3543 fi
166096dc 3544
4c2a3841 3545 if [ -f "$CSR_PATH" ] && [ ! -f "$CERT_KEY_PATH" ]; then
10afcaca 3546 _info "Signing from existing CSR."
3547 else
3548 _key=$(_readdomainconf Le_Keylength)
3549 _debug "Read key length:$_key"
c4b2e582 3550 if [ ! -f "$CERT_KEY_PATH" ] || [ "$_key_length" != "$_key" ] || [ "$Le_ForceNewDomainKey" = "1" ]; then
d9c9114b 3551 if ! createDomainKey "$_main_domain" "$_key_length"; then
10afcaca 3552 _err "Create domain key error."
3553 _clearup
85e1f4ea 3554 _on_issue_err "$_post_hook"
10afcaca 3555 return 1
3556 fi
3557 fi
3558
02140ce7 3559 if ! _createcsr "$_main_domain" "$_alt_domains" "$CERT_KEY_PATH" "$CSR_PATH" "$DOMAIN_SSL_CONF"; then
10afcaca 3560 _err "Create CSR error."
5ef501c5 3561 _clearup
85e1f4ea 3562 _on_issue_err "$_post_hook"
41e3eafa 3563 return 1
3564 fi
4c3b3608 3565 fi
10afcaca 3566
d9c9114b 3567 _savedomainconf "Le_Keylength" "$_key_length"
4c2a3841 3568
4c3b3608 3569 vlist="$Le_Vlist"
cae203be 3570
3571 _info "Getting domain auth token for each domain"
4c3b3608 3572 sep='#'
9d725af6 3573 dvsep=','
4c2a3841 3574 if [ -z "$vlist" ]; then
d2cde379 3575 if [ "$ACME_VERSION" = "2" ]; then
c1151b0d 3576 #make new order request
3577 _identifiers="{\"type\":\"dns\",\"value\":\"$_main_domain\"}"
38f1b4d2 3578 _w_index=1
674b5088 3579 while true; do
dd17124e 3580 d="$(echo "$_alt_domains," | cut -d , -f "$_w_index")"
674b5088 3581 _w_index="$(_math "$_w_index" + 1)"
3582 _debug d "$d"
3583 if [ -z "$d" ]; then
3584 break
c1151b0d 3585 fi
674b5088 3586 _identifiers="$_identifiers,{\"type\":\"dns\",\"value\":\"$d\"}"
c1151b0d 3587 done
3588 _debug2 _identifiers "$_identifiers"
3589 if ! _send_signed_request "$ACME_NEW_ORDER" "{\"identifiers\": [$_identifiers]}"; then
3590 _err "Create new order error."
3591 _clearup
3592 _on_issue_err "$_post_hook"
3593 return 1
3594 fi
3595
d2cde379 3596 Le_OrderFinalize="$(echo "$response" | tr -d '\r\n' | _egrep_o '"finalize" *: *"[^"]*"' | cut -d '"' -f 4)"
3597 _debug Le_OrderFinalize "$Le_OrderFinalize"
3598 if [ -z "$Le_OrderFinalize" ]; then
78915896 3599 _err "Create new order error. Le_OrderFinalize not found. $response"
c1151b0d 3600 _clearup
3601 _on_issue_err "$_post_hook"
3602 return 1
3603 fi
3604
3605 #for dns manual mode
d2cde379 3606 _savedomainconf "Le_OrderFinalize" "$Le_OrderFinalize"
c1151b0d 3607
f8d22c48 3608 _authorizations_seg="$(echo "$response" | tr -d '\r\n' | _egrep_o '"authorizations" *: *\[[^\]*\]' | cut -d '[' -f 2 | tr -d ']' | tr -d '"')"
c1151b0d 3609 _debug2 _authorizations_seg "$_authorizations_seg"
3610 if [ -z "$_authorizations_seg" ]; then
3611 _err "_authorizations_seg not found."
3612 _clearup
3613 _on_issue_err "$_post_hook"
3614 return 1
3615 fi
3616
3617 #domain and authz map
3618 _authorizations_map=""
f8d22c48 3619 for _authz_url in $(echo "$_authorizations_seg" | tr ',' ' '); do
c1151b0d 3620 _debug2 "_authz_url" "$_authz_url"
3621 if ! response="$(_get "$_authz_url")"; then
3622 _err "get to authz error."
263c38ca 3623 _err "_authorizations_seg" "$_authorizations_seg"
3624 _err "_authz_url" "$_authz_url"
c1151b0d 3625 _clearup
3626 _on_issue_err "$_post_hook"
3627 return 1
3628 fi
3629
3630 response="$(echo "$response" | _normalizeJson)"
3631 _debug2 response "$response"
3632 _d="$(echo "$response" | _egrep_o '"value" *: *"[^"]*"' | cut -d : -f 2 | tr -d ' "')"
72f54ca6 3633 if _contains "$response" "\"wildcard\" *: *true"; then
3634 _d="*.$_d"
3635 fi
c1151b0d 3636 _debug2 _d "$_d"
3637 _authorizations_map="$_d,$response
3638$_authorizations_map"
3639 done
3640 _debug2 _authorizations_map "$_authorizations_map"
3641 fi
3642
c1151b0d 3643 _index=0
a63b05a9 3644 _currentRoot=""
38f1b4d2 3645 _w_index=1
88bbe55b 3646 while true; do
88bbe55b 3647 d="$(echo "$_main_domain,$_alt_domains," | cut -d , -f "$_w_index")"
3648 _w_index="$(_math "$_w_index" + 1)"
3649 _debug d "$d"
3650 if [ -z "$d" ]; then
3651 break
3652 fi
ca7202eb 3653 _info "Getting webroot for domain" "$d"
c1151b0d 3654 _index=$(_math $_index + 1)
af1cc3b3 3655 _w="$(echo $_web_roots | cut -d , -f $_index)"
9d725af6 3656 _debug _w "$_w"
4c2a3841 3657 if [ "$_w" ]; then
a63b05a9 3658 _currentRoot="$_w"
3659 fi
3660 _debug "_currentRoot" "$_currentRoot"
4c2a3841 3661
a63b05a9 3662 vtype="$VTYPE_HTTP"
c1151b0d 3663 #todo, v2 wildcard force to use dns
3881f221 3664 if _startswith "$_currentRoot" "$W_DNS"; then
a63b05a9 3665 vtype="$VTYPE_DNS"
3666 fi
4c2a3841 3667
3668 if [ "$_currentRoot" = "$W_TLS" ]; then
c1151b0d 3669 if [ "$ACME_VERSION" = "2" ]; then
3670 vtype="$VTYPE_TLS2"
3671 else
3672 vtype="$VTYPE_TLS"
3673 fi
e22bcf7c 3674 fi
c4d8fd83 3675
c1151b0d 3676 if [ "$ACME_VERSION" = "2" ]; then
3677 response="$(echo "$_authorizations_map" | grep "^$d," | sed "s/$d,//")"
3678 _debug2 "response" "$response"
3679 if [ -z "$response" ]; then
3680 _err "get to authz error."
263c38ca 3681 _err "_authorizations_map" "$_authorizations_map"
c1151b0d 3682 _clearup
3683 _on_issue_err "$_post_hook"
3684 return 1
3685 fi
3686 else
3687 if ! __get_domain_new_authz "$d"; then
3688 _clearup
3689 _on_issue_err "$_post_hook"
3690 return 1
3691 fi
c4d8fd83 3692 fi
3693
4c2a3841 3694 if [ -z "$thumbprint" ]; then
339a8ad6 3695 thumbprint="$(__calc_account_thumbprint)"
4c3b3608 3696 fi
3697
4c2a3841 3698 entry="$(printf "%s\n" "$response" | _egrep_o '[^\{]*"type":"'$vtype'"[^\}]*')"
4c3b3608 3699 _debug entry "$entry"
4c2a3841 3700 if [ -z "$entry" ]; then
584fb290 3701 _err "Error, can not get domain token entry $d"
a63766a0 3702 _supported_vtypes="$(echo "$response" | _egrep_o "\"challenges\":\[[^]]*]" | tr '{' "\n" | grep type | cut -d '"' -f 4 | tr "\n" ' ')"
b51ed9bb 3703 if [ "$_supported_vtypes" ]; then
3704 _err "The supported validation types are: $_supported_vtypes, but you specified: $vtype"
3705 fi
19539575 3706 _clearup
85e1f4ea 3707 _on_issue_err "$_post_hook"
19539575 3708 return 1
3709 fi
22ea4004 3710 token="$(printf "%s\n" "$entry" | _egrep_o '"token":"[^"]*' | cut -d : -f 2 | tr -d '"')"
ca7202eb 3711 _debug token "$token"
4c2a3841 3712
584fb290 3713 if [ -z "$token" ]; then
3714 _err "Error, can not get domain token $entry"
3715 _clearup
3716 _on_issue_err "$_post_hook"
3717 return 1
3718 fi
c1151b0d 3719 if [ "$ACME_VERSION" = "2" ]; then
3720 uri="$(printf "%s\n" "$entry" | _egrep_o '"url":"[^"]*' | cut -d '"' -f 4 | _head_n 1)"
3721 else
3722 uri="$(printf "%s\n" "$entry" | _egrep_o '"uri":"[^"]*' | cut -d '"' -f 4)"
3723 fi
ca7202eb 3724 _debug uri "$uri"
cae203be 3725
584fb290 3726 if [ -z "$uri" ]; then
3727 _err "Error, can not get domain uri. $entry"
3728 _clearup
3729 _on_issue_err "$_post_hook"
3730 return 1
3731 fi
4c3b3608 3732 keyauthorization="$token.$thumbprint"
3733 _debug keyauthorization "$keyauthorization"
3734
95e06de5 3735 if printf "%s" "$response" | grep '"status":"valid"' >/dev/null 2>&1; then
c1151b0d 3736 _debug "$d is already verified."
ca7202eb 3737 keyauthorization="$STATE_VERIFIED"
d35bf517 3738 _debug keyauthorization "$keyauthorization"
ec603bee 3739 fi
3740
a63b05a9 3741 dvlist="$d$sep$keyauthorization$sep$uri$sep$vtype$sep$_currentRoot"
4c3b3608 3742 _debug dvlist "$dvlist"
4c2a3841 3743
9d725af6 3744 vlist="$vlist$dvlist$dvsep"
4c3b3608 3745
3746 done
9d725af6 3747 _debug vlist "$vlist"
4c3b3608 3748 #add entry
3749 dnsadded=""
9d725af6 3750 ventries=$(echo "$vlist" | tr "$dvsep" ' ')
1f7df33e 3751 _alias_index=1
4c2a3841 3752 for ventry in $ventries; do
ca7202eb 3753 d=$(echo "$ventry" | cut -d "$sep" -f 1)
3754 keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2)
3755 vtype=$(echo "$ventry" | cut -d "$sep" -f 4)
3756 _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5)
72f54ca6 3757 _debug d "$d"
4c2a3841 3758 if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
69212114 3759 _debug "$d is already verified, skip $vtype."
ec603bee 3760 continue
3761 fi
3762
4c2a3841 3763 if [ "$vtype" = "$VTYPE_DNS" ]; then
4c3b3608 3764 dnsadded='0'
72f54ca6 3765 _dns_root_d="$d"
3766 if _startswith "$_dns_root_d" "*."; then
3767 _dns_root_d="$(echo "$_dns_root_d" | sed 's/*.//')"
3768 fi
875625b1 3769 _d_alias="$(_getfield "$_challenge_alias" "$_alias_index")"
3770 _alias_index="$(_math "$_alias_index" + 1)"
3771 _debug "_d_alias" "$_d_alias"
3772 if [ "$_d_alias" ]; then
64821ad4 3773 if _startswith "$_d_alias" "$DNS_ALIAS_PREFIX"; then
3774 txtdomain="$(echo "$_d_alias" | sed "s/$DNS_ALIAS_PREFIX//")"
3775 else
3776 txtdomain="_acme-challenge.$_d_alias"
3777 fi
875625b1 3778 else
3779 txtdomain="_acme-challenge.$_dns_root_d"
3780 fi
4c3b3608 3781 _debug txtdomain "$txtdomain"
11927a76 3782 txt="$(printf "%s" "$keyauthorization" | _digest "sha256" | _url_replace)"
4c3b3608 3783 _debug txt "$txt"
a61fe418 3784
72f54ca6 3785 d_api="$(_findHook "$_dns_root_d" dnsapi "$_currentRoot")"
a61fe418 3786
4c3b3608 3787 _debug d_api "$d_api"
4c2a3841 3788
3789 if [ "$d_api" ]; then
4c3b3608 3790 _info "Found domain api file: $d_api"
3791 else
3881f221 3792 if [ "$_currentRoot" != "$W_DNS" ]; then
3793 _err "Can not find dns api hook for: $_currentRoot"
3794 _info "You need to add the txt record manually."
3795 fi
5f8b60a0 3796 _info "$(__red "Add the following TXT record:")"
81772fb7 3797 _info "$(__red "Domain: '$(__green "$txtdomain")'")"
3798 _info "$(__red "TXT value: '$(__green "$txt")'")"
3799 _info "$(__red "Please be aware that you prepend _acme-challenge. before your domain")"
3800 _info "$(__red "so the resulting subdomain will be: $txtdomain")"
4c3b3608 3801 continue
3802 fi
4c2a3841 3803
73b8b120 3804 (
ca7202eb 3805 if ! . "$d_api"; then
73b8b120 3806 _err "Load file $d_api error. Please check your api file and try again."
3807 return 1
3808 fi
4c2a3841 3809
158f22f7 3810 addcommand="${_currentRoot}_add"
ca7202eb 3811 if ! _exists "$addcommand"; then
73b8b120 3812 _err "It seems that your api file is not correct, it must have a function named: $addcommand"
3813 return 1
3814 fi
4c2a3841 3815
ca7202eb 3816 if ! $addcommand "$txtdomain" "$txt"; then
73b8b120 3817 _err "Error add txt for domain:$txtdomain"
3818 return 1
3819 fi
3820 )
4c2a3841 3821
3822 if [ "$?" != "0" ]; then
5ef501c5 3823 _clearup
ea722da3 3824 _on_issue_err "$_post_hook" "$vlist"
4c3b3608 3825 return 1
3826 fi
3827 dnsadded='1'
3828 fi
3829 done
3830
4c2a3841 3831 if [ "$dnsadded" = '0' ]; then
3832 _savedomainconf "Le_Vlist" "$vlist"
4c3b3608 3833 _debug "Dns record not added yet, so, save to $DOMAIN_CONF and exit."
a8b62261 3834 _err "Please add the TXT records to the domains, and re-run with --renew."
5ef501c5 3835 _clearup
5f8b60a0 3836 _on_issue_err "$_post_hook"
4c3b3608 3837 return 1
3838 fi
4c2a3841 3839
4c3b3608 3840 fi
4c2a3841 3841
3842 if [ "$dnsadded" = '1' ]; then
3843 if [ -z "$Le_DNSSleep" ]; then
ca7202eb 3844 Le_DNSSleep="$DEFAULT_DNS_SLEEP"
0e38c60d 3845 else
4c2a3841 3846 _savedomainconf "Le_DNSSleep" "$Le_DNSSleep"
0e38c60d 3847 fi
3848
5fbc47eb 3849 _info "Sleep $(__green $Le_DNSSleep) seconds for the txt records to take effect"
ca7202eb 3850 _sleep "$Le_DNSSleep"
4c3b3608 3851 fi
4c2a3841 3852
5d943a35 3853 NGINX_RESTORE_VLIST=""
4c3b3608 3854 _debug "ok, let's start to verify"
a63b05a9 3855
0463b5d6 3856 _ncIndex=1
9d725af6 3857 ventries=$(echo "$vlist" | tr "$dvsep" ' ')
4c2a3841 3858 for ventry in $ventries; do
ca7202eb 3859 d=$(echo "$ventry" | cut -d "$sep" -f 1)
3860 keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2)
3861 uri=$(echo "$ventry" | cut -d "$sep" -f 3)
3862 vtype=$(echo "$ventry" | cut -d "$sep" -f 4)
3863 _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5)
ec603bee 3864
4c2a3841 3865 if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
ec603bee 3866 _info "$d is already verified, skip $vtype."
3867 continue
3868 fi
3869
4c3b3608 3870 _info "Verifying:$d"
3871 _debug "d" "$d"
3872 _debug "keyauthorization" "$keyauthorization"
3873 _debug "uri" "$uri"
3874 removelevel=""
e22bcf7c 3875 token="$(printf "%s" "$keyauthorization" | cut -d '.' -f 1)"
a63b05a9 3876
3877 _debug "_currentRoot" "$_currentRoot"
3878
4c2a3841 3879 if [ "$vtype" = "$VTYPE_HTTP" ]; then
3880 if [ "$_currentRoot" = "$NO_VALUE" ]; then
4c3b3608 3881 _info "Standalone mode server"
85e1f4ea 3882 _ncaddr="$(_getfield "$_local_addr" "$_ncIndex")"
0463b5d6 3883 _ncIndex="$(_math $_ncIndex + 1)"
3794b5cb 3884 _startserver "$keyauthorization" "$_ncaddr"
4c2a3841 3885 if [ "$?" != "0" ]; then
5ef501c5 3886 _clearup
58e4d337 3887 _on_issue_err "$_post_hook" "$vlist"
6fc1447f 3888 return 1
3889 fi
5dbf664a 3890 sleep 1
ca7202eb 3891 _debug serverproc "$serverproc"
0e44f587 3892 elif [ "$_currentRoot" = "$MODE_STATELESS" ]; then
3893 _info "Stateless mode for domain:$d"
3894 _sleep 1
9d725af6 3895 elif _startswith "$_currentRoot" "$NGINX"; then
3896 _info "Nginx mode for domain:$d"
3897 #set up nginx server
3898 FOUND_REAL_NGINX_CONF=""
3899 BACKUP_NGINX_CONF=""
3900 if ! _setNginx "$d" "$_currentRoot" "$thumbprint"; then
3901 _clearup
58e4d337 3902 _on_issue_err "$_post_hook" "$vlist"
9d725af6 3903 return 1
03f8d6e9 3904 fi
302c41ed 3905
03f8d6e9 3906 if [ "$FOUND_REAL_NGINX_CONF" ]; then
9d725af6 3907 _realConf="$FOUND_REAL_NGINX_CONF"
3908 _backup="$BACKUP_NGINX_CONF"
3909 _debug _realConf "$_realConf"
5d943a35 3910 NGINX_RESTORE_VLIST="$d$sep$_realConf$sep$_backup$dvsep$NGINX_RESTORE_VLIST"
9d725af6 3911 fi
3912 _sleep 1
4c3b3608 3913 else
4c2a3841 3914 if [ "$_currentRoot" = "apache" ]; then
6f930641 3915 wellknown_path="$ACME_DIR"
3916 else
a63b05a9 3917 wellknown_path="$_currentRoot/.well-known/acme-challenge"
4c2a3841 3918 if [ ! -d "$_currentRoot/.well-known" ]; then
6f930641 3919 removelevel='1'
4c2a3841 3920 elif [ ! -d "$_currentRoot/.well-known/acme-challenge" ]; then
6f930641 3921 removelevel='2'
3922 else
3923 removelevel='3'
3924 fi
4c3b3608 3925 fi
6f930641 3926
4c3b3608 3927 _debug wellknown_path "$wellknown_path"
6f930641 3928
4c3b3608 3929 _debug "writing token:$token to $wellknown_path/$token"
3930
3931 mkdir -p "$wellknown_path"
93fc48a2 3932
4c2a3841 3933 if ! printf "%s" "$keyauthorization" >"$wellknown_path/$token"; then
93fc48a2 3934 _err "$d:Can not write token to file : $wellknown_path/$token"
3935 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
3936 _clearup
58e4d337 3937 _on_issue_err "$_post_hook" "$vlist"
93fc48a2 3938 return 1
3939 fi
3940
4c2a3841 3941 if [ ! "$usingApache" ]; then
44edb2bd 3942 if webroot_owner=$(_stat "$_currentRoot"); then
32fdc196 3943 _debug "Changing owner/group of .well-known to $webroot_owner"
c87cd0de 3944 if ! _exec "chown -R \"$webroot_owner\" \"$_currentRoot/.well-known\""; then
3945 _debug "$(cat "$_EXEC_TEMP_ERR")"
3946 _exec_err >/dev/null 2>&1
3947 fi
32fdc196 3948 else
b54ce310 3949 _debug "not changing owner/group of webroot"
32fdc196 3950 fi
df886ffa 3951 fi
4c2a3841 3952
4c3b3608 3953 fi
4c2a3841 3954
3955 elif [ "$vtype" = "$VTYPE_TLS" ]; then
e22bcf7c 3956 #create A
3957 #_hash_A="$(printf "%s" $token | _digest "sha256" "hex" )"
3958 #_debug2 _hash_A "$_hash_A"
3959 #_x="$(echo $_hash_A | cut -c 1-32)"
3960 #_debug2 _x "$_x"
3961 #_y="$(echo $_hash_A | cut -c 33-64)"
3962 #_debug2 _y "$_y"
3963 #_SAN_A="$_x.$_y.token.acme.invalid"
3964 #_debug2 _SAN_A "$_SAN_A"
4c2a3841 3965
e22bcf7c 3966 #create B
0c538f75 3967 _hash_B="$(printf "%s" "$keyauthorization" | _digest "sha256" "hex")"
e22bcf7c 3968 _debug2 _hash_B "$_hash_B"
0c538f75 3969 _x="$(echo "$_hash_B" | cut -c 1-32)"
e22bcf7c 3970 _debug2 _x "$_x"
0c538f75 3971 _y="$(echo "$_hash_B" | cut -c 33-64)"
e22bcf7c 3972 _debug2 _y "$_y"
4c2a3841 3973
e22bcf7c 3974 #_SAN_B="$_x.$_y.ka.acme.invalid"
4c2a3841 3975
e22bcf7c 3976 _SAN_B="$_x.$_y.acme.invalid"
3977 _debug2 _SAN_B "$_SAN_B"
4c2a3841 3978
85e1f4ea 3979 _ncaddr="$(_getfield "$_local_addr" "$_ncIndex")"
0c538f75 3980 _ncIndex="$(_math "$_ncIndex" + 1)"
0463b5d6 3981 if ! _starttlsserver "$_SAN_B" "$_SAN_A" "$Le_TLSPort" "$keyauthorization" "$_ncaddr"; then
e22bcf7c 3982 _err "Start tls server error."
3983 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
3984 _clearup
58e4d337 3985 _on_issue_err "$_post_hook" "$vlist"
e22bcf7c 3986 return 1
3987 fi
4c3b3608 3988 fi
4c2a3841 3989
f94433e5 3990 if ! __trigger_validation "$uri" "$keyauthorization"; then
c4d8fd83 3991 _err "$d:Can not get challenge: $response"
3992 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
3993 _clearup
58e4d337 3994 _on_issue_err "$_post_hook" "$vlist"
c4d8fd83 3995 return 1
3996 fi
4c2a3841 3997
c1151b0d 3998 if [ "$code" ] && [ "$code" != '202' ]; then
3999 if [ "$ACME_VERSION" = "2" ] && [ "$code" = '200' ]; then
4000 _debug "trigger validation code: $code"
4001 else
4002 _err "$d:Challenge error: $response"
4003 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4004 _clearup
4005 _on_issue_err "$_post_hook" "$vlist"
4006 return 1
4007 fi
4c3b3608 4008 fi
4c2a3841 4009
6fc1447f 4010 waittimes=0
4c2a3841 4011 if [ -z "$MAX_RETRY_TIMES" ]; then
6fc1447f 4012 MAX_RETRY_TIMES=30
4013 fi
4c2a3841 4014
4015 while true; do
0c538f75 4016 waittimes=$(_math "$waittimes" + 1)
4c2a3841 4017 if [ "$waittimes" -ge "$MAX_RETRY_TIMES" ]; then
6fc1447f 4018 _err "$d:Timeout"
4019 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4020 _clearup
58e4d337 4021 _on_issue_err "$_post_hook" "$vlist"
6fc1447f 4022 return 1
4023 fi
4c2a3841 4024
5dbf664a 4025 _debug "sleep 2 secs to verify"
4026 sleep 2
4c3b3608 4027 _debug "checking"
44edb2bd 4028 response="$(_get "$uri")"
4c2a3841 4029 if [ "$?" != "0" ]; then
c60883ef 4030 _err "$d:Verify error:$response"
a63b05a9 4031 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4c3b3608 4032 _clearup
58e4d337 4033 _on_issue_err "$_post_hook" "$vlist"
4c3b3608 4034 return 1
4035 fi
9aaf36cd 4036 _debug2 original "$response"
4c2a3841 4037
4038 response="$(echo "$response" | _normalizeJson)"
7012b91f 4039 _debug2 response "$response"
4c2a3841 4040
4041 status=$(echo "$response" | _egrep_o '"status":"[^"]*' | cut -d : -f 2 | tr -d '"')
4042 if [ "$status" = "valid" ]; then
93f3098a 4043 _info "$(__green Success)"
ca7202eb 4044 _stopserver "$serverproc"
4c3b3608 4045 serverproc=""
a63b05a9 4046 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4c2a3841 4047 break
4c3b3608 4048 fi
4c2a3841 4049
4050 if [ "$status" = "invalid" ]; then
4051 error="$(echo "$response" | tr -d "\r\n" | _egrep_o '"error":\{[^\}]*')"
4052 _debug2 error "$error"
4053 errordetail="$(echo "$error" | _egrep_o '"detail": *"[^"]*' | cut -d '"' -f 4)"
4054 _debug2 errordetail "$errordetail"
4055 if [ "$errordetail" ]; then
4056 _err "$d:Verify error:$errordetail"
4057 else
4058 _err "$d:Verify error:$error"
4059 fi
4060 if [ "$DEBUG" ]; then
4061 if [ "$vtype" = "$VTYPE_HTTP" ]; then
4062 _debug "Debug: get token url."
4063 _get "http://$d/.well-known/acme-challenge/$token" "" 1
4064 fi
4065 fi
a63b05a9 4066 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4c3b3608 4067 _clearup
58e4d337 4068 _on_issue_err "$_post_hook" "$vlist"
4c2a3841 4069 return 1
4c3b3608 4070 fi
4c2a3841 4071
4072 if [ "$status" = "pending" ]; then
4c3b3608 4073 _info "Pending"
4074 else
4c2a3841 4075 _err "$d:Verify error:$response"
a63b05a9 4076 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4c3b3608 4077 _clearup
58e4d337 4078 _on_issue_err "$_post_hook" "$vlist"
4c3b3608 4079 return 1
4080 fi
4c2a3841 4081
4c3b3608 4082 done
4c2a3841 4083
4c3b3608 4084 done
4085
4086 _clearup
4087 _info "Verify finished, start to sign."
11927a76 4088 der="$(_getfile "${CSR_PATH}" "${BEGIN_CSR}" "${END_CSR}" | tr -d "\r\n" | _url_replace)"
4c2a3841 4089
c1151b0d 4090 if [ "$ACME_VERSION" = "2" ]; then
d2cde379 4091 if ! _send_signed_request "${Le_OrderFinalize}" "{\"csr\": \"$der\"}"; then
c1151b0d 4092 _err "Sign failed."
4093 _on_issue_err "$_post_hook"
4094 return 1
4095 fi
4096 if [ "$code" != "200" ]; then
4097 _err "Sign failed, code is not 200."
4098 _on_issue_err "$_post_hook"
4099 return 1
4100 fi
f8d22c48 4101 Le_LinkCert="$(echo "$response" | tr -d '\r\n' | _egrep_o '"certificate" *: *"[^"]*"' | cut -d '"' -f 4)"
4c2a3841 4102
f8d22c48 4103 if ! _get "$Le_LinkCert" >"$CERT_PATH"; then
c1151b0d 4104 _err "Sign failed, code is not 200."
4105 _on_issue_err "$_post_hook"
4106 return 1
4107 fi
4c3b3608 4108
1c35f46b 4109 if [ "$(grep -- "$BEGIN_CERT" "$CERT_PATH" | wc -l)" -gt "1" ]; then
4110 _debug "Found cert chain"
120cde16 4111 cat "$CERT_PATH" >"$CERT_FULLCHAIN_PATH"
1c35f46b 4112 _end_n="$(grep -n -- "$END_CERT" "$CERT_FULLCHAIN_PATH" | _head_n 1 | cut -d : -f 1)"
4113 _debug _end_n "$_end_n"
120cde16 4114 sed -n "1,${_end_n}p" "$CERT_FULLCHAIN_PATH" >"$CERT_PATH"
1c35f46b 4115 _end_n="$(_math $_end_n + 1)"
120cde16 4116 sed -n "${_end_n},9999p" "$CERT_FULLCHAIN_PATH" >"$CA_CERT_PATH"
1c35f46b 4117 fi
c1151b0d 4118 else
4119 if ! _send_signed_request "${ACME_NEW_ORDER}" "{\"resource\": \"$ACME_NEW_ORDER_RES\", \"csr\": \"$der\"}" "needbase64"; then
323febe8 4120 _err "Sign failed. $response"
c1151b0d 4121 _on_issue_err "$_post_hook"
4122 return 1
4123 fi
4124 _rcert="$response"
36a7a840 4125 Le_LinkCert="$(grep -i '^Location.*$' "$HTTP_HEADER" | _tail_n 1 | tr -d "\r\n" | cut -d " " -f 2)"
4c2a3841 4126 echo "$BEGIN_CERT" >"$CERT_PATH"
4c3b3608 4127
72518d48 4128 #if ! _get "$Le_LinkCert" | _base64 "multiline" >> "$CERT_PATH" ; then
4129 # _debug "Get cert failed. Let's try last response."
3c07f57a 4130 # printf -- "%s" "$_rcert" | _dbase64 "multiline" | _base64 "multiline" >> "$CERT_PATH"
72518d48 4131 #fi
4c2a3841 4132
4133 if ! printf -- "%s" "$_rcert" | _dbase64 "multiline" | _base64 "multiline" >>"$CERT_PATH"; then
72518d48 4134 _debug "Try cert link."
4c2a3841 4135 _get "$Le_LinkCert" | _base64 "multiline" >>"$CERT_PATH"
d404e92d 4136 fi
4137
4c2a3841 4138 echo "$END_CERT" >>"$CERT_PATH"
c1151b0d 4139 fi
4140
4141 _debug "Le_LinkCert" "$Le_LinkCert"
4142 _savedomainconf "Le_LinkCert" "$Le_LinkCert"
4143
183063a2 4144 if [ -z "$Le_LinkCert" ] || ! _checkcert "$CERT_PATH"; then
716f7277 4145 response="$(echo "$response" | _dbase64 "multiline" | tr -d '\0' | _normalizeJson)"
183063a2 4146 _err "Sign failed: $(echo "$response" | _egrep_o '"detail":"[^"]*"')"
4147 _on_issue_err "$_post_hook"
4148 return 1
4149 fi
4150
c1151b0d 4151 if [ "$Le_LinkCert" ]; then
43822d37 4152 _info "$(__green "Cert success.")"
4c3b3608 4153 cat "$CERT_PATH"
5980ebc7 4154
4c2a3841 4155 _info "Your cert is in $(__green " $CERT_PATH ")"
4156
4157 if [ -f "$CERT_KEY_PATH" ]; then
4158 _info "Your cert key is in $(__green " $CERT_KEY_PATH ")"
5980ebc7 4159 fi
4160
4c2a3841 4161 if [ ! "$USER_PATH" ] || [ ! "$IN_CRON" ]; then
281aa349 4162 USER_PATH="$PATH"
4163 _saveaccountconf "USER_PATH" "$USER_PATH"
4164 fi
4c3b3608 4165 fi
4c3b3608 4166
4c2a3841 4167 _cleardomainconf "Le_Vlist"
4168
1c35f46b 4169 if [ "$ACME_VERSION" = "2" ]; then
4170 _debug "v2 chain."
4171 else
183063a2 4172 cp "$CERT_PATH" "$CERT_FULLCHAIN_PATH"
1c35f46b 4173 Le_LinkIssuer=$(grep -i '^Link' "$HTTP_HEADER" | _head_n 1 | cut -d " " -f 2 | cut -d ';' -f 1 | tr -d '<>')
d8ba26e6 4174
1c35f46b 4175 if [ "$Le_LinkIssuer" ]; then
4176 if ! _contains "$Le_LinkIssuer" ":"; then
4177 _info "$(__red "Relative issuer link found.")"
4178 Le_LinkIssuer="$_ACME_SERVER_HOST$Le_LinkIssuer"
d8ba26e6 4179 fi
1c35f46b 4180 _debug Le_LinkIssuer "$Le_LinkIssuer"
4181 _savedomainconf "Le_LinkIssuer" "$Le_LinkIssuer"
d8ba26e6 4182
1c35f46b 4183 _link_issuer_retry=0
4184 _MAX_ISSUER_RETRY=5
4185 while [ "$_link_issuer_retry" -lt "$_MAX_ISSUER_RETRY" ]; do
4186 _debug _link_issuer_retry "$_link_issuer_retry"
4187 if [ "$ACME_VERSION" = "2" ]; then
4188 if _get "$Le_LinkIssuer" >"$CA_CERT_PATH"; then
4189 break
4190 fi
4191 else
4192 if _get "$Le_LinkIssuer" >"$CA_CERT_PATH.der"; then
4193 echo "$BEGIN_CERT" >"$CA_CERT_PATH"
4194 _base64 "multiline" <"$CA_CERT_PATH.der" >>"$CA_CERT_PATH"
4195 echo "$END_CERT" >>"$CA_CERT_PATH"
0f120c41 4196 if ! _checkcert "$CA_CERT_PATH"; then
183063a2 4197 _err "Can not get the ca cert."
4198 break
4199 fi
1c35f46b 4200 cat "$CA_CERT_PATH" >>"$CERT_FULLCHAIN_PATH"
4201 rm -f "$CA_CERT_PATH.der"
4202 break
4203 fi
c1151b0d 4204 fi
1c35f46b 4205 _link_issuer_retry=$(_math $_link_issuer_retry + 1)
4206 _sleep "$_link_issuer_retry"
4207 done
4208 if [ "$_link_issuer_retry" = "$_MAX_ISSUER_RETRY" ]; then
4209 _err "Max retry for issuer ca cert is reached."
d8ba26e6 4210 fi
1c35f46b 4211 else
4212 _debug "No Le_LinkIssuer header found."
d8ba26e6 4213 fi
4c3b3608 4214 fi
1c35f46b 4215 [ -f "$CA_CERT_PATH" ] && _info "The intermediate CA cert is in $(__green " $CA_CERT_PATH ")"
4216 [ -f "$CERT_FULLCHAIN_PATH" ] && _info "And the full chain certs is there: $(__green " $CERT_FULLCHAIN_PATH ")"
4c2a3841 4217
3aae1ae3 4218 Le_CertCreateTime=$(_time)
4c2a3841 4219 _savedomainconf "Le_CertCreateTime" "$Le_CertCreateTime"
4220
4221 Le_CertCreateTimeStr=$(date -u)
4222 _savedomainconf "Le_CertCreateTimeStr" "$Le_CertCreateTimeStr"
4223
4224 if [ -z "$Le_RenewalDays" ] || [ "$Le_RenewalDays" -lt "0" ] || [ "$Le_RenewalDays" -gt "$MAX_RENEW" ]; then
ca7202eb 4225 Le_RenewalDays="$MAX_RENEW"
054cb72e 4226 else
4c2a3841 4227 _savedomainconf "Le_RenewalDays" "$Le_RenewalDays"
13d7cae9 4228 fi
4c2a3841 4229
4230 if [ "$CA_BUNDLE" ]; then
78009539
PS
4231 _saveaccountconf CA_BUNDLE "$CA_BUNDLE"
4232 else
4233 _clearaccountconf "CA_BUNDLE"
4234 fi
4235
2aa75f03 4236 if [ "$CA_PATH" ]; then
4237 _saveaccountconf CA_PATH "$CA_PATH"
4238 else
4239 _clearaccountconf "CA_PATH"
4240 fi
78009539 4241
4c2a3841 4242 if [ "$HTTPS_INSECURE" ]; then
fac1e367 4243 _saveaccountconf HTTPS_INSECURE "$HTTPS_INSECURE"
4244 else
4c2a3841 4245 _clearaccountconf "HTTPS_INSECURE"
13d7cae9 4246 fi
00a50605 4247
4c2a3841 4248 if [ "$Le_Listen_V4" ]; then
4249 _savedomainconf "Le_Listen_V4" "$Le_Listen_V4"
50827188 4250 _cleardomainconf Le_Listen_V6
4c2a3841 4251 elif [ "$Le_Listen_V6" ]; then
4252 _savedomainconf "Le_Listen_V6" "$Le_Listen_V6"
50827188 4253 _cleardomainconf Le_Listen_V4
4254 fi
f6dcd989 4255
c4b2e582 4256 if [ "$Le_ForceNewDomainKey" = "1" ]; then
4257 _savedomainconf "Le_ForceNewDomainKey" "$Le_ForceNewDomainKey"
4258 else
4259 _cleardomainconf Le_ForceNewDomainKey
4260 fi
4261
ca7202eb 4262 Le_NextRenewTime=$(_math "$Le_CertCreateTime" + "$Le_RenewalDays" \* 24 \* 60 \* 60)
4c2a3841 4263
ca7202eb 4264 Le_NextRenewTimeStr=$(_time2str "$Le_NextRenewTime")
4c2a3841 4265 _savedomainconf "Le_NextRenewTimeStr" "$Le_NextRenewTimeStr"
4266
ca7202eb 4267 Le_NextRenewTime=$(_math "$Le_NextRenewTime" - 86400)
4c2a3841 4268 _savedomainconf "Le_NextRenewTime" "$Le_NextRenewTime"
f6dcd989 4269
6104680c 4270 if ! _on_issue_success "$_post_hook" "$_renew_hook"; then
4271 _err "Call hook error."
4272 return 1
4273 fi
4c3b3608 4274
85e1f4ea 4275 if [ "$_real_cert$_real_key$_real_ca$_reload_cmd$_real_fullchain" ]; then
4276 _savedomainconf "Le_RealCertPath" "$_real_cert"
4277 _savedomainconf "Le_RealCACertPath" "$_real_ca"
4278 _savedomainconf "Le_RealKeyPath" "$_real_key"
4279 _savedomainconf "Le_ReloadCmd" "$_reload_cmd"
4280 _savedomainconf "Le_RealFullChainPath" "$_real_fullchain"
044da37c 4281 _installcert "$_main_domain" "$_real_cert" "$_real_key" "$_real_ca" "$_real_fullchain" "$_reload_cmd"
01f54558 4282 fi
4c0d3f1b 4283
4c3b3608 4284}
4285
43822d37 4286#domain [isEcc]
4c3b3608 4287renew() {
4288 Le_Domain="$1"
4c2a3841 4289 if [ -z "$Le_Domain" ]; then
43822d37 4290 _usage "Usage: $PROJECT_ENTRY --renew -d domain.com [--ecc]"
4c3b3608 4291 return 1
4292 fi
4293
43822d37 4294 _isEcc="$2"
4295
e799ef29 4296 _initpath "$Le_Domain" "$_isEcc"
43822d37 4297
e2053b22 4298 _info "$(__green "Renew: '$Le_Domain'")"
4c2a3841 4299 if [ ! -f "$DOMAIN_CONF" ]; then
43822d37 4300 _info "'$Le_Domain' is not a issued domain, skip."
4c2a3841 4301 return 0
4c3b3608 4302 fi
4c2a3841 4303
4304 if [ "$Le_RenewalDays" ]; then
1e6b68f5 4305 _savedomainconf Le_RenewalDays "$Le_RenewalDays"
4306 fi
4307
8663fb7e 4308 . "$DOMAIN_CONF"
c5f1cca3 4309 _debug Le_API "$Le_API"
4c2a3841 4310 if [ "$Le_API" ]; then
48d9a8c1 4311 if [ "$_OLD_CA_HOST" = "$Le_API" ]; then
4312 export Le_API="$DEFAULT_CA"
4313 _savedomainconf Le_API "$Le_API"
4314 fi
4a2ac7bd 4315 if [ "$_OLD_STAGE_CA_HOST" = "$Le_API" ]; then
c1151b0d 4316 export Le_API="$DEFAULT_STAGING_CA"
4a2ac7bd 4317 _savedomainconf Le_API "$Le_API"
4318 fi
48d9a8c1 4319 export ACME_DIRECTORY="$Le_API"
c4236e58 4320 #reload ca configs
4321 ACCOUNT_KEY_PATH=""
4322 ACCOUNT_JSON_PATH=""
4323 CA_CONF=""
4324 _debug3 "initpath again."
4325 _initpath "$Le_Domain" "$_isEcc"
5c48e139 4326 fi
4c2a3841 4327
4328 if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(_time)" -lt "$Le_NextRenewTime" ]; then
e2053b22 4329 _info "Skip, Next renewal time is: $(__green "$Le_NextRenewTimeStr")"
4330 _info "Add '$(__red '--force')' to force to renew."
e799ef29 4331 return "$RENEW_SKIP"
4c3b3608 4332 fi
4c2a3841 4333
c4d0aec5 4334 if [ "$IN_CRON" = "1" ] && [ -z "$Le_CertCreateTime" ]; then
4335 _info "Skip invalid cert for: $Le_Domain"
4336 return 0
4337 fi
4338
4c3b3608 4339 IS_RENEW="1"
875625b1 4340 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 4341 res="$?"
4c2a3841 4342 if [ "$res" != "0" ]; then
e799ef29 4343 return "$res"
a61fe418 4344 fi
4c2a3841 4345
4346 if [ "$Le_DeployHook" ]; then
93bce1b2 4347 _deploy "$Le_Domain" "$Le_DeployHook"
e799ef29 4348 res="$?"
a61fe418 4349 fi
4c2a3841 4350
4c3b3608 4351 IS_RENEW=""
4352
e799ef29 4353 return "$res"
4c3b3608 4354}
4355
cc179731 4356#renewAll [stopRenewOnError]
4c3b3608 4357renewAll() {
4358 _initpath
cc179731 4359 _stopRenewOnError="$1"
4360 _debug "_stopRenewOnError" "$_stopRenewOnError"
4361 _ret="0"
43822d37 4362
e591d5cf 4363 for di in "${CERT_HOME}"/*.*/; do
4364 _debug di "$di"
44483dba 4365 if ! [ -d "$di" ]; then
3498a585 4366 _debug "Not directory, skip: $di"
4367 continue
4368 fi
e591d5cf 4369 d=$(basename "$di")
201aa244 4370 _debug d "$d"
43822d37 4371 (
201aa244 4372 if _endswith "$d" "$ECC_SUFFIX"; then
4373 _isEcc=$(echo "$d" | cut -d "$ECC_SEP" -f 2)
4374 d=$(echo "$d" | cut -d "$ECC_SEP" -f 1)
43822d37 4375 fi
4376 renew "$d" "$_isEcc"
4d2f38b0 4377 )
cc179731 4378 rc="$?"
4379 _debug "Return code: $rc"
4c2a3841 4380 if [ "$rc" != "0" ]; then
4381 if [ "$rc" = "$RENEW_SKIP" ]; then
cc179731 4382 _info "Skipped $d"
4c2a3841 4383 elif [ "$_stopRenewOnError" ]; then
cc179731 4384 _err "Error renew $d, stop now."
201aa244 4385 return "$rc"
cc179731 4386 else
4387 _ret="$rc"
482cb737 4388 _err "Error renew $d."
cc179731 4389 fi
4390 fi
4c3b3608 4391 done
201aa244 4392 return "$_ret"
4c3b3608 4393}
4394
10afcaca 4395#csr webroot
4c2a3841 4396signcsr() {
10afcaca 4397 _csrfile="$1"
4398 _csrW="$2"
4399 if [ -z "$_csrfile" ] || [ -z "$_csrW" ]; then
4400 _usage "Usage: $PROJECT_ENTRY --signcsr --csr mycsr.csr -w /path/to/webroot/a.com/ "
4401 return 1
4402 fi
4403
875625b1 4404 _real_cert="$3"
4405 _real_key="$4"
4406 _real_ca="$5"
4407 _reload_cmd="$6"
4408 _real_fullchain="$7"
4409 _pre_hook="${8}"
4410 _post_hook="${9}"
4411 _renew_hook="${10}"
4412 _local_addr="${11}"
4413 _challenge_alias="${12}"
4414
10afcaca 4415 _csrsubj=$(_readSubjectFromCSR "$_csrfile")
4c2a3841 4416 if [ "$?" != "0" ]; then
10afcaca 4417 _err "Can not read subject from csr: $_csrfile"
4418 return 1
4419 fi
ad752b31 4420 _debug _csrsubj "$_csrsubj"
2c9ed4c5 4421 if _contains "$_csrsubj" ' ' || ! _contains "$_csrsubj" '.'; then
4422 _info "It seems that the subject: $_csrsubj is not a valid domain name. Drop it."
4423 _csrsubj=""
4424 fi
10afcaca 4425
4426 _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile")
4c2a3841 4427 if [ "$?" != "0" ]; then
10afcaca 4428 _err "Can not read domain list from csr: $_csrfile"
4429 return 1
4430 fi
4431 _debug "_csrdomainlist" "$_csrdomainlist"
4c2a3841 4432
4433 if [ -z "$_csrsubj" ]; then
ad752b31 4434 _csrsubj="$(_getfield "$_csrdomainlist" 1)"
4435 _debug _csrsubj "$_csrsubj"
4436 _csrdomainlist="$(echo "$_csrdomainlist" | cut -d , -f 2-)"
4437 _debug "_csrdomainlist" "$_csrdomainlist"
4438 fi
4c2a3841 4439
4440 if [ -z "$_csrsubj" ]; then
ad752b31 4441 _err "Can not read subject from csr: $_csrfile"
4442 return 1
4443 fi
4c2a3841 4444
10afcaca 4445 _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile")
4c2a3841 4446 if [ "$?" != "0" ] || [ -z "$_csrkeylength" ]; then
10afcaca 4447 _err "Can not read key length from csr: $_csrfile"
4448 return 1
4449 fi
4c2a3841 4450
cd9fb3b6 4451 if [ -z "$ACME_VERSION" ] && _contains "$_csrsubj,$_csrdomainlist" "*."; then
4452 export ACME_VERSION=2
4453 fi
10afcaca 4454 _initpath "$_csrsubj" "$_csrkeylength"
4455 mkdir -p "$DOMAIN_PATH"
4c2a3841 4456
10afcaca 4457 _info "Copy csr to: $CSR_PATH"
4458 cp "$_csrfile" "$CSR_PATH"
4c2a3841 4459
875625b1 4460 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 4461
10afcaca 4462}
4463
4464showcsr() {
4c2a3841 4465 _csrfile="$1"
10afcaca 4466 _csrd="$2"
4467 if [ -z "$_csrfile" ] && [ -z "$_csrd" ]; then
4468 _usage "Usage: $PROJECT_ENTRY --showcsr --csr mycsr.csr"
4469 return 1
4470 fi
4471
4472 _initpath
4c2a3841 4473
10afcaca 4474 _csrsubj=$(_readSubjectFromCSR "$_csrfile")
4c2a3841 4475 if [ "$?" != "0" ] || [ -z "$_csrsubj" ]; then
10afcaca 4476 _err "Can not read subject from csr: $_csrfile"
4477 return 1
4478 fi
4c2a3841 4479
10afcaca 4480 _info "Subject=$_csrsubj"
4481
4482 _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile")
4c2a3841 4483 if [ "$?" != "0" ]; then
10afcaca 4484 _err "Can not read domain list from csr: $_csrfile"
4485 return 1
4486 fi
4487 _debug "_csrdomainlist" "$_csrdomainlist"
4488
4489 _info "SubjectAltNames=$_csrdomainlist"
4490
10afcaca 4491 _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile")
4c2a3841 4492 if [ "$?" != "0" ] || [ -z "$_csrkeylength" ]; then
10afcaca 4493 _err "Can not read key length from csr: $_csrfile"
4494 return 1
4495 fi
4496 _info "KeyLength=$_csrkeylength"
4497}
4498
6d7eda3e 4499list() {
22ea4004 4500 _raw="$1"
6d7eda3e 4501 _initpath
4c2a3841 4502
dcf4f8f6 4503 _sep="|"
4c2a3841 4504 if [ "$_raw" ]; then
d5ec5f80 4505 printf "%s\n" "Main_Domain${_sep}KeyLength${_sep}SAN_Domains${_sep}Created${_sep}Renew"
e591d5cf 4506 for di in "${CERT_HOME}"/*.*/; do
44483dba 4507 if ! [ -d "$di" ]; then
3498a585 4508 _debug "Not directory, skip: $di"
4509 continue
4510 fi
e591d5cf 4511 d=$(basename "$di")
201aa244 4512 _debug d "$d"
dcf4f8f6 4513 (
201aa244 4514 if _endswith "$d" "$ECC_SUFFIX"; then
4515 _isEcc=$(echo "$d" | cut -d "$ECC_SEP" -f 2)
4516 d=$(echo "$d" | cut -d "$ECC_SEP" -f 1)
43822d37 4517 fi
e591d5cf 4518 _initpath "$d" "$_isEcc"
4c2a3841 4519 if [ -f "$DOMAIN_CONF" ]; then
dcf4f8f6 4520 . "$DOMAIN_CONF"
d5ec5f80 4521 printf "%s\n" "$Le_Domain${_sep}\"$Le_Keylength\"${_sep}$Le_Alt${_sep}$Le_CertCreateTimeStr${_sep}$Le_NextRenewTimeStr"
dcf4f8f6 4522 fi
4523 )
4524 done
4525 else
4c2a3841 4526 if _exists column; then
22ea4004 4527 list "raw" | column -t -s "$_sep"
4528 else
43822d37 4529 list "raw" | tr "$_sep" '\t'
22ea4004 4530 fi
dcf4f8f6 4531 fi
6d7eda3e 4532
6d7eda3e 4533}
4534
93bce1b2 4535_deploy() {
4536 _d="$1"
4537 _hooks="$2"
4538
4539 for _d_api in $(echo "$_hooks" | tr ',' " "); do
4540 _deployApi="$(_findHook "$_d" deploy "$_d_api")"
4541 if [ -z "$_deployApi" ]; then
4542 _err "The deploy hook $_d_api is not found."
4543 return 1
4544 fi
4545 _debug _deployApi "$_deployApi"
4546
4547 if ! (
4548 if ! . "$_deployApi"; then
4549 _err "Load file $_deployApi error. Please check your api file and try again."
4550 return 1
4551 fi
4552
4553 d_command="${_d_api}_deploy"
4554 if ! _exists "$d_command"; then
4555 _err "It seems that your api file is not correct, it must have a function named: $d_command"
4556 return 1
4557 fi
4558
4559 if ! $d_command "$_d" "$CERT_KEY_PATH" "$CERT_PATH" "$CA_CERT_PATH" "$CERT_FULLCHAIN_PATH"; then
4560 _err "Error deploy for domain:$_d"
4561 return 1
4562 fi
4563 ); then
4564 _err "Deploy error."
4565 return 1
4566 else
4567 _info "$(__green Success)"
4568 fi
4569 done
4570}
4571
4572#domain hooks
a61fe418 4573deploy() {
93bce1b2 4574 _d="$1"
4575 _hooks="$2"
a61fe418 4576 _isEcc="$3"
93bce1b2 4577 if [ -z "$_hooks" ]; then
a61fe418 4578 _usage "Usage: $PROJECT_ENTRY --deploy -d domain.com --deploy-hook cpanel [--ecc] "
4579 return 1
4580 fi
4581
93bce1b2 4582 _initpath "$_d" "$_isEcc"
4c2a3841 4583 if [ ! -d "$DOMAIN_PATH" ]; then
93bce1b2 4584 _err "Domain is not valid:'$_d'"
a61fe418 4585 return 1
4586 fi
4c2a3841 4587
93bce1b2 4588 . "$DOMAIN_CONF"
4c2a3841 4589
93bce1b2 4590 _savedomainconf Le_DeployHook "$_hooks"
4c2a3841 4591
93bce1b2 4592 _deploy "$_d" "$_hooks"
a61fe418 4593}
4594
4c3b3608 4595installcert() {
85e1f4ea 4596 _main_domain="$1"
4597 if [ -z "$_main_domain" ]; then
5c539af7 4598 _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 4599 return 1
4600 fi
4601
85e1f4ea 4602 _real_cert="$2"
4603 _real_key="$3"
4604 _real_ca="$4"
4605 _reload_cmd="$5"
4606 _real_fullchain="$6"
43822d37 4607 _isEcc="$7"
4608
85e1f4ea 4609 _initpath "$_main_domain" "$_isEcc"
4c2a3841 4610 if [ ! -d "$DOMAIN_PATH" ]; then
85e1f4ea 4611 _err "Domain is not valid:'$_main_domain'"
43822d37 4612 return 1
4613 fi
4614
85e1f4ea 4615 _savedomainconf "Le_RealCertPath" "$_real_cert"
4616 _savedomainconf "Le_RealCACertPath" "$_real_ca"
4617 _savedomainconf "Le_RealKeyPath" "$_real_key"
4618 _savedomainconf "Le_ReloadCmd" "$_reload_cmd"
4619 _savedomainconf "Le_RealFullChainPath" "$_real_fullchain"
4620
044da37c 4621 _installcert "$_main_domain" "$_real_cert" "$_real_key" "$_real_ca" "$_real_fullchain" "$_reload_cmd"
43822d37 4622}
4c3b3608 4623
044da37c 4624#domain cert key ca fullchain reloadcmd backup-prefix
43822d37 4625_installcert() {
85e1f4ea 4626 _main_domain="$1"
4627 _real_cert="$2"
4628 _real_key="$3"
4629 _real_ca="$4"
044da37c 4630 _real_fullchain="$5"
4631 _reload_cmd="$6"
4632 _backup_prefix="$7"
4c3b3608 4633
85e1f4ea 4634 if [ "$_real_cert" = "$NO_VALUE" ]; then
4635 _real_cert=""
4d2f38b0 4636 fi
85e1f4ea 4637 if [ "$_real_key" = "$NO_VALUE" ]; then
4638 _real_key=""
4d2f38b0 4639 fi
85e1f4ea 4640 if [ "$_real_ca" = "$NO_VALUE" ]; then
4641 _real_ca=""
4d2f38b0 4642 fi
85e1f4ea 4643 if [ "$_reload_cmd" = "$NO_VALUE" ]; then
4644 _reload_cmd=""
4d2f38b0 4645 fi
85e1f4ea 4646 if [ "$_real_fullchain" = "$NO_VALUE" ]; then
4647 _real_fullchain=""
4d2f38b0 4648 fi
4c2a3841 4649
044da37c 4650 _backup_path="$DOMAIN_BACKUP_PATH/$_backup_prefix"
4651 mkdir -p "$_backup_path"
4652
85e1f4ea 4653 if [ "$_real_cert" ]; then
4654 _info "Installing cert to:$_real_cert"
4655 if [ -f "$_real_cert" ] && [ ! "$IS_RENEW" ]; then
044da37c 4656 cp "$_real_cert" "$_backup_path/cert.bak"
4c3b3608 4657 fi
85e1f4ea 4658 cat "$CERT_PATH" >"$_real_cert"
4c3b3608 4659 fi
4c2a3841 4660
85e1f4ea 4661 if [ "$_real_ca" ]; then
4662 _info "Installing CA to:$_real_ca"
4663 if [ "$_real_ca" = "$_real_cert" ]; then
4664 echo "" >>"$_real_ca"
4665 cat "$CA_CERT_PATH" >>"$_real_ca"
4c3b3608 4666 else
85e1f4ea 4667 if [ -f "$_real_ca" ] && [ ! "$IS_RENEW" ]; then
044da37c 4668 cp "$_real_ca" "$_backup_path/ca.bak"
78552b18 4669 fi
85e1f4ea 4670 cat "$CA_CERT_PATH" >"$_real_ca"
4c3b3608 4671 fi
4672 fi
4673
85e1f4ea 4674 if [ "$_real_key" ]; then
4675 _info "Installing key to:$_real_key"
4676 if [ -f "$_real_key" ] && [ ! "$IS_RENEW" ]; then
044da37c 4677 cp "$_real_key" "$_backup_path/key.bak"
4c3b3608 4678 fi
82014583 4679 if [ -f "$_real_key" ]; then
4680 cat "$CERT_KEY_PATH" >"$_real_key"
4681 else
4682 cat "$CERT_KEY_PATH" >"$_real_key"
7b92371a 4683 chmod 600 "$_real_key"
82014583 4684 fi
4c3b3608 4685 fi
4c2a3841 4686
85e1f4ea 4687 if [ "$_real_fullchain" ]; then
4688 _info "Installing full chain to:$_real_fullchain"
4689 if [ -f "$_real_fullchain" ] && [ ! "$IS_RENEW" ]; then
044da37c 4690 cp "$_real_fullchain" "$_backup_path/fullchain.bak"
a63b05a9 4691 fi
85e1f4ea 4692 cat "$CERT_FULLCHAIN_PATH" >"$_real_fullchain"
4c2a3841 4693 fi
4c3b3608 4694
85e1f4ea 4695 if [ "$_reload_cmd" ]; then
4696 _info "Run reload cmd: $_reload_cmd"
25555b8c 4697 if (
839bf0e2 4698 export CERT_PATH
4699 export CERT_KEY_PATH
4700 export CA_CERT_PATH
4701 export CERT_FULLCHAIN_PATH
58d4c74b 4702 export Le_Domain
85e1f4ea 4703 cd "$DOMAIN_PATH" && eval "$_reload_cmd"
839bf0e2 4704 ); then
43822d37 4705 _info "$(__green "Reload success")"
4d2f38b0 4706 else
4707 _err "Reload error for :$Le_Domain"
4708 fi
4709 fi
4710
4c3b3608 4711}
4712
27dbe77f 4713#confighome
4c3b3608 4714installcronjob() {
27dbe77f 4715 _c_home="$1"
4c3b3608 4716 _initpath
415f375c 4717 _CRONTAB="crontab"
4718 if ! _exists "$_CRONTAB" && _exists "fcrontab"; then
4719 _CRONTAB="fcrontab"
4720 fi
4721 if ! _exists "$_CRONTAB"; then
4722 _err "crontab/fcrontab doesn't exist, so, we can not install cron jobs."
77546ea5 4723 _err "All your certs will not be renewed automatically."
a7b7355d 4724 _err "You must add your own cron job to call '$PROJECT_ENTRY --cron' everyday."
77546ea5 4725 return 1
4726 fi
4727
4c3b3608 4728 _info "Installing cron job"
415f375c 4729 if ! $_CRONTAB -l | grep "$PROJECT_ENTRY --cron"; then
4c2a3841 4730 if [ -f "$LE_WORKING_DIR/$PROJECT_ENTRY" ]; then
a7b7355d 4731 lesh="\"$LE_WORKING_DIR\"/$PROJECT_ENTRY"
4c3b3608 4732 else
a7b7355d 4733 _err "Can not install cronjob, $PROJECT_ENTRY not found."
4c3b3608 4734 return 1
4735 fi
27dbe77f 4736
4737 if [ "$_c_home" ]; then
80941f84 4738 _c_entry="--config-home \"$_c_home\" "
27dbe77f 4739 fi
32b3717c 4740 _t=$(_time)
4741 random_minute=$(_math $_t % 60)
e2c939fb 4742 if _exists uname && uname -a | grep SunOS >/dev/null; then
415f375c 4743 $_CRONTAB -l | {
4c2a3841 4744 cat
0533bde9 4745 echo "$random_minute 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" $_c_entry> /dev/null"
415f375c 4746 } | $_CRONTAB --
22ea4004 4747 else
415f375c 4748 $_CRONTAB -l | {
4c2a3841 4749 cat
0533bde9 4750 echo "$random_minute 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" $_c_entry> /dev/null"
415f375c 4751 } | $_CRONTAB -
22ea4004 4752 fi
4c3b3608 4753 fi
4c2a3841 4754 if [ "$?" != "0" ]; then
4c3b3608 4755 _err "Install cron job failed. You need to manually renew your certs."
4756 _err "Or you can add cronjob by yourself:"
a7b7355d 4757 _err "$lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null"
4c3b3608 4758 return 1
4759 fi
4760}
4761
4762uninstallcronjob() {
415f375c 4763 _CRONTAB="crontab"
4764 if ! _exists "$_CRONTAB" && _exists "fcrontab"; then
4765 _CRONTAB="fcrontab"
4766 fi
4767
4768 if ! _exists "$_CRONTAB"; then
37db5b81 4769 return
4770 fi
4c3b3608 4771 _info "Removing cron job"
415f375c 4772 cr="$($_CRONTAB -l | grep "$PROJECT_ENTRY --cron")"
4c2a3841 4773 if [ "$cr" ]; then
4774 if _exists uname && uname -a | grep solaris >/dev/null; then
415f375c 4775 $_CRONTAB -l | sed "/$PROJECT_ENTRY --cron/d" | $_CRONTAB --
22ea4004 4776 else
415f375c 4777 $_CRONTAB -l | sed "/$PROJECT_ENTRY --cron/d" | $_CRONTAB -
22ea4004 4778 fi
a7b7355d 4779 LE_WORKING_DIR="$(echo "$cr" | cut -d ' ' -f 9 | tr -d '"')"
4c3b3608 4780 _info LE_WORKING_DIR "$LE_WORKING_DIR"
27dbe77f 4781 if _contains "$cr" "--config-home"; then
f5b546b3 4782 LE_CONFIG_HOME="$(echo "$cr" | cut -d ' ' -f 11 | tr -d '"')"
4783 _debug LE_CONFIG_HOME "$LE_CONFIG_HOME"
27dbe77f 4784 fi
4c2a3841 4785 fi
4c3b3608 4786 _initpath
a7b7355d 4787
4c3b3608 4788}
4789
6cb415f5 4790revoke() {
4791 Le_Domain="$1"
4c2a3841 4792 if [ -z "$Le_Domain" ]; then
78f0201d 4793 _usage "Usage: $PROJECT_ENTRY --revoke -d domain.com [--ecc]"
6cb415f5 4794 return 1
4795 fi
4c2a3841 4796
43822d37 4797 _isEcc="$2"
4798
c4a375b3 4799 _initpath "$Le_Domain" "$_isEcc"
4c2a3841 4800 if [ ! -f "$DOMAIN_CONF" ]; then
6cb415f5 4801 _err "$Le_Domain is not a issued domain, skip."
4c2a3841 4802 return 1
6cb415f5 4803 fi
4c2a3841 4804
4805 if [ ! -f "$CERT_PATH" ]; then
6cb415f5 4806 _err "Cert for $Le_Domain $CERT_PATH is not found, skip."
4807 return 1
4808 fi
6cb415f5 4809
11927a76 4810 cert="$(_getfile "${CERT_PATH}" "${BEGIN_CERT}" "${END_CERT}" | tr -d "\r\n" | _url_replace)"
4c2a3841 4811
4812 if [ -z "$cert" ]; then
6cb415f5 4813 _err "Cert for $Le_Domain is empty found, skip."
4814 return 1
4815 fi
4c2a3841 4816
48d9a8c1 4817 _initAPI
4818
d2cde379 4819 if [ "$ACME_VERSION" = "2" ]; then
4820 data="{\"certificate\": \"$cert\"}"
4821 else
4822 data="{\"resource\": \"revoke-cert\", \"certificate\": \"$cert\"}"
4823 fi
48d9a8c1 4824 uri="${ACME_REVOKE_CERT}"
6cb415f5 4825
4c2a3841 4826 if [ -f "$CERT_KEY_PATH" ]; then
1befee5a 4827 _info "Try domain key first."
c4a375b3 4828 if _send_signed_request "$uri" "$data" "" "$CERT_KEY_PATH"; then
4c2a3841 4829 if [ -z "$response" ]; then
1befee5a 4830 _info "Revoke success."
c4a375b3 4831 rm -f "$CERT_PATH"
1befee5a 4832 return 0
4c2a3841 4833 else
1befee5a 4834 _err "Revoke error by domain key."
4835 _err "$response"
4836 fi
6cb415f5 4837 fi
4c2a3841 4838 else
1befee5a 4839 _info "Domain key file doesn't exists."
6cb415f5 4840 fi
6cb415f5 4841
1befee5a 4842 _info "Try account key."
6cb415f5 4843
c4a375b3 4844 if _send_signed_request "$uri" "$data" "" "$ACCOUNT_KEY_PATH"; then
4c2a3841 4845 if [ -z "$response" ]; then
6cb415f5 4846 _info "Revoke success."
c4a375b3 4847 rm -f "$CERT_PATH"
6cb415f5 4848 return 0
4c2a3841 4849 else
6cb415f5 4850 _err "Revoke error."
c9c31c04 4851 _debug "$response"
6cb415f5 4852 fi
4853 fi
4854 return 1
4855}
4c3b3608 4856
78f0201d 4857#domain ecc
4858remove() {
4859 Le_Domain="$1"
4860 if [ -z "$Le_Domain" ]; then
4861 _usage "Usage: $PROJECT_ENTRY --remove -d domain.com [--ecc]"
4862 return 1
4863 fi
4864
4865 _isEcc="$2"
4866
4867 _initpath "$Le_Domain" "$_isEcc"
4868 _removed_conf="$DOMAIN_CONF.removed"
4869 if [ ! -f "$DOMAIN_CONF" ]; then
4870 if [ -f "$_removed_conf" ]; then
4871 _err "$Le_Domain is already removed, You can remove the folder by yourself: $DOMAIN_PATH"
4872 else
4873 _err "$Le_Domain is not a issued domain, skip."
4874 fi
4875 return 1
4876 fi
4877
4878 if mv "$DOMAIN_CONF" "$_removed_conf"; then
68aea3af 4879 _info "$Le_Domain is removed, the key and cert files are in $(__green $DOMAIN_PATH)"
78f0201d 4880 _info "You can remove them by yourself."
4881 return 0
4882 else
4883 _err "Remove $Le_Domain failed."
4884 return 1
4885 fi
4886}
4887
0c00e870 4888#domain vtype
4889_deactivate() {
4890 _d_domain="$1"
4891 _d_type="$2"
4892 _initpath
4c2a3841 4893
d2cde379 4894 if [ "$ACME_VERSION" = "2" ]; then
4895 _identifiers="{\"type\":\"dns\",\"value\":\"$_d_domain\"}"
4896 if ! _send_signed_request "$ACME_NEW_ORDER" "{\"identifiers\": [$_identifiers]}"; then
4897 _err "Can not get domain new order."
4898 return 1
4899 fi
4900 _authorizations_seg="$(echo "$response" | tr -d '\r\n' | _egrep_o '"authorizations" *: *\[[^\]*\]' | cut -d '[' -f 2 | tr -d ']' | tr -d '"')"
4901 _debug2 _authorizations_seg "$_authorizations_seg"
4902 if [ -z "$_authorizations_seg" ]; then
4903 _err "_authorizations_seg not found."
4904 _clearup
4905 _on_issue_err "$_post_hook"
4906 return 1
4907 fi
4c2a3841 4908
d2cde379 4909 authzUri="$_authorizations_seg"
4910 _debug2 "authzUri" "$authzUri"
4911 if ! response="$(_get "$authzUri")"; then
4912 _err "get to authz error."
263c38ca 4913 _err "_authorizations_seg" "$_authorizations_seg"
4914 _err "authzUri" "$authzUri"
d2cde379 4915 _clearup
4916 _on_issue_err "$_post_hook"
4917 return 1
4918 fi
4c2a3841 4919
d2cde379 4920 response="$(echo "$response" | _normalizeJson)"
4921 _debug2 response "$response"
4922 _URL_NAME="url"
4923 else
4924 if ! __get_domain_new_authz "$_d_domain"; then
4925 _err "Can not get domain new authz token."
4926 return 1
4927 fi
4928
4929 authzUri="$(echo "$responseHeaders" | grep "^Location:" | _head_n 1 | cut -d ' ' -f 2 | tr -d "\r\n")"
4930 _debug "authzUri" "$authzUri"
4931 if [ "$code" ] && [ ! "$code" = '201' ]; then
4932 _err "new-authz error: $response"
4933 return 1
4934 fi
4935 _URL_NAME="uri"
14d7bfda 4936 fi
0c00e870 4937
d2cde379 4938 entries="$(echo "$response" | _egrep_o "{ *\"type\":\"[^\"]*\", *\"status\": *\"valid\", *\"$_URL_NAME\"[^}]*")"
14d7bfda 4939 if [ -z "$entries" ]; then
4940 _info "No valid entries found."
4941 if [ -z "$thumbprint" ]; then
4942 thumbprint="$(__calc_account_thumbprint)"
4943 fi
4944 _debug "Trigger validation."
d2cde379 4945 vtype="$VTYPE_DNS"
14d7bfda 4946 entry="$(printf "%s\n" "$response" | _egrep_o '[^\{]*"type":"'$vtype'"[^\}]*')"
4947 _debug entry "$entry"
4948 if [ -z "$entry" ]; then
4949 _err "Error, can not get domain token $d"
0c00e870 4950 return 1
4951 fi
14d7bfda 4952 token="$(printf "%s\n" "$entry" | _egrep_o '"token":"[^"]*' | cut -d : -f 2 | tr -d '"')"
4953 _debug token "$token"
4c2a3841 4954
d2cde379 4955 uri="$(printf "%s\n" "$entry" | _egrep_o "\"$_URL_NAME\":\"[^\"]*" | cut -d : -f 2,3 | tr -d '"')"
14d7bfda 4956 _debug uri "$uri"
4957
4958 keyauthorization="$token.$thumbprint"
4959 _debug keyauthorization "$keyauthorization"
4960 __trigger_validation "$uri" "$keyauthorization"
4961
4962 fi
4963
4964 _d_i=0
4965 _d_max_retry=$(echo "$entries" | wc -l)
4966 while [ "$_d_i" -lt "$_d_max_retry" ]; do
4967 _info "Deactivate: $_d_domain"
4968 _d_i="$(_math $_d_i + 1)"
4969 entry="$(echo "$entries" | sed -n "${_d_i}p")"
0c00e870 4970 _debug entry "$entry"
4c2a3841 4971
4972 if [ -z "$entry" ]; then
fb2029e7 4973 _info "No more valid entry found."
0c00e870 4974 break
4975 fi
4c2a3841 4976
0c00e870 4977 _vtype="$(printf "%s\n" "$entry" | _egrep_o '"type": *"[^"]*"' | cut -d : -f 2 | tr -d '"')"
c4a375b3 4978 _debug _vtype "$_vtype"
0c00e870 4979 _info "Found $_vtype"
4980
d2cde379 4981 uri="$(printf "%s\n" "$entry" | _egrep_o "\"$_URL_NAME\":\"[^\"]*" | cut -d : -f 2,3 | tr -d '"')"
c4a375b3 4982 _debug uri "$uri"
4c2a3841 4983
4984 if [ "$_d_type" ] && [ "$_d_type" != "$_vtype" ]; then
0c00e870 4985 _info "Skip $_vtype"
4986 continue
4987 fi
4c2a3841 4988
0c00e870 4989 _info "Deactivate: $_vtype"
4c2a3841 4990
d2cde379 4991 if [ "$ACME_VERSION" = "2" ]; then
4992 _djson="{\"status\":\"deactivated\"}"
4993 else
4994 _djson="{\"resource\": \"authz\", \"status\":\"deactivated\"}"
4995 fi
4996
4997 if _send_signed_request "$authzUri" "$_djson" && _contains "$response" '"deactivated"'; then
14d7bfda 4998 _info "Deactivate: $_vtype success."
4999 else
0c00e870 5000 _err "Can not deactivate $_vtype."
14d7bfda 5001 break
0c00e870 5002 fi
4c2a3841 5003
0c00e870 5004 done
5005 _debug "$_d_i"
14d7bfda 5006 if [ "$_d_i" -eq "$_d_max_retry" ]; then
0c00e870 5007 _info "Deactivated success!"
5008 else
5009 _err "Deactivate failed."
5010 fi
5011
5012}
5013
5014deactivate() {
3f4513b3 5015 _d_domain_list="$1"
0c00e870 5016 _d_type="$2"
5017 _initpath
a3bdaa85 5018 _initAPI
3f4513b3 5019 _debug _d_domain_list "$_d_domain_list"
4c2a3841 5020 if [ -z "$(echo $_d_domain_list | cut -d , -f 1)" ]; then
3f4513b3 5021 _usage "Usage: $PROJECT_ENTRY --deactivate -d domain.com [-d domain.com]"
0c00e870 5022 return 1
5023 fi
4c2a3841 5024 for _d_dm in $(echo "$_d_domain_list" | tr ',' ' '); do
5025 if [ -z "$_d_dm" ] || [ "$_d_dm" = "$NO_VALUE" ]; then
3f4513b3 5026 continue
5027 fi
c4a375b3 5028 if ! _deactivate "$_d_dm" "$_d_type"; then
86c017ec 5029 return 1
5030 fi
3f4513b3 5031 done
0c00e870 5032}
5033
4c3b3608 5034# Detect profile file if not specified as environment variable
5035_detect_profile() {
4c2a3841 5036 if [ -n "$PROFILE" -a -f "$PROFILE" ]; then
4c3b3608 5037 echo "$PROFILE"
5038 return
5039 fi
5040
4c3b3608 5041 DETECTED_PROFILE=''
4c3b3608 5042 SHELLTYPE="$(basename "/$SHELL")"
5043
4c2a3841 5044 if [ "$SHELLTYPE" = "bash" ]; then
5045 if [ -f "$HOME/.bashrc" ]; then
4c3b3608 5046 DETECTED_PROFILE="$HOME/.bashrc"
4c2a3841 5047 elif [ -f "$HOME/.bash_profile" ]; then
4c3b3608 5048 DETECTED_PROFILE="$HOME/.bash_profile"
5049 fi
4c2a3841 5050 elif [ "$SHELLTYPE" = "zsh" ]; then
4c3b3608 5051 DETECTED_PROFILE="$HOME/.zshrc"
5052 fi
5053
4c2a3841 5054 if [ -z "$DETECTED_PROFILE" ]; then
5055 if [ -f "$HOME/.profile" ]; then
4c3b3608 5056 DETECTED_PROFILE="$HOME/.profile"
4c2a3841 5057 elif [ -f "$HOME/.bashrc" ]; then
4c3b3608 5058 DETECTED_PROFILE="$HOME/.bashrc"
4c2a3841 5059 elif [ -f "$HOME/.bash_profile" ]; then
4c3b3608 5060 DETECTED_PROFILE="$HOME/.bash_profile"
4c2a3841 5061 elif [ -f "$HOME/.zshrc" ]; then
4c3b3608 5062 DETECTED_PROFILE="$HOME/.zshrc"
5063 fi
5064 fi
5065
1be222f6 5066 echo "$DETECTED_PROFILE"
4c3b3608 5067}
5068
5069_initconf() {
5070 _initpath
4c2a3841 5071 if [ ! -f "$ACCOUNT_CONF_PATH" ]; then
0ca5b799 5072 echo "
d404e92d 5073
d0871bda 5074#LOG_FILE=\"$DEFAULT_LOG_FILE\"
6b500036 5075#LOG_LEVEL=1
5ea6e9c9 5076
251d1c5c 5077#AUTO_UPGRADE=\"1\"
89002ed2 5078
569d6c55 5079#NO_TIMESTAMP=1
5b771039 5080
d5ec5f80 5081 " >"$ACCOUNT_CONF_PATH"
4c3b3608 5082 fi
5083}
5084
c8e9a31e 5085# nocron
c60883ef 5086_precheck() {
c8e9a31e 5087 _nocron="$1"
4c2a3841 5088
5089 if ! _exists "curl" && ! _exists "wget"; then
c60883ef 5090 _err "Please install curl or wget first, we need to access http resources."
4c3b3608 5091 return 1
5092 fi
4c2a3841 5093
5094 if [ -z "$_nocron" ]; then
415f375c 5095 if ! _exists "crontab" && ! _exists "fcrontab"; then
c8e9a31e 5096 _err "It is recommended to install crontab first. try to install 'cron, crontab, crontabs or vixie-cron'."
5097 _err "We need to set cron job to renew the certs automatically."
5098 _err "Otherwise, your certs will not be able to be renewed automatically."
4c2a3841 5099 if [ -z "$FORCE" ]; then
c8e9a31e 5100 _err "Please add '--force' and try install again to go without crontab."
5101 _err "./$PROJECT_ENTRY --install --force"
5102 return 1
5103 fi
77546ea5 5104 fi
4c3b3608 5105 fi
4c2a3841 5106
d8ba26e6 5107 if ! _exists "${ACME_OPENSSL_BIN:-openssl}"; then
851fedf7 5108 _err "Please install openssl first. ACME_OPENSSL_BIN=$ACME_OPENSSL_BIN"
c60883ef 5109 _err "We need openssl to generate keys."
4c3b3608 5110 return 1
5111 fi
4c2a3841 5112
3794b5cb 5113 if ! _exists "socat"; then
5114 _err "It is recommended to install socat first."
5115 _err "We use socat for standalone server if you use standalone mode."
c60883ef 5116 _err "If you don't use standalone mode, just ignore this warning."
5117 fi
4c2a3841 5118
c60883ef 5119 return 0
5120}
5121
0a7c9364 5122_setShebang() {
5123 _file="$1"
5124 _shebang="$2"
4c2a3841 5125 if [ -z "$_shebang" ]; then
43822d37 5126 _usage "Usage: file shebang"
0a7c9364 5127 return 1
5128 fi
5129 cp "$_file" "$_file.tmp"
4c2a3841 5130 echo "$_shebang" >"$_file"
5131 sed -n 2,99999p "$_file.tmp" >>"$_file"
5132 rm -f "$_file.tmp"
0a7c9364 5133}
5134
27dbe77f 5135#confighome
94dc5f33 5136_installalias() {
27dbe77f 5137 _c_home="$1"
94dc5f33 5138 _initpath
5139
5140 _envfile="$LE_WORKING_DIR/$PROJECT_ENTRY.env"
4c2a3841 5141 if [ "$_upgrading" ] && [ "$_upgrading" = "1" ]; then
44edb2bd 5142 echo "$(cat "$_envfile")" | sed "s|^LE_WORKING_DIR.*$||" >"$_envfile"
5143 echo "$(cat "$_envfile")" | sed "s|^alias le.*$||" >"$_envfile"
5144 echo "$(cat "$_envfile")" | sed "s|^alias le.sh.*$||" >"$_envfile"
94dc5f33 5145 fi
5146
27dbe77f 5147 if [ "$_c_home" ]; then
be83a6a3 5148 _c_entry=" --config-home '$_c_home'"
27dbe77f 5149 fi
5150
1786a5e5 5151 _setopt "$_envfile" "export LE_WORKING_DIR" "=" "\"$LE_WORKING_DIR\""
f5b546b3 5152 if [ "$_c_home" ]; then
5153 _setopt "$_envfile" "export LE_CONFIG_HOME" "=" "\"$LE_CONFIG_HOME\""
d04434e3 5154 else
5155 _sed_i "/^export LE_CONFIG_HOME/d" "$_envfile"
f5b546b3 5156 fi
be83a6a3 5157 _setopt "$_envfile" "alias $PROJECT_ENTRY" "=" "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\""
94dc5f33 5158
5159 _profile="$(_detect_profile)"
4c2a3841 5160 if [ "$_profile" ]; then
94dc5f33 5161 _debug "Found profile: $_profile"
aba5c634 5162 _info "Installing alias to '$_profile'"
94dc5f33 5163 _setopt "$_profile" ". \"$_envfile\""
5164 _info "OK, Close and reopen your terminal to start using $PROJECT_NAME"
5165 else
5166 _info "No profile is found, you will need to go into $LE_WORKING_DIR to use $PROJECT_NAME"
5167 fi
94dc5f33 5168
5169 #for csh
5170 _cshfile="$LE_WORKING_DIR/$PROJECT_ENTRY.csh"
94dc5f33 5171 _csh_profile="$HOME/.cshrc"
4c2a3841 5172 if [ -f "$_csh_profile" ]; then
aba5c634 5173 _info "Installing alias to '$_csh_profile'"
6626371d 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\""
d04434e3 5177 else
5178 _sed_i "/^setenv LE_CONFIG_HOME/d" "$_cshfile"
f5b546b3 5179 fi
be83a6a3 5180 _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\""
4c2a3841 5181 _setopt "$_csh_profile" "source \"$_cshfile\""
94dc5f33 5182 fi
4c2a3841 5183
acafa585 5184 #for tcsh
5185 _tcsh_profile="$HOME/.tcshrc"
4c2a3841 5186 if [ -f "$_tcsh_profile" ]; then
aba5c634 5187 _info "Installing alias to '$_tcsh_profile'"
acafa585 5188 _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\""
f5b546b3 5189 if [ "$_c_home" ]; then
5190 _setopt "$_cshfile" "setenv LE_CONFIG_HOME" " " "\"$LE_CONFIG_HOME\""
5191 fi
be83a6a3 5192 _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\""
4c2a3841 5193 _setopt "$_tcsh_profile" "source \"$_cshfile\""
acafa585 5194 fi
94dc5f33 5195
5196}
5197
86ef0a26 5198# nocron confighome noprofile
c60883ef 5199install() {
f3e4cea3 5200
4c2a3841 5201 if [ -z "$LE_WORKING_DIR" ]; then
f3e4cea3 5202 LE_WORKING_DIR="$DEFAULT_INSTALL_HOME"
5203 fi
4c2a3841 5204
c8e9a31e 5205 _nocron="$1"
27dbe77f 5206 _c_home="$2"
86ef0a26 5207 _noprofile="$3"
4c2a3841 5208 if ! _initpath; then
c60883ef 5209 _err "Install failed."
4c3b3608 5210 return 1
5211 fi
4c2a3841 5212 if [ "$_nocron" ]; then
52677b0a 5213 _debug "Skip install cron job"
5214 fi
4c2a3841 5215
4356eefb 5216 if [ "$IN_CRON" != "1" ]; then
5217 if ! _precheck "$_nocron"; then
5218 _err "Pre-check failed, can not install."
5219 return 1
5220 fi
4c3b3608 5221 fi
4c2a3841 5222
8e845d9f 5223 if [ -z "$_c_home" ] && [ "$LE_CONFIG_HOME" != "$LE_WORKING_DIR" ]; then
5224 _info "Using config home: $LE_CONFIG_HOME"
5225 _c_home="$LE_CONFIG_HOME"
5226 fi
5227
6cc11ffb 5228 #convert from le
4c2a3841 5229 if [ -d "$HOME/.le" ]; then
5230 for envfile in "le.env" "le.sh.env"; do
5231 if [ -f "$HOME/.le/$envfile" ]; then
5232 if grep "le.sh" "$HOME/.le/$envfile" >/dev/null; then
5233 _upgrading="1"
5234 _info "You are upgrading from le.sh"
5235 _info "Renaming \"$HOME/.le\" to $LE_WORKING_DIR"
5236 mv "$HOME/.le" "$LE_WORKING_DIR"
5237 mv "$LE_WORKING_DIR/$envfile" "$LE_WORKING_DIR/$PROJECT_ENTRY.env"
5238 break
6cc11ffb 5239 fi
5240 fi
5241 done
5242 fi
5243
4c3b3608 5244 _info "Installing to $LE_WORKING_DIR"
635695ec 5245
d04434e3 5246 if [ ! -d "$LE_WORKING_DIR" ]; then
5247 if ! mkdir -p "$LE_WORKING_DIR"; then
5248 _err "Can not create working dir: $LE_WORKING_DIR"
5249 return 1
5250 fi
5251
5252 chmod 700 "$LE_WORKING_DIR"
4a0f23e2 5253 fi
4c2a3841 5254
d04434e3 5255 if [ ! -d "$LE_CONFIG_HOME" ]; then
5256 if ! mkdir -p "$LE_CONFIG_HOME"; then
5257 _err "Can not create config dir: $LE_CONFIG_HOME"
5258 return 1
5259 fi
762978f8 5260
d04434e3 5261 chmod 700 "$LE_CONFIG_HOME"
27dbe77f 5262 fi
5263
d5ec5f80 5264 cp "$PROJECT_ENTRY" "$LE_WORKING_DIR/" && chmod +x "$LE_WORKING_DIR/$PROJECT_ENTRY"
4c3b3608 5265
4c2a3841 5266 if [ "$?" != "0" ]; then
a7b7355d 5267 _err "Install failed, can not copy $PROJECT_ENTRY"
4c3b3608 5268 return 1
5269 fi
5270
a7b7355d 5271 _info "Installed to $LE_WORKING_DIR/$PROJECT_ENTRY"
4c3b3608 5272
86ef0a26 5273 if [ "$IN_CRON" != "1" ] && [ -z "$_noprofile" ]; then
4356eefb 5274 _installalias "$_c_home"
5275 fi
4c3b3608 5276
4c2a3841 5277 for subf in $_SUB_FOLDERS; do
5278 if [ -d "$subf" ]; then
d5ec5f80 5279 mkdir -p "$LE_WORKING_DIR/$subf"
5280 cp "$subf"/* "$LE_WORKING_DIR"/"$subf"/
a61fe418 5281 fi
5282 done
5283
4c2a3841 5284 if [ ! -f "$ACCOUNT_CONF_PATH" ]; then
4c3b3608 5285 _initconf
5286 fi
6cc11ffb 5287
4c2a3841 5288 if [ "$_DEFAULT_ACCOUNT_CONF_PATH" != "$ACCOUNT_CONF_PATH" ]; then
635695ec 5289 _setopt "$_DEFAULT_ACCOUNT_CONF_PATH" "ACCOUNT_CONF_PATH" "=" "\"$ACCOUNT_CONF_PATH\""
6cc11ffb 5290 fi
5291
4c2a3841 5292 if [ "$_DEFAULT_CERT_HOME" != "$CERT_HOME" ]; then
b2817897 5293 _saveaccountconf "CERT_HOME" "$CERT_HOME"
5294 fi
5295
4c2a3841 5296 if [ "$_DEFAULT_ACCOUNT_KEY_PATH" != "$ACCOUNT_KEY_PATH" ]; then
b2817897 5297 _saveaccountconf "ACCOUNT_KEY_PATH" "$ACCOUNT_KEY_PATH"
5298 fi
4c2a3841 5299
5300 if [ -z "$_nocron" ]; then
27dbe77f 5301 installcronjob "$_c_home"
c8e9a31e 5302 fi
0a7c9364 5303
4c2a3841 5304 if [ -z "$NO_DETECT_SH" ]; then
641989fd 5305 #Modify shebang
4c2a3841 5306 if _exists bash; then
694af4ae 5307 _bash_path="$(bash -c "command -v bash 2>/dev/null")"
5308 if [ -z "$_bash_path" ]; then
5309 _bash_path="$(bash -c 'echo $SHELL')"
5310 fi
5311 fi
5312 if [ "$_bash_path" ]; then
329174b6 5313 _info "Good, bash is found, so change the shebang to use bash as preferred."
694af4ae 5314 _shebang='#!'"$_bash_path"
641989fd 5315 _setShebang "$LE_WORKING_DIR/$PROJECT_ENTRY" "$_shebang"
4c2a3841 5316 for subf in $_SUB_FOLDERS; do
5317 if [ -d "$LE_WORKING_DIR/$subf" ]; then
5318 for _apifile in "$LE_WORKING_DIR/$subf/"*.sh; do
a61fe418 5319 _setShebang "$_apifile" "$_shebang"
5320 done
5321 fi
5322 done
0a7c9364 5323 fi
5324 fi
5325
4c3b3608 5326 _info OK
5327}
5328
52677b0a 5329# nocron
4c3b3608 5330uninstall() {
52677b0a 5331 _nocron="$1"
4c2a3841 5332 if [ -z "$_nocron" ]; then
52677b0a 5333 uninstallcronjob
5334 fi
4c3b3608 5335 _initpath
5336
9aa3be7f 5337 _uninstallalias
4c2a3841 5338
d5ec5f80 5339 rm -f "$LE_WORKING_DIR/$PROJECT_ENTRY"
f5b546b3 5340 _info "The keys and certs are in \"$(__green "$LE_CONFIG_HOME")\", you can remove them by yourself."
9aa3be7f 5341
5342}
5343
5344_uninstallalias() {
5345 _initpath
5346
4c3b3608 5347 _profile="$(_detect_profile)"
4c2a3841 5348 if [ "$_profile" ]; then
9aa3be7f 5349 _info "Uninstalling alias from: '$_profile'"
d5ec5f80 5350 text="$(cat "$_profile")"
4c2a3841 5351 echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.env\"$||" >"$_profile"
4c3b3608 5352 fi
5353
94dc5f33 5354 _csh_profile="$HOME/.cshrc"
4c2a3841 5355 if [ -f "$_csh_profile" ]; then
9aa3be7f 5356 _info "Uninstalling alias from: '$_csh_profile'"
d5ec5f80 5357 text="$(cat "$_csh_profile")"
4c2a3841 5358 echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" >"$_csh_profile"
94dc5f33 5359 fi
4c2a3841 5360
acafa585 5361 _tcsh_profile="$HOME/.tcshrc"
4c2a3841 5362 if [ -f "$_tcsh_profile" ]; then
9aa3be7f 5363 _info "Uninstalling alias from: '$_csh_profile'"
d5ec5f80 5364 text="$(cat "$_tcsh_profile")"
4c2a3841 5365 echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" >"$_tcsh_profile"
acafa585 5366 fi
4c3b3608 5367
5368}
5369
5370cron() {
c73fdd40 5371 export IN_CRON=1
89002ed2 5372 _initpath
d8ba26e6 5373 _info "$(__green "===Starting cron===")"
4c2a3841 5374 if [ "$AUTO_UPGRADE" = "1" ]; then
89002ed2 5375 export LE_WORKING_DIR
5376 (
4c2a3841 5377 if ! upgrade; then
5378 _err "Cron:Upgrade failed!"
5379 return 1
5380 fi
89002ed2 5381 )
d5ec5f80 5382 . "$LE_WORKING_DIR/$PROJECT_ENTRY" >/dev/null
1ab63043 5383
4c2a3841 5384 if [ -t 1 ]; then
1ab63043 5385 __INTERACTIVE="1"
5386 fi
4c2a3841 5387
89002ed2 5388 _info "Auto upgraded to: $VER"
5389 fi
4c3b3608 5390 renewAll
cc179731 5391 _ret="$?"
281aa349 5392 IN_CRON=""
d8ba26e6 5393 _info "$(__green "===End cron===")"
0ba95a3d 5394 exit $_ret
4c3b3608 5395}
5396
5397version() {
a63b05a9 5398 echo "$PROJECT"
5399 echo "v$VER"
4c3b3608 5400}
5401
5402showhelp() {
d0871bda 5403 _initpath
4c3b3608 5404 version
a7b7355d 5405 echo "Usage: $PROJECT_ENTRY command ...[parameters]....
a63b05a9 5406Commands:
5407 --help, -h Show this help message.
5408 --version, -v Show version info.
a7b7355d 5409 --install Install $PROJECT_NAME to your system.
5410 --uninstall Uninstall $PROJECT_NAME, and uninstall the cron job.
d8beaf72 5411 --upgrade Upgrade $PROJECT_NAME to the latest code from $PROJECT.
a63b05a9 5412 --issue Issue a cert.
10afcaca 5413 --signcsr Issue a cert from an existing csr.
a61fe418 5414 --deploy Deploy the cert to your server.
27dbe77f 5415 --install-cert Install the issued cert to apache/nginx or any other server.
a63b05a9 5416 --renew, -r Renew a cert.
27dbe77f 5417 --renew-all Renew all the certs.
a63b05a9 5418 --revoke Revoke a cert.
47b49f1b 5419 --remove Remove the cert from list of certs known to $PROJECT_NAME.
10afcaca 5420 --list List all the certs.
5421 --showcsr Show the content of a csr.
27dbe77f 5422 --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.
5423 --uninstall-cronjob Uninstall the cron job. The 'uninstall' command can do this automatically.
a63b05a9 5424 --cron Run cron job to renew all the certs.
5425 --toPkcs Export the certificate and key to a pfx file.
4410226d 5426 --toPkcs8 Convert to pkcs8 format.
27dbe77f 5427 --update-account Update account info.
5428 --register-account Register account key.
422dd1fa 5429 --deactivate-account Deactivate the account.
0984585d 5430 --create-account-key Create an account private key, professional use.
5431 --create-domain-key Create an domain private key, professional use.
a63b05a9 5432 --createCSR, -ccsr Create CSR , professional use.
0c00e870 5433 --deactivate Deactivate the domain authz, professional use.
3c07f57a 5434
a63b05a9 5435Parameters:
5436 --domain, -d domain.tld Specifies a domain, used to issue, renew or revoke etc.
64821ad4 5437 --challenge-alias domain.tld The challenge domain alias for DNS alias mode: $_DNS_ALIAS_WIKI
5438 --domain-alias domain.tld The domain alias for DNS alias mode: $_DNS_ALIAS_WIKI
a63b05a9 5439 --force, -f Used to force to install or force to renew a cert immediately.
5440 --staging, --test Use staging server, just for test.
5441 --debug Output debug info.
e6e85b0c 5442 --output-insecure Output all the sensitive messages. By default all the credentials/sensitive messages are hidden from the output/debug/log for secure.
a63b05a9 5443 --webroot, -w /path/to/webroot Specifies the web root folder for web root mode.
5444 --standalone Use standalone mode.
0e44f587 5445 --stateless Use stateless mode, see: $_STATELESS_WIKI
a63b05a9 5446 --apache Use apache mode.
eccec5f6 5447 --dns [dns_cf|dns_dp|dns_cx|/path/to/api/file] Use dns mode or dns api.
4a4dacb5 5448 --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 5449
a63b05a9 5450 --keylength, -k [2048] Specifies the domain key length: 2048, 3072, 4096, 8192 or ec-256, ec-384.
5451 --accountkeylength, -ak [2048] Specifies the account key length.
d0871bda 5452 --log [/path/to/logfile] Specifies the log file. The default is: \"$DEFAULT_LOG_FILE\" if you don't give a file path here.
a73c5b33 5453 --log-level 1|2 Specifies the log level, default is 1.
52765466 5454 --syslog [0|3|6|7] Syslog level, 0: disable syslog, 3: error, 6: info, 7: debug.
3c07f57a 5455
a63b05a9 5456 These parameters are to install the cert to nginx/apache or anyother server after issue/renew a cert:
3c07f57a 5457
13fe54c9 5458 --cert-file After issue/renew, the cert will be copied to this path.
5459 --key-file After issue/renew, the key will be copied to this path.
5460 --ca-file After issue/renew, the intermediate cert will be copied to this path.
5461 --fullchain-file After issue/renew, the fullchain cert will be copied to this path.
3c07f57a 5462
a63b05a9 5463 --reloadcmd \"service nginx reload\" After issue/renew, it's used to reload the server.
5464
48d9a8c1 5465 --server SERVER ACME Directory Resource URI. (default: https://acme-v01.api.letsencrypt.org/directory)
a63b05a9 5466 --accountconf Specifies a customized account config file.
635695ec 5467 --home Specifies the home dir for $PROJECT_NAME .
27dbe77f 5468 --cert-home Specifies the home dir to save all the certs, only valid for '--install' command.
5469 --config-home Specifies the home dir to save all the configurations.
635695ec 5470 --useragent Specifies the user agent string. it will be saved for future use too.
9082862b
C
5471 --accountemail Specifies the account email, only valid for the '--install' and '--update-account' command.
5472 --accountkey Specifies the account key path, only valid for the '--install' command.
523c7682 5473 --days Specifies the days to renew the cert when using '--issue' command. The max value is $MAX_RENEW days.
39c8f79f 5474 --httpport Specifies the standalone listening port. Only valid if the server is behind a reverse proxy or load balancer.
6ae0f7f5 5475 --local-address Specifies the standalone/tls server listening address, in case you have multiple ip addresses.
dcf4f8f6 5476 --listraw Only used for '--list' command, list the certs in raw format.
27dbe77f 5477 --stopRenewOnError, -se Only valid for '--renew-all' command. Stop if one cert has error in renewal.
13d7cae9 5478 --insecure Do not check the server certificate, in some devices, the api server's certificate may not be trusted.
8f73e241 5479 --ca-bundle Specifies the path to the CA certificate bundle to verify api server's certificate.
13fe54c9 5480 --ca-path Specifies directory containing CA certificates in PEM format, used by wget or curl.
bc96082f 5481 --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 5482 --no-color Do not output color text.
27dbe77f 5483 --ecc Specifies to use the ECC cert. Valid for '--install-cert', '--renew', '--revoke', '--toPkcs' and '--createCSR'
10afcaca 5484 --csr Specifies the input csr.
b0070f03 5485 --pre-hook Command to be run before obtaining any certificates.
84a6730b 5486 --post-hook Command to be run after attempting to obtain/renew certificates. No matter the obtain/renew is success or failed.
b0070f03 5487 --renew-hook Command to be run once for each successfully renewed certificate.
a61fe418 5488 --deploy-hook The hook file to deploy cert
0c9546cc 5489 --ocsp-must-staple, --ocsp Generate ocsp must Staple extension.
c4b2e582 5490 --always-force-new-domain-key Generate new domain key when renewal. Otherwise, the domain key is not changed by default.
6bf281f9 5491 --auto-upgrade [0|1] Valid for '--upgrade' command, indicating whether to upgrade automatically in future.
6ae0f7f5 5492 --listen-v4 Force standalone/tls server to listen at ipv4.
5493 --listen-v6 Force standalone/tls server to listen at ipv6.
a746139c 5494 --openssl-bin Specifies a custom openssl bin location.
9b124070 5495 --use-wget Force to use wget, if you have both curl and wget installed.
4c3b3608 5496 "
5497}
5498
86ef0a26 5499# nocron noprofile
4a0f23e2 5500_installOnline() {
5501 _info "Installing from online archive."
52677b0a 5502 _nocron="$1"
86ef0a26 5503 _noprofile="$2"
4c2a3841 5504 if [ ! "$BRANCH" ]; then
4a0f23e2 5505 BRANCH="master"
5506 fi
a8df88ab 5507
4a0f23e2 5508 target="$PROJECT/archive/$BRANCH.tar.gz"
5509 _info "Downloading $target"
5510 localname="$BRANCH.tar.gz"
4c2a3841 5511 if ! _get "$target" >$localname; then
df9547ae 5512 _err "Download error."
4a0f23e2 5513 return 1
5514 fi
0bbe6eef 5515 (
4c2a3841 5516 _info "Extracting $localname"
3a3b0dd5 5517 if ! (tar xzf $localname || gtar xzf $localname); then
5518 _err "Extraction error."
5519 exit 1
5520 fi
4c2a3841 5521
5522 cd "$PROJECT_NAME-$BRANCH"
5523 chmod +x $PROJECT_ENTRY
86ef0a26 5524 if ./$PROJECT_ENTRY install "$_nocron" "" "$_noprofile"; then
4c2a3841 5525 _info "Install success!"
5526 fi
5527
5528 cd ..
5529
5530 rm -rf "$PROJECT_NAME-$BRANCH"
5531 rm -f "$localname"
0bbe6eef 5532 )
4a0f23e2 5533}
5534
52677b0a 5535upgrade() {
5536 if (
267f283a 5537 _initpath
5538 export LE_WORKING_DIR
d0b748a4 5539 cd "$LE_WORKING_DIR"
86ef0a26 5540 _installOnline "nocron" "noprofile"
4c2a3841 5541 ); then
52677b0a 5542 _info "Upgrade success!"
096d8992 5543 exit 0
52677b0a 5544 else
5545 _err "Upgrade failed!"
096d8992 5546 exit 1
52677b0a 5547 fi
5548}
a63b05a9 5549
5ea6e9c9 5550_processAccountConf() {
4c2a3841 5551 if [ "$_useragent" ]; then
5ea6e9c9 5552 _saveaccountconf "USER_AGENT" "$_useragent"
4c2a3841 5553 elif [ "$USER_AGENT" ] && [ "$USER_AGENT" != "$DEFAULT_USER_AGENT" ]; then
d0871bda 5554 _saveaccountconf "USER_AGENT" "$USER_AGENT"
5ea6e9c9 5555 fi
4c2a3841 5556
5557 if [ "$_accountemail" ]; then
5ea6e9c9 5558 _saveaccountconf "ACCOUNT_EMAIL" "$_accountemail"
4c2a3841 5559 elif [ "$ACCOUNT_EMAIL" ] && [ "$ACCOUNT_EMAIL" != "$DEFAULT_ACCOUNT_EMAIL" ]; then
d0871bda 5560 _saveaccountconf "ACCOUNT_EMAIL" "$ACCOUNT_EMAIL"
5ea6e9c9 5561 fi
4c2a3841 5562
a746139c 5563 if [ "$_openssl_bin" ]; then
851fedf7 5564 _saveaccountconf "ACME_OPENSSL_BIN" "$_openssl_bin"
5565 elif [ "$ACME_OPENSSL_BIN" ] && [ "$ACME_OPENSSL_BIN" != "$DEFAULT_OPENSSL_BIN" ]; then
5566 _saveaccountconf "ACME_OPENSSL_BIN" "$ACME_OPENSSL_BIN"
a746139c 5567 fi
5568
4c2a3841 5569 if [ "$_auto_upgrade" ]; then
6bf281f9 5570 _saveaccountconf "AUTO_UPGRADE" "$_auto_upgrade"
4c2a3841 5571 elif [ "$AUTO_UPGRADE" ]; then
6bf281f9 5572 _saveaccountconf "AUTO_UPGRADE" "$AUTO_UPGRADE"
5573 fi
4c2a3841 5574
9b124070 5575 if [ "$_use_wget" ]; then
5576 _saveaccountconf "ACME_USE_WGET" "$_use_wget"
5577 elif [ "$ACME_USE_WGET" ]; then
5578 _saveaccountconf "ACME_USE_WGET" "$ACME_USE_WGET"
5579 fi
5580
5ea6e9c9 5581}
5582
a63b05a9 5583_process() {
5584 _CMD=""
5585 _domain=""
3f4513b3 5586 _altdomains="$NO_VALUE"
a63b05a9 5587 _webroot=""
875625b1 5588 _challenge_alias=""
bdbf323f 5589 _keylength=""
5590 _accountkeylength=""
5c539af7 5591 _cert_file=""
5592 _key_file=""
5593 _ca_file=""
5594 _fullchain_file=""
4d2f38b0 5595 _reloadcmd=""
a63b05a9 5596 _password=""
635695ec 5597 _accountconf=""
5598 _useragent=""
b5eb4b90 5599 _accountemail=""
5600 _accountkey=""
b2817897 5601 _certhome=""
27dbe77f 5602 _confighome=""
39c8f79f 5603 _httpport=""
e22bcf7c 5604 _tlsport=""
0e38c60d 5605 _dnssleep=""
dcf4f8f6 5606 _listraw=""
cc179731 5607 _stopRenewOnError=""
e3698edd 5608 #_insecure=""
78009539 5609 _ca_bundle=""
2aa75f03 5610 _ca_path=""
c8e9a31e 5611 _nocron=""
43822d37 5612 _ecc=""
10afcaca 5613 _csr=""
b0070f03 5614 _pre_hook=""
5615 _post_hook=""
5616 _renew_hook=""
a61fe418 5617 _deploy_hook=""
5ea6e9c9 5618 _logfile=""
d0871bda 5619 _log=""
0463b5d6 5620 _local_address=""
a73c5b33 5621 _log_level=""
6bf281f9 5622 _auto_upgrade=""
6ae0f7f5 5623 _listen_v4=""
5624 _listen_v6=""
a746139c 5625 _openssl_bin=""
e2edf208 5626 _syslog=""
9b124070 5627 _use_wget=""
98394f99 5628 _server=""
4c2a3841 5629 while [ ${#} -gt 0 ]; do
a63b05a9 5630 case "${1}" in
4c2a3841 5631
5632 --help | -h)
a63b05a9 5633 showhelp
5634 return
5635 ;;
4c2a3841 5636 --version | -v)
a63b05a9 5637 version
5638 return
5639 ;;
4c2a3841 5640 --install)
a63b05a9 5641 _CMD="install"
5642 ;;
4c2a3841 5643 --uninstall)
a63b05a9 5644 _CMD="uninstall"
5645 ;;
4c2a3841 5646 --upgrade)
52677b0a 5647 _CMD="upgrade"
5648 ;;
4c2a3841 5649 --issue)
a63b05a9 5650 _CMD="issue"
5651 ;;
4c2a3841 5652 --deploy)
a61fe418 5653 _CMD="deploy"
5654 ;;
4c2a3841 5655 --signcsr)
10afcaca 5656 _CMD="signcsr"
5657 ;;
4c2a3841 5658 --showcsr)
10afcaca 5659 _CMD="showcsr"
5660 ;;
db7e4bf9 5661 --installcert | -i | --install-cert)
a63b05a9 5662 _CMD="installcert"
5663 ;;
4c2a3841 5664 --renew | -r)
a63b05a9 5665 _CMD="renew"
5666 ;;
db7e4bf9 5667 --renewAll | --renewall | --renew-all)
a63b05a9 5668 _CMD="renewAll"
5669 ;;
4c2a3841 5670 --revoke)
a63b05a9 5671 _CMD="revoke"
5672 ;;
78f0201d 5673 --remove)
5674 _CMD="remove"
5675 ;;
4c2a3841 5676 --list)
6d7eda3e 5677 _CMD="list"
5678 ;;
ee20015d 5679 --installcronjob | --install-cronjob)
a63b05a9 5680 _CMD="installcronjob"
5681 ;;
db7e4bf9 5682 --uninstallcronjob | --uninstall-cronjob)
a63b05a9 5683 _CMD="uninstallcronjob"
5684 ;;
4c2a3841 5685 --cron)
a63b05a9 5686 _CMD="cron"
5687 ;;
4c2a3841 5688 --toPkcs)
a63b05a9 5689 _CMD="toPkcs"
4c2a3841 5690 ;;
4410226d 5691 --toPkcs8)
5692 _CMD="toPkcs8"
342128a4 5693 ;;
0984585d 5694 --createAccountKey | --createaccountkey | -cak | --create-account-key)
a63b05a9 5695 _CMD="createAccountKey"
5696 ;;
0984585d 5697 --createDomainKey | --createdomainkey | -cdk | --create-domain-key)
a63b05a9 5698 _CMD="createDomainKey"
5699 ;;
4c2a3841 5700 --createCSR | --createcsr | -ccr)
a63b05a9 5701 _CMD="createCSR"
5702 ;;
4c2a3841 5703 --deactivate)
0c00e870 5704 _CMD="deactivate"
5705 ;;
ee20015d 5706 --updateaccount | --update-account)
eb59817e 5707 _CMD="updateaccount"
5708 ;;
ee20015d 5709 --registeraccount | --register-account)
eb59817e 5710 _CMD="registeraccount"
5711 ;;
422dd1fa 5712 --deactivate-account)
5713 _CMD="deactivateaccount"
5714 ;;
4c2a3841 5715 --domain | -d)
a63b05a9 5716 _dvalue="$2"
4c2a3841 5717
5718 if [ "$_dvalue" ]; then
5719 if _startswith "$_dvalue" "-"; then
ee1737a5 5720 _err "'$_dvalue' is not a valid domain for parameter '$1'"
5721 return 1
5722 fi
4c2a3841 5723 if _is_idn "$_dvalue" && ! _exists idn; then
9774b01b 5724 _err "It seems that $_dvalue is an IDN( Internationalized Domain Names), please install 'idn' command first."
5725 return 1
5726 fi
4c2a3841 5727
9e9f839d 5728 if _startswith "$_dvalue" "*."; then
5729 _debug "Wildcard domain"
5730 export ACME_VERSION=2
5731 fi
4c2a3841 5732 if [ -z "$_domain" ]; then
ee1737a5 5733 _domain="$_dvalue"
a63b05a9 5734 else
4c2a3841 5735 if [ "$_altdomains" = "$NO_VALUE" ]; then
ee1737a5 5736 _altdomains="$_dvalue"
5737 else
5738 _altdomains="$_altdomains,$_dvalue"
5739 fi
a63b05a9 5740 fi
5741 fi
4c2a3841 5742
a63b05a9 5743 shift
5744 ;;
5745
4c2a3841 5746 --force | -f)
a63b05a9 5747 FORCE="1"
5748 ;;
4c2a3841 5749 --staging | --test)
a63b05a9 5750 STAGE="1"
5751 ;;
48d9a8c1 5752 --server)
5753 ACME_DIRECTORY="$2"
98394f99 5754 _server="$ACME_DIRECTORY"
48d9a8c1 5755 export ACME_DIRECTORY
5756 shift
5757 ;;
4c2a3841 5758 --debug)
5759 if [ -z "$2" ] || _startswith "$2" "-"; then
fc6cf4d9 5760 DEBUG="$DEBUG_LEVEL_DEFAULT"
a63b05a9 5761 else
5762 DEBUG="$2"
5763 shift
4c2a3841 5764 fi
a63b05a9 5765 ;;
e6e85b0c 5766 --output-insecure)
5767 export OUTPUT_INSECURE=1
5768 ;;
4c2a3841 5769 --webroot | -w)
a63b05a9 5770 wvalue="$2"
4c2a3841 5771 if [ -z "$_webroot" ]; then
a63b05a9 5772 _webroot="$wvalue"
5773 else
5774 _webroot="$_webroot,$wvalue"
5775 fi
5776 shift
4c2a3841 5777 ;;
875625b1 5778 --challenge-alias)
5779 cvalue="$2"
5780 _challenge_alias="$_challenge_alias$cvalue,"
5781 shift
5782 ;;
64821ad4 5783 --domain-alias)
5784 cvalue="$DNS_ALIAS_PREFIX$2"
5785 _challenge_alias="$_challenge_alias$cvalue,"
5786 shift
1f7df33e 5787 ;;
4c2a3841 5788 --standalone)
3f4513b3 5789 wvalue="$NO_VALUE"
4c2a3841 5790 if [ -z "$_webroot" ]; then
a63b05a9 5791 _webroot="$wvalue"
5792 else
5793 _webroot="$_webroot,$wvalue"
5794 fi
5795 ;;
0e44f587 5796 --stateless)
5797 wvalue="$MODE_STATELESS"
5798 if [ -z "$_webroot" ]; then
5799 _webroot="$wvalue"
5800 else
5801 _webroot="$_webroot,$wvalue"
5802 fi
5803 ;;
4c2a3841 5804 --local-address)
0463b5d6 5805 lvalue="$2"
5806 _local_address="$_local_address$lvalue,"
5807 shift
5808 ;;
4c2a3841 5809 --apache)
a63b05a9 5810 wvalue="apache"
4c2a3841 5811 if [ -z "$_webroot" ]; then
a63b05a9 5812 _webroot="$wvalue"
5813 else
5814 _webroot="$_webroot,$wvalue"
5815 fi
5816 ;;
9d725af6 5817 --nginx)
5818 wvalue="$NGINX"
5819 if [ -z "$_webroot" ]; then
5820 _webroot="$wvalue"
5821 else
5822 _webroot="$_webroot,$wvalue"
5823 fi
5824 ;;
4c2a3841 5825 --dns)
3881f221 5826 wvalue="$W_DNS"
a5c56c54 5827 if [ "$2" ] && ! _startswith "$2" "-"; then
a63b05a9 5828 wvalue="$2"
5829 shift
5830 fi
4c2a3841 5831 if [ -z "$_webroot" ]; then
a63b05a9 5832 _webroot="$wvalue"
5833 else
5834 _webroot="$_webroot,$wvalue"
5835 fi
5836 ;;
4c2a3841 5837 --dnssleep)
0e38c60d 5838 _dnssleep="$2"
5839 Le_DNSSleep="$_dnssleep"
5840 shift
5841 ;;
4c2a3841 5842
5843 --keylength | -k)
a63b05a9 5844 _keylength="$2"
a63b05a9 5845 shift
5846 ;;
4c2a3841 5847 --accountkeylength | -ak)
2ce87fe2 5848 _accountkeylength="$2"
a63b05a9 5849 shift
5850 ;;
5851
5c539af7 5852 --cert-file | --certpath)
5853 _cert_file="$2"
a63b05a9 5854 shift
5855 ;;
5c539af7 5856 --key-file | --keypath)
5857 _key_file="$2"
a63b05a9 5858 shift
5859 ;;
5c539af7 5860 --ca-file | --capath)
5861 _ca_file="$2"
a63b05a9 5862 shift
5863 ;;
5c539af7 5864 --fullchain-file | --fullchainpath)
5865 _fullchain_file="$2"
a63b05a9 5866 shift
5867 ;;
4c2a3841 5868 --reloadcmd | --reloadCmd)
a63b05a9 5869 _reloadcmd="$2"
5870 shift
5871 ;;
4c2a3841 5872 --password)
a63b05a9 5873 _password="$2"
5874 shift
5875 ;;
4c2a3841 5876 --accountconf)
635695ec 5877 _accountconf="$2"
5878 ACCOUNT_CONF_PATH="$_accountconf"
a7b7355d 5879 shift
a63b05a9 5880 ;;
4c2a3841 5881 --home)
a63b05a9 5882 LE_WORKING_DIR="$2"
a7b7355d 5883 shift
a63b05a9 5884 ;;
ee20015d 5885 --certhome | --cert-home)
b2817897 5886 _certhome="$2"
5887 CERT_HOME="$_certhome"
5888 shift
4c2a3841 5889 ;;
27dbe77f 5890 --config-home)
5891 _confighome="$2"
f5b546b3 5892 LE_CONFIG_HOME="$_confighome"
27dbe77f 5893 shift
5894 ;;
4c2a3841 5895 --useragent)
635695ec 5896 _useragent="$2"
5897 USER_AGENT="$_useragent"
5898 shift
5899 ;;
4c2a3841 5900 --accountemail)
b5eb4b90 5901 _accountemail="$2"
5902 ACCOUNT_EMAIL="$_accountemail"
5903 shift
5904 ;;
4c2a3841 5905 --accountkey)
b5eb4b90 5906 _accountkey="$2"
5907 ACCOUNT_KEY_PATH="$_accountkey"
5908 shift
5909 ;;
4c2a3841 5910 --days)
06625071 5911 _days="$2"
5912 Le_RenewalDays="$_days"
5913 shift
5914 ;;
4c2a3841 5915 --httpport)
39c8f79f 5916 _httpport="$2"
5917 Le_HTTPPort="$_httpport"
5918 shift
5919 ;;
4c2a3841 5920 --listraw)
dcf4f8f6 5921 _listraw="raw"
4c2a3841 5922 ;;
5923 --stopRenewOnError | --stoprenewonerror | -se)
cc179731 5924 _stopRenewOnError="1"
5925 ;;
4c2a3841 5926 --insecure)
e3698edd 5927 #_insecure="1"
fac1e367 5928 HTTPS_INSECURE="1"
13d7cae9 5929 ;;
4c2a3841 5930 --ca-bundle)
78d1cfb4 5931 _ca_bundle="$(_readlink "$2")"
78009539
PS
5932 CA_BUNDLE="$_ca_bundle"
5933 shift
5934 ;;
2aa75f03 5935 --ca-path)
5936 _ca_path="$2"
5937 CA_PATH="$_ca_path"
5938 shift
5939 ;;
4c2a3841 5940 --nocron)
c8e9a31e 5941 _nocron="1"
5942 ;;
08b4e1a7 5943 --no-color)
5944 export ACME_NO_COLOR=1
5945 ;;
4c2a3841 5946 --ecc)
43822d37 5947 _ecc="isEcc"
5948 ;;
4c2a3841 5949 --csr)
10afcaca 5950 _csr="$2"
5951 shift
5952 ;;
4c2a3841 5953 --pre-hook)
b0070f03 5954 _pre_hook="$2"
5955 shift
5956 ;;
4c2a3841 5957 --post-hook)
b0070f03 5958 _post_hook="$2"
5959 shift
5960 ;;
4c2a3841 5961 --renew-hook)
b0070f03 5962 _renew_hook="$2"
5963 shift
5964 ;;
4c2a3841 5965 --deploy-hook)
93bce1b2 5966 if [ -z "$2" ] || _startswith "$2" "-"; then
5967 _usage "Please specify a value for '--deploy-hook'"
5968 return 1
5969 fi
5970 _deploy_hook="$_deploy_hook$2,"
a61fe418 5971 shift
5972 ;;
4c2a3841 5973 --ocsp-must-staple | --ocsp)
96db9362 5974 Le_OCSP_Staple="1"
0c9546cc 5975 ;;
c4b2e582 5976 --always-force-new-domain-key)
5977 if [ -z "$2" ] || _startswith "$2" "-"; then
5978 Le_ForceNewDomainKey=1
5979 else
5980 Le_ForceNewDomainKey="$2"
5981 shift
5982 fi
5983 ;;
4c2a3841 5984 --log | --logfile)
d0871bda 5985 _log="1"
5ea6e9c9 5986 _logfile="$2"
4c2a3841 5987 if _startswith "$_logfile" '-'; then
d0871bda 5988 _logfile=""
5989 else
5990 shift
5991 fi
5ea6e9c9 5992 LOG_FILE="$_logfile"
4c2a3841 5993 if [ -z "$LOG_LEVEL" ]; then
a73c5b33 5994 LOG_LEVEL="$DEFAULT_LOG_LEVEL"
5995 fi
5996 ;;
4c2a3841 5997 --log-level)
30bfc2ce 5998 _log_level="$2"
a73c5b33 5999 LOG_LEVEL="$_log_level"
6000 shift
5ea6e9c9 6001 ;;
e2edf208 6002 --syslog)
6003 if ! _startswith "$2" '-'; then
6004 _syslog="$2"
6005 shift
6006 fi
6007 if [ -z "$_syslog" ]; then
fc6cf4d9 6008 _syslog="$SYSLOG_LEVEL_DEFAULT"
e2edf208 6009 fi
6010 ;;
4c2a3841 6011 --auto-upgrade)
6bf281f9 6012 _auto_upgrade="$2"
4c2a3841 6013 if [ -z "$_auto_upgrade" ] || _startswith "$_auto_upgrade" '-'; then
6bf281f9 6014 _auto_upgrade="1"
6015 else
6016 shift
6017 fi
6018 AUTO_UPGRADE="$_auto_upgrade"
6019 ;;
4c2a3841 6020 --listen-v4)
6ae0f7f5 6021 _listen_v4="1"
6022 Le_Listen_V4="$_listen_v4"
6023 ;;
4c2a3841 6024 --listen-v6)
6ae0f7f5 6025 _listen_v6="1"
6026 Le_Listen_V6="$_listen_v6"
6027 ;;
a746139c 6028 --openssl-bin)
6029 _openssl_bin="$2"
851fedf7 6030 ACME_OPENSSL_BIN="$_openssl_bin"
7c2e8754 6031 shift
a746139c 6032 ;;
9b124070 6033 --use-wget)
6034 _use_wget="1"
6035 ACME_USE_WGET="1"
6036 ;;
4c2a3841 6037 *)
a63b05a9 6038 _err "Unknown parameter : $1"
6039 return 1
6040 ;;
6041 esac
6042
6043 shift 1
6044 done
6045
4c2a3841 6046 if [ "${_CMD}" != "install" ]; then
5ea6e9c9 6047 __initHome
661f0583 6048 if [ "$_log" ]; then
4c2a3841 6049 if [ -z "$_logfile" ]; then
661f0583 6050 _logfile="$DEFAULT_LOG_FILE"
6051 fi
d0871bda 6052 fi
4c2a3841 6053 if [ "$_logfile" ]; then
5ea6e9c9 6054 _saveaccountconf "LOG_FILE" "$_logfile"
661f0583 6055 LOG_FILE="$_logfile"
5ea6e9c9 6056 fi
a73c5b33 6057
4c2a3841 6058 if [ "$_log_level" ]; then
a73c5b33 6059 _saveaccountconf "LOG_LEVEL" "$_log_level"
6060 LOG_LEVEL="$_log_level"
6061 fi
4c2a3841 6062
e2edf208 6063 if [ "$_syslog" ]; then
6064 if _exists logger; then
6065 if [ "$_syslog" = "0" ]; then
6066 _clearaccountconf "SYS_LOG"
6067 else
6068 _saveaccountconf "SYS_LOG" "$_syslog"
6069 fi
6070 SYS_LOG="$_syslog"
6071 else
6072 _err "The 'logger' command is not found, can not enable syslog."
6073 _clearaccountconf "SYS_LOG"
6074 SYS_LOG=""
6075 fi
6076 fi
6077
5ea6e9c9 6078 _processAccountConf
6079 fi
4c2a3841 6080
9d548d81 6081 _debug2 LE_WORKING_DIR "$LE_WORKING_DIR"
4c2a3841 6082
6083 if [ "$DEBUG" ]; then
dcf9cb58 6084 version
98394f99 6085 if [ "$_server" ]; then
6086 _debug "Using server: $_server"
6087 fi
dcf9cb58 6088 fi
a63b05a9 6089
6090 case "${_CMD}" in
27dbe77f 6091 install) install "$_nocron" "$_confighome" ;;
bc96082f 6092 uninstall) uninstall "$_nocron" ;;
52677b0a 6093 upgrade) upgrade ;;
a63b05a9 6094 issue)
875625b1 6095 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 6096 ;;
a61fe418 6097 deploy)
6098 deploy "$_domain" "$_deploy_hook" "$_ecc"
6099 ;;
10afcaca 6100 signcsr)
875625b1 6101 signcsr "$_csr" "$_webroot" "$_cert_file" "$_key_file" "$_ca_file" "$_reloadcmd" "$_fullchain_file" "$_pre_hook" "$_post_hook" "$_renew_hook" "$_local_address" "$_challenge_alias"
10afcaca 6102 ;;
6103 showcsr)
6104 showcsr "$_csr" "$_domain"
6105 ;;
a63b05a9 6106 installcert)
5c539af7 6107 installcert "$_domain" "$_cert_file" "$_key_file" "$_ca_file" "$_reloadcmd" "$_fullchain_file" "$_ecc"
a63b05a9 6108 ;;
4c2a3841 6109 renew)
43822d37 6110 renew "$_domain" "$_ecc"
a63b05a9 6111 ;;
4c2a3841 6112 renewAll)
cc179731 6113 renewAll "$_stopRenewOnError"
a63b05a9 6114 ;;
4c2a3841 6115 revoke)
43822d37 6116 revoke "$_domain" "$_ecc"
a63b05a9 6117 ;;
78f0201d 6118 remove)
6119 remove "$_domain" "$_ecc"
6120 ;;
4c2a3841 6121 deactivate)
3f4513b3 6122 deactivate "$_domain,$_altdomains"
eb59817e 6123 ;;
4c2a3841 6124 registeraccount)
57e58ce7 6125 registeraccount "$_accountkeylength"
eb59817e 6126 ;;
4c2a3841 6127 updateaccount)
eb59817e 6128 updateaccount
6129 ;;
422dd1fa 6130 deactivateaccount)
6131 deactivateaccount
6132 ;;
4c2a3841 6133 list)
dcf4f8f6 6134 list "$_listraw"
6d7eda3e 6135 ;;
27dbe77f 6136 installcronjob) installcronjob "$_confighome" ;;
a63b05a9 6137 uninstallcronjob) uninstallcronjob ;;
6138 cron) cron ;;
4c2a3841 6139 toPkcs)
43822d37 6140 toPkcs "$_domain" "$_password" "$_ecc"
a63b05a9 6141 ;;
4410226d 6142 toPkcs8)
6143 toPkcs8 "$_domain" "$_ecc"
6144 ;;
4c2a3841 6145 createAccountKey)
5fbc47eb 6146 createAccountKey "$_accountkeylength"
a63b05a9 6147 ;;
4c2a3841 6148 createDomainKey)
a63b05a9 6149 createDomainKey "$_domain" "$_keylength"
6150 ;;
4c2a3841 6151 createCSR)
43822d37 6152 createCSR "$_domain" "$_altdomains" "$_ecc"
a63b05a9 6153 ;;
6154
6155 *)
27dbe77f 6156 if [ "$_CMD" ]; then
6157 _err "Invalid command: $_CMD"
6158 fi
4c2a3841 6159 showhelp
a63b05a9 6160 return 1
4c2a3841 6161 ;;
a63b05a9 6162 esac
d3595686 6163 _ret="$?"
4c2a3841 6164 if [ "$_ret" != "0" ]; then
d3595686 6165 return $_ret
6166 fi
4c2a3841 6167
6168 if [ "${_CMD}" = "install" ]; then
6169 if [ "$_log" ]; then
6170 if [ -z "$LOG_FILE" ]; then
d0871bda 6171 LOG_FILE="$DEFAULT_LOG_FILE"
6172 fi
6173 _saveaccountconf "LOG_FILE" "$LOG_FILE"
5ea6e9c9 6174 fi
4c2a3841 6175
6176 if [ "$_log_level" ]; then
a73c5b33 6177 _saveaccountconf "LOG_LEVEL" "$_log_level"
6178 fi
e2edf208 6179
6180 if [ "$_syslog" ]; then
6181 if _exists logger; then
6182 if [ "$_syslog" = "0" ]; then
6183 _clearaccountconf "SYS_LOG"
6184 else
6185 _saveaccountconf "SYS_LOG" "$_syslog"
6186 fi
6187 else
6188 _err "The 'logger' command is not found, can not enable syslog."
6189 _clearaccountconf "SYS_LOG"
6190 SYS_LOG=""
6191 fi
6192 fi
6193
5ea6e9c9 6194 _processAccountConf
b5eb4b90 6195 fi
635695ec 6196
a63b05a9 6197}
6198
4c2a3841 6199if [ "$INSTALLONLINE" ]; then
d1f97fc8 6200 INSTALLONLINE=""
2fbf3991 6201 _installOnline
4a0f23e2 6202 exit
6203fi
4c3b3608 6204
319e0ae3 6205main() {
6206 [ -z "$1" ] && showhelp && return
4c2a3841 6207 if _startswith "$1" '-'; then _process "$@"; else "$@"; fi
319e0ae3 6208}
e69a7c38 6209
aa7b82de 6210main "$@"