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