]> git.proxmox.com Git - mirror_acme.sh.git/blame - acme.sh
support OPENSSL_BIN and "--openssl-bin"
[mirror_acme.sh.git] / acme.sh
CommitLineData
0a7c9364 1#!/usr/bin/env sh
bfdf1f48 2
8f9a1881 3VER=2.6.5
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
4c3b3608 16DEFAULT_CA="https://acme-v01.api.letsencrypt.org"
c93ec933 17DEFAULT_AGREEMENT="https://letsencrypt.org/documents/LE-SA-v1.1.1-August-1-2016.pdf"
4c3b3608 18
07af4247 19DEFAULT_USER_AGENT="$PROJECT_NAME/$VER ($PROJECT)"
d0871bda 20DEFAULT_ACCOUNT_EMAIL=""
bbbdcb09 21
57e58ce7 22DEFAULT_ACCOUNT_KEY_LENGTH=2048
23DEFAULT_DOMAIN_KEY_LENGTH=2048
24
a746139c 25DEFAULT_OPENSSL_BIN="openssl"
26
4c3b3608 27STAGE_CA="https://acme-staging.api.letsencrypt.org"
28
29VTYPE_HTTP="http-01"
30VTYPE_DNS="dns-01"
e22bcf7c 31VTYPE_TLS="tls-sni-01"
e591d5cf 32#VTYPE_TLS2="tls-sni-02"
e22bcf7c 33
0463b5d6 34LOCAL_ANY_ADDRESS="0.0.0.0"
35
656bd330 36MAX_RENEW=60
523c7682 37
4a4dacb5 38DEFAULT_DNS_SLEEP=120
39
3f4513b3 40NO_VALUE="no"
41
e22bcf7c 42W_TLS="tls"
4c3b3608 43
ec603bee 44STATE_VERIFIED="verified_ok"
45
88fab7d6 46BEGIN_CSR="-----BEGIN CERTIFICATE REQUEST-----"
47END_CSR="-----END CERTIFICATE REQUEST-----"
48
49BEGIN_CERT="-----BEGIN CERTIFICATE-----"
50END_CERT="-----END CERTIFICATE-----"
51
cc179731 52RENEW_SKIP=2
53
43822d37 54ECC_SEP="_"
55ECC_SUFFIX="${ECC_SEP}ecc"
56
a73c5b33 57LOG_LEVEL_1=1
58LOG_LEVEL_2=2
59LOG_LEVEL_3=3
60DEFAULT_LOG_LEVEL="$LOG_LEVEL_1"
61
62_DEBUG_WIKI="https://github.com/Neilpang/acme.sh/wiki/How-to-debug-acme.sh"
4c3b3608 63
08ee072f 64__INTERACTIVE=""
4c2a3841 65if [ -t 1 ]; then
08ee072f 66 __INTERACTIVE="1"
67fi
00a50605 68
43822d37 69__green() {
4c2a3841 70 if [ "$__INTERACTIVE" ]; then
2d12b689 71 printf '\033[1;31;32m'
72 fi
43822d37 73 printf -- "$1"
4c2a3841 74 if [ "$__INTERACTIVE" ]; then
2d12b689 75 printf '\033[0m'
76 fi
43822d37 77}
78
79__red() {
4c2a3841 80 if [ "$__INTERACTIVE" ]; then
2d12b689 81 printf '\033[1;31;40m'
82 fi
43822d37 83 printf -- "$1"
4c2a3841 84 if [ "$__INTERACTIVE" ]; then
2d12b689 85 printf '\033[0m'
86 fi
43822d37 87}
00a50605 88
a73c5b33 89_printargs() {
4c2a3841 90 if [ -z "$2" ]; then
a73c5b33 91 printf -- "[$(date)] $1"
43822d37 92 else
a73c5b33 93 printf -- "[$(date)] $1='$2'"
43822d37 94 fi
a73c5b33 95 printf "\n"
43822d37 96}
97
9d548d81 98_dlg_versions() {
99 echo "Diagnosis versions: "
a746139c 100 echo "openssl:$OPENSSL_BIN"
101 if _exists "$OPENSSL_BIN"; then
102 $OPENSSL_BIN version 2>&1
9d548d81 103 else
a746139c 104 echo "$OPENSSL_BIN doesn't exists."
9d548d81 105 fi
4c2a3841 106
9d548d81 107 echo "apache:"
4c2a3841 108 if [ "$_APACHECTL" ] && _exists "$_APACHECTL"; then
9d548d81 109 _APACHECTL -V 2>&1
110 else
111 echo "apache doesn't exists."
112 fi
4c2a3841 113
9d548d81 114 echo "nc:"
4c2a3841 115 if _exists "nc"; then
9d548d81 116 nc -h 2>&1
117 else
118 _debug "nc doesn't exists."
119 fi
120}
43822d37 121
a73c5b33 122_log() {
123 [ -z "$LOG_FILE" ] && return
95e06de5 124 _printargs "$@" >>"$LOG_FILE"
a73c5b33 125}
126
127_info() {
128 _log "$@"
129 _printargs "$@"
4c3b3608 130}
131
132_err() {
a73c5b33 133 _log "$@"
65de3110 134 printf -- "[$(date)] " >&2
4c2a3841 135 if [ -z "$2" ]; then
65de3110 136 __red "$1" >&2
137 else
138 __red "$1='$2'" >&2
139 fi
b19ba13a 140 printf "\n" >&2
4c3b3608 141 return 1
142}
143
43822d37 144_usage() {
4c2a3841 145 __red "$@" >&2
65de3110 146 printf "\n" >&2
43822d37 147}
148
c60883ef 149_debug() {
4c2a3841 150 if [ -z "$LOG_LEVEL" ] || [ "$LOG_LEVEL" -ge "$LOG_LEVEL_1" ]; then
a73c5b33 151 _log "$@"
152 fi
4c2a3841 153 if [ -z "$DEBUG" ]; then
c60883ef 154 return
155 fi
a73c5b33 156 _printargs "$@" >&2
c60883ef 157}
158
a63b05a9 159_debug2() {
4c2a3841 160 if [ "$LOG_LEVEL" ] && [ "$LOG_LEVEL" -ge "$LOG_LEVEL_2" ]; then
a73c5b33 161 _log "$@"
162 fi
4c2a3841 163 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
a63b05a9 164 _debug "$@"
165 fi
a63b05a9 166}
167
22ea4004 168_debug3() {
4c2a3841 169 if [ "$LOG_LEVEL" ] && [ "$LOG_LEVEL" -ge "$LOG_LEVEL_3" ]; then
a73c5b33 170 _log "$@"
171 fi
4c2a3841 172 if [ "$DEBUG" ] && [ "$DEBUG" -ge "3" ]; then
22ea4004 173 _debug "$@"
174 fi
22ea4004 175}
176
4c2a3841 177_startswith() {
dceb3aca 178 _str="$1"
179 _sub="$2"
19539575 180 echo "$_str" | grep "^$_sub" >/dev/null 2>&1
dceb3aca 181}
182
4c2a3841 183_endswith() {
43822d37 184 _str="$1"
185 _sub="$2"
186 echo "$_str" | grep -- "$_sub\$" >/dev/null 2>&1
187}
188
4c2a3841 189_contains() {
dceb3aca 190 _str="$1"
191 _sub="$2"
43822d37 192 echo "$_str" | grep -- "$_sub" >/dev/null 2>&1
dceb3aca 193}
194
c53da1ef 195_hasfield() {
196 _str="$1"
197 _field="$2"
198 _sep="$3"
4c2a3841 199 if [ -z "$_field" ]; then
43822d37 200 _usage "Usage: str field [sep]"
c53da1ef 201 return 1
202 fi
4c2a3841 203
204 if [ -z "$_sep" ]; then
c53da1ef 205 _sep=","
206 fi
4c2a3841 207
208 for f in $(echo "$_str" | tr ',' ' '); do
209 if [ "$f" = "$_field" ]; then
0c9546cc 210 _debug2 "'$_str' contains '$_field'"
c53da1ef 211 return 0 #contains ok
212 fi
213 done
0c9546cc 214 _debug2 "'$_str' does not contain '$_field'"
c53da1ef 215 return 1 #not contains
216}
217
4c2a3841 218_getfield() {
0463b5d6 219 _str="$1"
220 _findex="$2"
221 _sep="$3"
4c2a3841 222
223 if [ -z "$_findex" ]; then
0463b5d6 224 _usage "Usage: str field [sep]"
225 return 1
226 fi
4c2a3841 227
228 if [ -z "$_sep" ]; then
0463b5d6 229 _sep=","
230 fi
231
201aa244 232 _ffi="$_findex"
4c2a3841 233 while [ "$_ffi" -gt "0" ]; do
201aa244 234 _fv="$(echo "$_str" | cut -d "$_sep" -f "$_ffi")"
4c2a3841 235 if [ "$_fv" ]; then
0463b5d6 236 printf -- "%s" "$_fv"
237 return 0
238 fi
95e06de5 239 _ffi="$(_math "$_ffi" - 1)"
0463b5d6 240 done
4c2a3841 241
0463b5d6 242 printf -- "%s" "$_str"
243
244}
245
4c2a3841 246_exists() {
c60883ef 247 cmd="$1"
4c2a3841 248 if [ -z "$cmd" ]; then
43822d37 249 _usage "Usage: _exists cmd"
c60883ef 250 return 1
251 fi
82dc2244 252
253 if eval type type >/dev/null 2>&1; then
254 eval type "$cmd" >/dev/null 2>&1
255 elif command >/dev/null 2>&1; then
19539575 256 command -v "$cmd" >/dev/null 2>&1
ce4be4e9 257 else
e591d5cf 258 which "$cmd" >/dev/null 2>&1
eac18b1c 259 fi
c60883ef 260 ret="$?"
690a5e20 261 _debug3 "$cmd exists=$ret"
c60883ef 262 return $ret
263}
264
00a50605 265#a + b
4c2a3841 266_math() {
be68fbd4 267 _m_opts="$@"
268 printf "%s" "$(($_m_opts))"
00a50605 269}
270
271_h_char_2_dec() {
272 _ch=$1
273 case "${_ch}" in
4c2a3841 274 a | A)
19539575 275 printf "10"
4c2a3841 276 ;;
277 b | B)
19539575 278 printf "11"
4c2a3841 279 ;;
280 c | C)
19539575 281 printf "12"
4c2a3841 282 ;;
283 d | D)
19539575 284 printf "13"
4c2a3841 285 ;;
286 e | E)
19539575 287 printf "14"
4c2a3841 288 ;;
289 f | F)
19539575 290 printf "15"
4c2a3841 291 ;;
00a50605 292 *)
19539575 293 printf "%s" "$_ch"
4c2a3841 294 ;;
19539575 295 esac
00a50605 296
297}
298
fac1e367 299_URGLY_PRINTF=""
4c2a3841 300if [ "$(printf '\x41')" != 'A' ]; then
fac1e367 301 _URGLY_PRINTF=1
302fi
303
4c3b3608 304_h2b() {
305 hex=$(cat)
306 i=1
307 j=2
e591d5cf 308
690a5e20 309 _debug3 _URGLY_PRINTF "$_URGLY_PRINTF"
4c2a3841 310 while true; do
311 if [ -z "$_URGLY_PRINTF" ]; then
f9a6988e 312 h="$(printf "%s" "$hex" | cut -c $i-$j)"
4c2a3841 313 if [ -z "$h" ]; then
314 break
00a50605 315 fi
e591d5cf 316 printf "\x$h%s"
00a50605 317 else
95e06de5 318 ic="$(printf "%s" "$hex" | cut -c $i)"
319 jc="$(printf "%s" "$hex" | cut -c $j)"
4c2a3841 320 if [ -z "$ic$jc" ]; then
321 break
00a50605 322 fi
19539575 323 ic="$(_h_char_2_dec "$ic")"
324 jc="$(_h_char_2_dec "$jc")"
e51bef6d 325 printf '\'"$(printf "%o" "$(_math "$ic" \* 16 + $jc)")""%s"
4c3b3608 326 fi
e591d5cf 327
328 i="$(_math "$i" + 2)"
329 j="$(_math "$j" + 2)"
330
4c3b3608 331 done
332}
333
e009ec8b 334#hex string
335_hex() {
336 _str="$1"
337 _str_len=${#_str}
338 _h_i=1
339 while [ "$_h_i" -le "$_str_len" ]; do
16d79eba 340 _str_c="$(printf "%s" "$_str" | cut -c "$_h_i")"
e009ec8b 341 printf "%02x" "'$_str_c"
342 _h_i="$(_math "$_h_i" + 1)"
343 done
344}
345
c60883ef 346#options file
347_sed_i() {
348 options="$1"
349 filename="$2"
4c2a3841 350 if [ -z "$filename" ]; then
43822d37 351 _usage "Usage:_sed_i options filename"
c60883ef 352 return 1
353 fi
14f3dbb7 354 _debug2 options "$options"
355 if sed -h 2>&1 | grep "\-i\[SUFFIX]" >/dev/null 2>&1; then
c60883ef 356 _debug "Using sed -i"
14f3dbb7 357 sed -i "$options" "$filename"
c60883ef 358 else
359 _debug "No -i support in sed"
19539575 360 text="$(cat "$filename")"
4c2a3841 361 echo "$text" | sed "$options" >"$filename"
c60883ef 362 fi
363}
364
22ea4004 365_egrep_o() {
a3c0c754 366 if ! egrep -o "$1" 2>/dev/null; then
22ea4004 367 sed -n 's/.*\('"$1"'\).*/\1/p'
22ea4004 368 fi
369}
370
88fab7d6 371#Usage: file startline endline
372_getfile() {
373 filename="$1"
374 startline="$2"
375 endline="$3"
4c2a3841 376 if [ -z "$endline" ]; then
43822d37 377 _usage "Usage: file startline endline"
88fab7d6 378 return 1
379 fi
4c2a3841 380
381 i="$(grep -n -- "$startline" "$filename" | cut -d : -f 1)"
382 if [ -z "$i" ]; then
88fab7d6 383 _err "Can not find start line: $startline"
384 return 1
385 fi
19539575 386 i="$(_math "$i" + 1)"
387 _debug i "$i"
4c2a3841 388
389 j="$(grep -n -- "$endline" "$filename" | cut -d : -f 1)"
390 if [ -z "$j" ]; then
88fab7d6 391 _err "Can not find end line: $endline"
392 return 1
393 fi
19539575 394 j="$(_math "$j" - 1)"
395 _debug j "$j"
4c2a3841 396
397 sed -n "$i,${j}p" "$filename"
88fab7d6 398
399}
400
401#Usage: multiline
4c3b3608 402_base64() {
4c2a3841 403 if [ "$1" ]; then
a746139c 404 $OPENSSL_BIN base64 -e
88fab7d6 405 else
a746139c 406 $OPENSSL_BIN base64 -e | tr -d '\r\n'
88fab7d6 407 fi
408}
409
410#Usage: multiline
411_dbase64() {
4c2a3841 412 if [ "$1" ]; then
a746139c 413 $OPENSSL_BIN base64 -d -A
88fab7d6 414 else
a746139c 415 $OPENSSL_BIN base64 -d
88fab7d6 416 fi
417}
418
e22bcf7c 419#Usage: hashalg [outputhex]
88fab7d6 420#Output Base64-encoded digest
421_digest() {
422 alg="$1"
4c2a3841 423 if [ -z "$alg" ]; then
43822d37 424 _usage "Usage: _digest hashalg"
88fab7d6 425 return 1
426 fi
4c2a3841 427
e22bcf7c 428 outputhex="$2"
4c2a3841 429
c7b16249 430 if [ "$alg" = "sha256" ] || [ "$alg" = "sha1" ] || [ "$alg" = "md5" ]; then
4c2a3841 431 if [ "$outputhex" ]; then
a746139c 432 $OPENSSL_BIN dgst -"$alg" -hex | cut -d = -f 2 | tr -d ' '
e22bcf7c 433 else
a746139c 434 $OPENSSL_BIN dgst -"$alg" -binary | _base64
b001840d 435 fi
436 else
437 _err "$alg is not supported yet"
438 return 1
439 fi
440
441}
442
e009ec8b 443#Usage: hashalg secret_hex [outputhex]
444#Output binary hmac
b001840d 445_hmac() {
446 alg="$1"
e009ec8b 447 secret_hex="$2"
b001840d 448 outputhex="$3"
4c2a3841 449
e009ec8b 450 if [ -z "$secret_hex" ]; then
4c2a3841 451 _usage "Usage: _hmac hashalg secret [outputhex]"
b001840d 452 return 1
453 fi
454
a6014bf0 455 if [ "$alg" = "sha256" ] || [ "$alg" = "sha1" ]; then
4c2a3841 456 if [ "$outputhex" ]; then
a746139c 457 $OPENSSL_BIN dgst -"$alg" -mac HMAC -macopt "hexkey:$secret_hex" | cut -d = -f 2 | tr -d ' '
e22bcf7c 458 else
a746139c 459 $OPENSSL_BIN dgst -"$alg" -mac HMAC -macopt "hexkey:$secret_hex" -binary
e22bcf7c 460 fi
88fab7d6 461 else
462 _err "$alg is not supported yet"
463 return 1
464 fi
465
466}
467
468#Usage: keyfile hashalg
469#Output: Base64-encoded signature value
470_sign() {
471 keyfile="$1"
472 alg="$2"
4c2a3841 473 if [ -z "$alg" ]; then
43822d37 474 _usage "Usage: _sign keyfile hashalg"
88fab7d6 475 return 1
476 fi
4c2a3841 477
a746139c 478 _sign_openssl="$OPENSSL_BIN dgst -sign $keyfile "
4c2a3841 479 if [ "$alg" = "sha256" ]; then
998783eb 480 _sign_openssl="$_sign_openssl -$alg"
88fab7d6 481 else
482 _err "$alg is not supported yet"
483 return 1
fac1e367 484 fi
4c2a3841 485
486 if grep "BEGIN RSA PRIVATE KEY" "$keyfile" >/dev/null 2>&1; then
998783eb 487 $_sign_openssl | _base64
4c2a3841 488 elif grep "BEGIN EC PRIVATE KEY" "$keyfile" >/dev/null 2>&1; then
a746139c 489 if ! _signedECText="$($_sign_openssl | $OPENSSL_BIN asn1parse -inform DER)"; then
67184d7b 490 _err "Sign failed: $_sign_openssl"
491 _err "Key file: $keyfile"
f9a6988e 492 _err "Key content:$(wc -l <"$keyfile") lises"
67184d7b 493 return 1
494 fi
998783eb 495 _debug3 "_signedECText" "$_signedECText"
496 _ec_r="$(echo "$_signedECText" | _head_n 2 | _tail_n 1 | cut -d : -f 4 | tr -d "\r\n")"
497 _debug3 "_ec_r" "$_ec_r"
498 _ec_s="$(echo "$_signedECText" | _head_n 3 | _tail_n 1 | cut -d : -f 4 | tr -d "\r\n")"
499 _debug3 "_ec_s" "$_ec_s"
500 printf "%s" "$_ec_r$_ec_s" | _h2b | _base64
501 else
502 _err "Unknown key file format."
503 return 1
504 fi
4c2a3841 505
4c3b3608 506}
507
43822d37 508#keylength
509_isEccKey() {
510 _length="$1"
511
4c2a3841 512 if [ -z "$_length" ]; then
43822d37 513 return 1
514 fi
515
516 [ "$_length" != "1024" ] \
4c2a3841 517 && [ "$_length" != "2048" ] \
518 && [ "$_length" != "3072" ] \
519 && [ "$_length" != "4096" ] \
520 && [ "$_length" != "8192" ]
43822d37 521}
522
e22bcf7c 523# _createkey 2048|ec-256 file
524_createkey() {
525 length="$1"
526 f="$2"
43822d37 527 eccname="$length"
4c2a3841 528 if _startswith "$length" "ec-"; then
f9a6988e 529 length=$(printf "%s" "$length" | cut -d '-' -f 2-100)
e22bcf7c 530
4c2a3841 531 if [ "$length" = "256" ]; then
e22bcf7c 532 eccname="prime256v1"
533 fi
4c2a3841 534 if [ "$length" = "384" ]; then
e22bcf7c 535 eccname="secp384r1"
536 fi
4c2a3841 537 if [ "$length" = "521" ]; then
e22bcf7c 538 eccname="secp521r1"
539 fi
43822d37 540
e22bcf7c 541 fi
542
4c2a3841 543 if [ -z "$length" ]; then
544 length=2048
43822d37 545 fi
4c2a3841 546
cbcd7e0f 547 _debug "Use length $length"
43822d37 548
4c2a3841 549 if _isEccKey "$length"; then
cbcd7e0f 550 _debug "Using ec name: $eccname"
a746139c 551 $OPENSSL_BIN ecparam -name "$eccname" -genkey 2>/dev/null >"$f"
e22bcf7c 552 else
cbcd7e0f 553 _debug "Using RSA: $length"
a746139c 554 $OPENSSL_BIN genrsa "$length" 2>/dev/null >"$f"
e22bcf7c 555 fi
43822d37 556
4c2a3841 557 if [ "$?" != "0" ]; then
43822d37 558 _err "Create key error."
559 return 1
560 fi
e22bcf7c 561}
562
9774b01b 563#domain
564_is_idn() {
565 _is_idn_d="$1"
049be104 566 _debug2 _is_idn_d "$_is_idn_d"
c7b8f223 567 _idn_temp=$(printf "%s" "$_is_idn_d" | tr -d '[0-9]' | tr -d '[a-z]' | tr -d '[A-Z]' | tr -d '.,-')
049be104 568 _debug2 _idn_temp "$_idn_temp"
569 [ "$_idn_temp" ]
9774b01b 570}
571
572#aa.com
573#aa.com,bb.com,cc.com
574_idn() {
575 __idn_d="$1"
4c2a3841 576 if ! _is_idn "$__idn_d"; then
9774b01b 577 printf "%s" "$__idn_d"
578 return 0
579 fi
4c2a3841 580
581 if _exists idn; then
582 if _contains "$__idn_d" ','; then
9774b01b 583 _i_first="1"
4c2a3841 584 for f in $(echo "$__idn_d" | tr ',' ' '); do
9774b01b 585 [ -z "$f" ] && continue
4c2a3841 586 if [ -z "$_i_first" ]; then
9774b01b 587 printf "%s" ","
588 else
589 _i_first=""
590 fi
2a1e06f8 591 idn --quiet "$f" | tr -d "\r\n"
9774b01b 592 done
593 else
594 idn "$__idn_d" | tr -d "\r\n"
595 fi
596 else
597 _err "Please install idn to process IDN names."
598 fi
599}
600
e22bcf7c 601#_createcsr cn san_list keyfile csrfile conf
602_createcsr() {
603 _debug _createcsr
604 domain="$1"
605 domainlist="$2"
0c9546cc 606 csrkey="$3"
e22bcf7c 607 csr="$4"
608 csrconf="$5"
609 _debug2 domain "$domain"
610 _debug2 domainlist "$domainlist"
0c9546cc 611 _debug2 csrkey "$csrkey"
612 _debug2 csr "$csr"
613 _debug2 csrconf "$csrconf"
4c2a3841 614
615 printf "[ req_distinguished_name ]\n[ req ]\ndistinguished_name = req_distinguished_name\nreq_extensions = v3_req\n[ v3_req ]\n\nkeyUsage = nonRepudiation, digitalSignature, keyEncipherment" >"$csrconf"
616
3f4513b3 617 if [ -z "$domainlist" ] || [ "$domainlist" = "$NO_VALUE" ]; then
e22bcf7c 618 #single domain
619 _info "Single domain" "$domain"
e22bcf7c 620 else
f9a6988e 621 domainlist="$(_idn "$domainlist")"
9774b01b 622 _debug2 domainlist "$domainlist"
4c2a3841 623 if _contains "$domainlist" ","; then
f9a6988e 624 alt="DNS:$(echo "$domainlist" | sed "s/,/,DNS:/g")"
e22bcf7c 625 else
626 alt="DNS:$domainlist"
627 fi
628 #multi
629 _info "Multi domain" "$alt"
4c2a3841 630 printf -- "\nsubjectAltName=$alt" >>"$csrconf"
0c9546cc 631 fi
4c2a3841 632 if [ "$Le_OCSP_Stable" ]; then
0c9546cc 633 _savedomainconf Le_OCSP_Stable "$Le_OCSP_Stable"
4c2a3841 634 printf -- "\nbasicConstraints = CA:FALSE\n1.3.6.1.5.5.7.1.24=DER:30:03:02:01:05" >>"$csrconf"
e22bcf7c 635 fi
4c2a3841 636
9774b01b 637 _csr_cn="$(_idn "$domain")"
638 _debug2 _csr_cn "$_csr_cn"
a746139c 639 $OPENSSL_BIN req -new -sha256 -key "$csrkey" -subj "/CN=$_csr_cn" -config "$csrconf" -out "$csr"
e22bcf7c 640}
641
642#_signcsr key csr conf cert
643_signcsr() {
644 key="$1"
645 csr="$2"
646 conf="$3"
647 cert="$4"
5aa146a5 648 _debug "_signcsr"
4c2a3841 649
a746139c 650 _msg="$($OPENSSL_BIN x509 -req -days 365 -in "$csr" -signkey "$key" -extensions v3_req -extfile "$conf" -out "$cert" 2>&1)"
5aa146a5 651 _ret="$?"
652 _debug "$_msg"
653 return $_ret
e22bcf7c 654}
655
10afcaca 656#_csrfile
657_readSubjectFromCSR() {
658 _csrfile="$1"
4c2a3841 659 if [ -z "$_csrfile" ]; then
10afcaca 660 _usage "_readSubjectFromCSR mycsr.csr"
661 return 1
662 fi
a746139c 663 $OPENSSL_BIN req -noout -in "$_csrfile" -subject | _egrep_o "CN=.*" | cut -d = -f 2 | cut -d / -f 1 | tr -d '\n'
10afcaca 664}
665
666#_csrfile
667#echo comma separated domain list
668_readSubjectAltNamesFromCSR() {
669 _csrfile="$1"
4c2a3841 670 if [ -z "$_csrfile" ]; then
10afcaca 671 _usage "_readSubjectAltNamesFromCSR mycsr.csr"
672 return 1
673 fi
4c2a3841 674
10afcaca 675 _csrsubj="$(_readSubjectFromCSR "$_csrfile")"
676 _debug _csrsubj "$_csrsubj"
4c2a3841 677
a746139c 678 _dnsAltnames="$($OPENSSL_BIN req -noout -text -in "$_csrfile" | grep "^ *DNS:.*" | tr -d ' \n')"
10afcaca 679 _debug _dnsAltnames "$_dnsAltnames"
4c2a3841 680
681 if _contains "$_dnsAltnames," "DNS:$_csrsubj,"; then
10afcaca 682 _debug "AltNames contains subject"
1643b476 683 _dnsAltnames="$(printf "%s" "$_dnsAltnames," | sed "s/DNS:$_csrsubj,//g")"
10afcaca 684 else
685 _debug "AltNames doesn't contain subject"
686 fi
4c2a3841 687
1643b476 688 printf "%s" "$_dnsAltnames" | sed "s/DNS://g"
10afcaca 689}
690
691#_csrfile
692_readKeyLengthFromCSR() {
693 _csrfile="$1"
4c2a3841 694 if [ -z "$_csrfile" ]; then
1643b476 695 _usage "_readKeyLengthFromCSR mycsr.csr"
10afcaca 696 return 1
697 fi
4c2a3841 698
a746139c 699 _outcsr="$($OPENSSL_BIN req -noout -text -in "$_csrfile")"
4c2a3841 700 if _contains "$_outcsr" "Public Key Algorithm: id-ecPublicKey"; then
10afcaca 701 _debug "ECC CSR"
702 echo "$_outcsr" | _egrep_o "^ *ASN1 OID:.*" | cut -d ':' -f 2 | tr -d ' '
703 else
704 _debug "RSA CSR"
705 echo "$_outcsr" | _egrep_o "^ *Public-Key:.*" | cut -d '(' -f 2 | cut -d ' ' -f 1
706 fi
707}
708
34c27e09 709_ss() {
710 _port="$1"
4c2a3841 711
712 if _exists "ss"; then
edf08da6 713 _debug "Using: ss"
19539575 714 ss -ntpl | grep ":$_port "
edf08da6 715 return 0
716 fi
717
4c2a3841 718 if _exists "netstat"; then
251fc37c 719 _debug "Using: netstat"
4c2a3841 720 if netstat -h 2>&1 | grep "\-p proto" >/dev/null; then
ccb96535 721 #for windows version netstat tool
0463b5d6 722 netstat -an -p tcp | grep "LISTENING" | grep ":$_port "
ccb96535 723 else
4c2a3841 724 if netstat -help 2>&1 | grep "\-p protocol" >/dev/null; then
19539575 725 netstat -an -p tcp | grep LISTEN | grep ":$_port "
4c2a3841 726 elif netstat -help 2>&1 | grep -- '-P protocol' >/dev/null; then
22ea4004 727 #for solaris
e3c66532 728 netstat -an -P tcp | grep "\.$_port " | grep "LISTEN"
edf08da6 729 else
19539575 730 netstat -ntpl | grep ":$_port "
edf08da6 731 fi
ccb96535 732 fi
34c27e09 733 return 0
734 fi
edf08da6 735
34c27e09 736 return 1
737}
738
43822d37 739#domain [password] [isEcc]
ac2d5123 740toPkcs() {
741 domain="$1"
742 pfxPassword="$2"
4c2a3841 743 if [ -z "$domain" ]; then
43822d37 744 _usage "Usage: $PROJECT_ENTRY --toPkcs -d domain [--password pfx-password]"
ac2d5123 745 return 1
746 fi
747
43822d37 748 _isEcc="$3"
4c2a3841 749
43822d37 750 _initpath "$domain" "$_isEcc"
751
4c2a3841 752 if [ "$pfxPassword" ]; then
a746139c 753 $OPENSSL_BIN pkcs12 -export -out "$CERT_PFX_PATH" -inkey "$CERT_KEY_PATH" -in "$CERT_PATH" -certfile "$CA_CERT_PATH" -password "pass:$pfxPassword"
ac2d5123 754 else
a746139c 755 $OPENSSL_BIN pkcs12 -export -out "$CERT_PFX_PATH" -inkey "$CERT_KEY_PATH" -in "$CERT_PATH" -certfile "$CA_CERT_PATH"
ac2d5123 756 fi
4c2a3841 757
758 if [ "$?" = "0" ]; then
ac2d5123 759 _info "Success, Pfx is exported to: $CERT_PFX_PATH"
760 fi
761
762}
763
5fbc47eb 764#[2048]
4c3b3608 765createAccountKey() {
766 _info "Creating account key"
4c2a3841 767 if [ -z "$1" ]; then
5fbc47eb 768 _usage "Usage: $PROJECT_ENTRY --createAccountKey --accountkeylength 2048"
4c3b3608 769 return
770 fi
4c2a3841 771
5fbc47eb 772 length=$1
57e58ce7 773 _create_account_key "$length"
774
775}
776
777_create_account_key() {
778
5fbc47eb 779 length=$1
4c2a3841 780
781 if [ -z "$length" ] || [ "$length" = "$NO_VALUE" ]; then
57e58ce7 782 _debug "Use default length $DEFAULT_ACCOUNT_KEY_LENGTH"
783 length="$DEFAULT_ACCOUNT_KEY_LENGTH"
4c3b3608 784 fi
4c2a3841 785
5fbc47eb 786 _debug length "$length"
4c3b3608 787 _initpath
5fbc47eb 788
57e58ce7 789 mkdir -p "$CA_DIR"
4c2a3841 790 if [ -f "$ACCOUNT_KEY_PATH" ]; then
4c3b3608 791 _info "Account key exists, skip"
792 return
793 else
794 #generate account key
31a5487c 795 _createkey "$length" "$ACCOUNT_KEY_PATH"
4c3b3608 796 fi
797
798}
799
43822d37 800#domain [length]
4c3b3608 801createDomainKey() {
802 _info "Creating domain key"
4c2a3841 803 if [ -z "$1" ]; then
43822d37 804 _usage "Usage: $PROJECT_ENTRY --createDomainKey -d domain.com [ --keylength 2048 ]"
4c3b3608 805 return
806 fi
4c2a3841 807
4c3b3608 808 domain=$1
e22bcf7c 809 length=$2
810
4c2a3841 811 if [ -z "$length" ]; then
57e58ce7 812 _debug "Use DEFAULT_DOMAIN_KEY_LENGTH=$DEFAULT_DOMAIN_KEY_LENGTH"
813 length="$DEFAULT_DOMAIN_KEY_LENGTH"
814 fi
e22bcf7c 815
f9a6988e 816 _initpath "$domain" "$length"
4c2a3841 817
818 if [ ! -f "$CERT_KEY_PATH" ] || ([ "$FORCE" ] && ! [ "$IS_RENEW" ]); then
e22bcf7c 819 _createkey "$length" "$CERT_KEY_PATH"
4c3b3608 820 else
4c2a3841 821 if [ "$IS_RENEW" ]; then
4c3b3608 822 _info "Domain key exists, skip"
823 return 0
824 else
825 _err "Domain key exists, do you want to overwrite the key?"
41e3eafa 826 _err "Add '--force', and try again."
4c3b3608 827 return 1
828 fi
829 fi
830
831}
832
43822d37 833# domain domainlist isEcc
4c3b3608 834createCSR() {
835 _info "Creating csr"
4c2a3841 836 if [ -z "$1" ]; then
43822d37 837 _usage "Usage: $PROJECT_ENTRY --createCSR -d domain1.com [-d domain2.com -d domain3.com ... ]"
4c3b3608 838 return
839 fi
4c2a3841 840
43822d37 841 domain="$1"
842 domainlist="$2"
843 _isEcc="$3"
4c2a3841 844
43822d37 845 _initpath "$domain" "$_isEcc"
4c2a3841 846
847 if [ -f "$CSR_PATH" ] && [ "$IS_RENEW" ] && [ -z "$FORCE" ]; then
4c3b3608 848 _info "CSR exists, skip"
849 return
850 fi
4c2a3841 851
852 if [ ! -f "$CERT_KEY_PATH" ]; then
43822d37 853 _err "The key file is not found: $CERT_KEY_PATH"
854 _err "Please create the key file first."
855 return 1
856 fi
e22bcf7c 857 _createcsr "$domain" "$domainlist" "$CERT_KEY_PATH" "$CSR_PATH" "$DOMAIN_SSL_CONF"
4c2a3841 858
4c3b3608 859}
860
166096dc 861_urlencode() {
f9a6988e 862 tr '/+' '_-' | tr -d '= '
4c3b3608 863}
864
865_time2str() {
866 #BSD
f9a6988e 867 if date -u -d@"$1" 2>/dev/null; then
4c3b3608 868 return
869 fi
4c2a3841 870
4c3b3608 871 #Linux
f9a6988e 872 if date -u -r "$1" 2>/dev/null; then
4c3b3608 873 return
874 fi
4c2a3841 875
22ea4004 876 #Soaris
4c2a3841 877 if _exists adb; then
031e885e 878 _t_s_a=$(echo "0t${1}=Y" | adb)
879 echo "$_t_s_a"
22ea4004 880 fi
4c2a3841 881
4c3b3608 882}
883
eae29099 884_normalizeJson() {
885 sed "s/\" *: *\([\"{\[]\)/\":\1/g" | sed "s/^ *\([^ ]\)/\1/" | tr -d "\r\n"
886}
887
44df2967 888_stat() {
889 #Linux
4c2a3841 890 if stat -c '%U:%G' "$1" 2>/dev/null; then
44df2967 891 return
892 fi
4c2a3841 893
44df2967 894 #BSD
4c2a3841 895 if stat -f '%Su:%Sg' "$1" 2>/dev/null; then
44df2967 896 return
897 fi
4c2a3841 898
899 return 1 #error, 'stat' not found
44df2967 900}
901
166096dc 902#keyfile
903_calcjwk() {
904 keyfile="$1"
4c2a3841 905 if [ -z "$keyfile" ]; then
43822d37 906 _usage "Usage: _calcjwk keyfile"
166096dc 907 return 1
908 fi
4c2a3841 909
910 if [ "$JWK_HEADER" ] && [ "$__CACHED_JWK_KEY_FILE" = "$keyfile" ]; then
ae2db62f 911 _debug2 "Use cached jwk for file: $__CACHED_JWK_KEY_FILE"
912 return 0
913 fi
4c2a3841 914
4c2a3841 915 if grep "BEGIN RSA PRIVATE KEY" "$keyfile" >/dev/null 2>&1; then
166096dc 916 _debug "RSA key"
a746139c 917 pub_exp=$($OPENSSL_BIN rsa -in "$keyfile" -noout -text | grep "^publicExponent:" | cut -d '(' -f 2 | cut -d 'x' -f 2 | cut -d ')' -f 1)
4c2a3841 918 if [ "${#pub_exp}" = "5" ]; then
166096dc 919 pub_exp=0$pub_exp
920 fi
22ea4004 921 _debug3 pub_exp "$pub_exp"
4c2a3841 922
f9a6988e 923 e=$(echo "$pub_exp" | _h2b | _base64)
22ea4004 924 _debug3 e "$e"
4c2a3841 925
a746139c 926 modulus=$($OPENSSL_BIN rsa -in "$keyfile" -modulus -noout | cut -d '=' -f 2)
22ea4004 927 _debug3 modulus "$modulus"
4c2a3841 928 n="$(printf "%s" "$modulus" | _h2b | _base64 | _urlencode)"
166096dc 929 jwk='{"e": "'$e'", "kty": "RSA", "n": "'$n'"}'
22ea4004 930 _debug3 jwk "$jwk"
4c2a3841 931
5982f4bc 932 JWK_HEADER='{"alg": "RS256", "jwk": '$jwk'}'
933 JWK_HEADERPLACE_PART1='{"nonce": "'
934 JWK_HEADERPLACE_PART2='", "alg": "RS256", "jwk": '$jwk'}'
4c2a3841 935 elif grep "BEGIN EC PRIVATE KEY" "$keyfile" >/dev/null 2>&1; then
166096dc 936 _debug "EC key"
a746139c 937 crv="$($OPENSSL_BIN ec -in "$keyfile" -noout -text 2>/dev/null | grep "^NIST CURVE:" | cut -d ":" -f 2 | tr -d " \r\n")"
22ea4004 938 _debug3 crv "$crv"
4c2a3841 939
940 if [ -z "$crv" ]; then
d22b7938 941 _debug "Let's try ASN1 OID"
a746139c 942 crv_oid="$($OPENSSL_BIN ec -in "$keyfile" -noout -text 2>/dev/null | grep "^ASN1 OID:" | cut -d ":" -f 2 | tr -d " \r\n")"
cae9cee2 943 _debug3 crv_oid "$crv_oid"
d22b7938 944 case "${crv_oid}" in
945 "prime256v1")
4c2a3841 946 crv="P-256"
947 ;;
d22b7938 948 "secp384r1")
4c2a3841 949 crv="P-384"
950 ;;
d22b7938 951 "secp521r1")
4c2a3841 952 crv="P-521"
953 ;;
d22b7938 954 *)
4c2a3841 955 _err "ECC oid : $crv_oid"
956 return 1
957 ;;
067d586c 958 esac
d22b7938 959 _debug3 crv "$crv"
960 fi
4c2a3841 961
a746139c 962 pubi="$($OPENSSL_BIN ec -in "$keyfile" -noout -text 2>/dev/null | grep -n pub: | cut -d : -f 1)"
79a267ab 963 pubi=$(_math "$pubi" + 1)
22ea4004 964 _debug3 pubi "$pubi"
4c2a3841 965
a746139c 966 pubj="$($OPENSSL_BIN ec -in "$keyfile" -noout -text 2>/dev/null | grep -n "ASN1 OID:" | cut -d : -f 1)"
79a267ab 967 pubj=$(_math "$pubj" - 1)
22ea4004 968 _debug3 pubj "$pubj"
4c2a3841 969
a746139c 970 pubtext="$($OPENSSL_BIN ec -in "$keyfile" -noout -text 2>/dev/null | sed -n "$pubi,${pubj}p" | tr -d " \n\r")"
22ea4004 971 _debug3 pubtext "$pubtext"
4c2a3841 972
95e06de5 973 xlen="$(printf "%s" "$pubtext" | tr -d ':' | wc -c)"
79a267ab 974 xlen=$(_math "$xlen" / 4)
22ea4004 975 _debug3 xlen "$xlen"
00a50605 976
998783eb 977 xend=$(_math "$xlen" + 1)
f9a6988e 978 x="$(printf "%s" "$pubtext" | cut -d : -f 2-"$xend")"
22ea4004 979 _debug3 x "$x"
4c2a3841 980
95e06de5 981 x64="$(printf "%s" "$x" | tr -d : | _h2b | _base64 | _urlencode)"
22ea4004 982 _debug3 x64 "$x64"
00a50605 983
19539575 984 xend=$(_math "$xend" + 1)
f9a6988e 985 y="$(printf "%s" "$pubtext" | cut -d : -f "$xend"-10000)"
22ea4004 986 _debug3 y "$y"
4c2a3841 987
95e06de5 988 y64="$(printf "%s" "$y" | tr -d : | _h2b | _base64 | _urlencode)"
22ea4004 989 _debug3 y64 "$y64"
4c2a3841 990
ae2db62f 991 jwk='{"crv": "'$crv'", "kty": "EC", "x": "'$x64'", "y": "'$y64'"}'
22ea4004 992 _debug3 jwk "$jwk"
4c2a3841 993
5982f4bc 994 JWK_HEADER='{"alg": "ES256", "jwk": '$jwk'}'
995 JWK_HEADERPLACE_PART1='{"nonce": "'
996 JWK_HEADERPLACE_PART2='", "alg": "ES256", "jwk": '$jwk'}'
166096dc 997 else
998 _err "Only RSA or EC key is supported."
999 return 1
1000 fi
1001
5982f4bc 1002 _debug3 JWK_HEADER "$JWK_HEADER"
ae2db62f 1003 __CACHED_JWK_KEY_FILE="$keyfile"
166096dc 1004}
fac1e367 1005
3aae1ae3 1006_time() {
1007 date -u "+%s"
1008}
fac1e367 1009
1010_mktemp() {
4c2a3841 1011 if _exists mktemp; then
1012 if mktemp 2>/dev/null; then
610e0f21 1013 return 0
4c2a3841 1014 elif _contains "$(mktemp 2>&1)" "-t prefix" && mktemp -t "$PROJECT_NAME" 2>/dev/null; then
5c48e139 1015 #for Mac osx
610e0f21 1016 return 0
b19ba13a 1017 fi
fac1e367 1018 fi
4c2a3841 1019 if [ -d "/tmp" ]; then
3aae1ae3 1020 echo "/tmp/${PROJECT_NAME}wefADf24sf.$(_time).tmp"
1021 return 0
4c2a3841 1022 elif [ "$LE_TEMP_DIR" ] && mkdir -p "$LE_TEMP_DIR"; then
610e0f21 1023 echo "/$LE_TEMP_DIR/wefADf24sf.$(_time).tmp"
1024 return 0
3aae1ae3 1025 fi
1026 _err "Can not create temp file."
fac1e367 1027}
1028
1029_inithttp() {
1030
4c2a3841 1031 if [ -z "$HTTP_HEADER" ] || ! touch "$HTTP_HEADER"; then
fac1e367 1032 HTTP_HEADER="$(_mktemp)"
1033 _debug2 HTTP_HEADER "$HTTP_HEADER"
1034 fi
1035
4c2a3841 1036 if [ "$__HTTP_INITIALIZED" ]; then
1037 if [ "$_ACME_CURL$_ACME_WGET" ]; then
1befee5a 1038 _debug2 "Http already initialized."
1039 return 0
1040 fi
1041 fi
4c2a3841 1042
1043 if [ -z "$_ACME_CURL" ] && _exists "curl"; then
1befee5a 1044 _ACME_CURL="curl -L --silent --dump-header $HTTP_HEADER "
4c2a3841 1045 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
fac1e367 1046 _CURL_DUMP="$(_mktemp)"
1befee5a 1047 _ACME_CURL="$_ACME_CURL --trace-ascii $_CURL_DUMP "
fac1e367 1048 fi
1049
4c2a3841 1050 if [ "$CA_BUNDLE" ]; then
1befee5a 1051 _ACME_CURL="$_ACME_CURL --cacert $CA_BUNDLE "
78009539
PS
1052 fi
1053
fac1e367 1054 fi
4c2a3841 1055
1befee5a 1056 if [ -z "$_ACME_WGET" ] && _exists "wget"; then
1057 _ACME_WGET="wget -q"
4c2a3841 1058 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
1befee5a 1059 _ACME_WGET="$_ACME_WGET -d "
fac1e367 1060 fi
4c2a3841 1061 if [ "$CA_BUNDLE" ]; then
1befee5a 1062 _ACME_WGET="$_ACME_WGET --ca-certificate $CA_BUNDLE "
78009539 1063 fi
fac1e367 1064 fi
1065
1befee5a 1066 __HTTP_INITIALIZED=1
fac1e367 1067
fac1e367 1068}
fac1e367 1069
c839b2b0 1070# body url [needbase64] [POST|PUT]
c60883ef 1071_post() {
1072 body="$1"
1073 url="$2"
1074 needbase64="$3"
a4270efa 1075 httpmethod="$4"
c60883ef 1076
4c2a3841 1077 if [ -z "$httpmethod" ]; then
a4270efa 1078 httpmethod="POST"
1079 fi
1080 _debug $httpmethod
484d9d2a 1081 _debug "url" "$url"
30de13b4 1082 _debug2 "body" "$body"
4c2a3841 1083
fac1e367 1084 _inithttp
4c2a3841 1085
1086 if [ "$_ACME_CURL" ]; then
1befee5a 1087 _CURL="$_ACME_CURL"
7834c252 1088 if [ "$HTTPS_INSECURE" ]; then
1089 _CURL="$_CURL --insecure "
1090 fi
ec9fc8cb 1091 _debug "_CURL" "$_CURL"
4c2a3841 1092 if [ "$needbase64" ]; then
690a5e20 1093 response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$url" | _base64)"
c60883ef 1094 else
4c2a3841 1095 response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$url")"
c60883ef 1096 fi
16679b57 1097 _ret="$?"
4c2a3841 1098 if [ "$_ret" != "0" ]; then
87ab2d90 1099 _err "Please refer to https://curl.haxx.se/libcurl/c/libcurl-errors.html for error code: $_ret"
4c2a3841 1100 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
87ab2d90 1101 _err "Here is the curl dump log:"
1102 _err "$(cat "$_CURL_DUMP")"
1103 fi
687cfcc2 1104 fi
4c2a3841 1105 elif [ "$_ACME_WGET" ]; then
7834c252 1106 _WGET="$_ACME_WGET"
1107 if [ "$HTTPS_INSECURE" ]; then
1108 _WGET="$_WGET --no-check-certificate "
1109 fi
1110 _debug "_WGET" "$_WGET"
4c2a3841 1111 if [ "$needbase64" ]; then
1112 if [ "$httpmethod" = "POST" ]; then
7834c252 1113 response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --post-data="$body" "$url" 2>"$HTTP_HEADER" | _base64)"
8fb9a709 1114 else
7834c252 1115 response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --method $httpmethod --body-data="$body" "$url" 2>"$HTTP_HEADER" | _base64)"
8fb9a709 1116 fi
c60883ef 1117 else
4c2a3841 1118 if [ "$httpmethod" = "POST" ]; then
7834c252 1119 response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --post-data="$body" "$url" 2>"$HTTP_HEADER")"
8fb9a709 1120 else
7834c252 1121 response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --method $httpmethod --body-data="$body" "$url" 2>"$HTTP_HEADER")"
8fb9a709 1122 fi
c60883ef 1123 fi
16679b57 1124 _ret="$?"
4c2a3841 1125 if [ "$_ret" = "8" ]; then
9f43c270 1126 _ret=0
1127 _debug "wget returns 8, the server returns a 'Bad request' respons, lets process the response later."
1128 fi
4c2a3841 1129 if [ "$_ret" != "0" ]; then
1130 _err "Please refer to https://www.gnu.org/software/wget/manual/html_node/Exit-Status.html for error code: $_ret"
687cfcc2 1131 fi
c60883ef 1132 _sed_i "s/^ *//g" "$HTTP_HEADER"
d0b748a4 1133 else
1134 _ret="$?"
1135 _err "Neither curl nor wget is found, can not do $httpmethod."
c60883ef 1136 fi
16679b57 1137 _debug "_ret" "$_ret"
19539575 1138 printf "%s" "$response"
16679b57 1139 return $_ret
c60883ef 1140}
1141
75da0713 1142# url getheader timeout
c60883ef 1143_get() {
a4270efa 1144 _debug GET
c60883ef 1145 url="$1"
1146 onlyheader="$2"
75da0713 1147 t="$3"
79a267ab 1148 _debug url "$url"
75da0713 1149 _debug "timeout" "$t"
fac1e367 1150
1151 _inithttp
1152
4c2a3841 1153 if [ "$_ACME_CURL" ]; then
1befee5a 1154 _CURL="$_ACME_CURL"
7834c252 1155 if [ "$HTTPS_INSECURE" ]; then
1156 _CURL="$_CURL --insecure "
1157 fi
4c2a3841 1158 if [ "$t" ]; then
75da0713 1159 _CURL="$_CURL --connect-timeout $t"
1160 fi
1161 _debug "_CURL" "$_CURL"
4c2a3841 1162 if [ "$onlyheader" ]; then
f9a6988e 1163 $_CURL -I --user-agent "$USER_AGENT" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$url"
c60883ef 1164 else
f9a6988e 1165 $_CURL --user-agent "$USER_AGENT" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$url"
c60883ef 1166 fi
9aaf36cd 1167 ret=$?
4c2a3841 1168 if [ "$ret" != "0" ]; then
d529eb6d 1169 _err "Please refer to https://curl.haxx.se/libcurl/c/libcurl-errors.html for error code: $ret"
4c2a3841 1170 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
fac1e367 1171 _err "Here is the curl dump log:"
1172 _err "$(cat "$_CURL_DUMP")"
1173 fi
1174 fi
4c2a3841 1175 elif [ "$_ACME_WGET" ]; then
1befee5a 1176 _WGET="$_ACME_WGET"
7834c252 1177 if [ "$HTTPS_INSECURE" ]; then
1178 _WGET="$_WGET --no-check-certificate "
1179 fi
4c2a3841 1180 if [ "$t" ]; then
75da0713 1181 _WGET="$_WGET --timeout=$t"
1182 fi
1183 _debug "_WGET" "$_WGET"
4c2a3841 1184 if [ "$onlyheader" ]; then
f9a6988e 1185 $_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 1186 else
f9a6988e 1187 $_WGET --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" -O - "$url"
c60883ef 1188 fi
9aaf36cd 1189 ret=$?
4c2a3841 1190 if [ "$_ret" = "8" ]; then
9f43c270 1191 _ret=0
1192 _debug "wget returns 8, the server returns a 'Bad request' respons, lets process the response later."
1193 fi
4c2a3841 1194 if [ "$ret" != "0" ]; then
1195 _err "Please refer to https://www.gnu.org/software/wget/manual/html_node/Exit-Status.html for error code: $ret"
fac1e367 1196 fi
d0b748a4 1197 else
1198 ret=$?
1199 _err "Neither curl nor wget is found, can not do GET."
9aaf36cd 1200 fi
ec9fc8cb 1201 _debug "ret" "$ret"
c60883ef 1202 return $ret
1203}
166096dc 1204
c2c8f320 1205_head_n() {
79a267ab 1206 head -n "$1"
c2c8f320 1207}
1208
1209_tail_n() {
f9a6988e 1210 if ! tail -n "$1" 2>/dev/null; then
19ab2a29 1211 #fix for solaris
f9a6988e 1212 tail -"$1"
19ab2a29 1213 fi
c2c8f320 1214}
fac1e367 1215
166096dc 1216# url payload needbase64 keyfile
4c3b3608 1217_send_signed_request() {
1218 url=$1
1219 payload=$2
1220 needbase64=$3
166096dc 1221 keyfile=$4
4c2a3841 1222 if [ -z "$keyfile" ]; then
166096dc 1223 keyfile="$ACCOUNT_KEY_PATH"
1224 fi
f9a6988e 1225 _debug url "$url"
4c3b3608 1226 _debug payload "$payload"
4c2a3841 1227
1228 if ! _calcjwk "$keyfile"; then
166096dc 1229 return 1
1230 fi
c60883ef 1231
22ea4004 1232 payload64=$(printf "%s" "$payload" | _base64 | _urlencode)
f9a6988e 1233 _debug3 payload64 "$payload64"
4c2a3841 1234
1235 if [ -z "$_CACHED_NONCE" ]; then
00bcbd36 1236 _debug2 "Get nonce."
1237 nonceurl="$API/directory"
79a267ab 1238 _headers="$(_get "$nonceurl" "onlyheader")"
4c2a3841 1239
1240 if [ "$?" != "0" ]; then
00bcbd36 1241 _err "Can not connect to $nonceurl to get nonce."
1242 return 1
1243 fi
4c2a3841 1244
1cbf416b 1245 _debug2 _headers "$_headers"
4c2a3841 1246
1247 _CACHED_NONCE="$(echo "$_headers" | grep "Replay-Nonce:" | _head_n 1 | tr -d "\r\n " | cut -d ':' -f 2)"
1cbf416b 1248 _debug2 _CACHED_NONCE "$_CACHED_NONCE"
00bcbd36 1249 else
1250 _debug2 "Use _CACHED_NONCE" "$_CACHED_NONCE"
7012b91f 1251 fi
00bcbd36 1252 nonce="$_CACHED_NONCE"
1cbf416b 1253 _debug2 nonce "$nonce"
a272ee4f 1254
5982f4bc 1255 protected="$JWK_HEADERPLACE_PART1$nonce$JWK_HEADERPLACE_PART2"
22ea4004 1256 _debug3 protected "$protected"
4c2a3841 1257
95e06de5 1258 protected64="$(printf "%s" "$protected" | _base64 | _urlencode)"
22ea4004 1259 _debug3 protected64 "$protected64"
166096dc 1260
4c2a3841 1261 if ! _sig_t="$(printf "%s" "$protected64.$payload64" | _sign "$keyfile" "sha256")"; then
29b75109 1262 _err "Sign request failed."
1263 return 1
1264 fi
1265 _debug3 _sig_t "$_sig_t"
4c2a3841 1266
29b75109 1267 sig="$(printf "%s" "$_sig_t" | _urlencode)"
22ea4004 1268 _debug3 sig "$sig"
4c2a3841 1269
5982f4bc 1270 body="{\"header\": $JWK_HEADER, \"protected\": \"$protected64\", \"payload\": \"$payload64\", \"signature\": \"$sig\"}"
22ea4004 1271 _debug3 body "$body"
bbbdcb09 1272
f9a6988e 1273 response="$(_post "$body" "$url" "$needbase64")"
00bcbd36 1274 _CACHED_NONCE=""
4c2a3841 1275 if [ "$?" != "0" ]; then
9f43c270 1276 _err "Can not post to $url"
7012b91f 1277 return 1
1278 fi
eae29099 1279 _debug2 original "$response"
4c3b3608 1280
4c2a3841 1281 response="$(echo "$response" | _normalizeJson)"
4c3b3608 1282
00bcbd36 1283 responseHeaders="$(cat "$HTTP_HEADER")"
4c2a3841 1284
a63b05a9 1285 _debug2 responseHeaders "$responseHeaders"
4c2a3841 1286 _debug2 response "$response"
79a267ab 1287 code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\r\n")"
1288 _debug code "$code"
4c3b3608 1289
00bcbd36 1290 _CACHED_NONCE="$(echo "$responseHeaders" | grep "Replay-Nonce:" | _head_n 1 | tr -d "\r\n " | cut -d ':' -f 2)"
4c3b3608 1291
4c3b3608 1292}
4c3b3608 1293
1294#setopt "file" "opt" "=" "value" [";"]
1295_setopt() {
1296 __conf="$1"
1297 __opt="$2"
1298 __sep="$3"
1299 __val="$4"
1300 __end="$5"
4c2a3841 1301 if [ -z "$__opt" ]; then
1302 _usage usage: _setopt '"file" "opt" "=" "value" [";"]'
4c3b3608 1303 return
1304 fi
4c2a3841 1305 if [ ! -f "$__conf" ]; then
4c3b3608 1306 touch "$__conf"
1307 fi
1308
4c2a3841 1309 if grep -n "^$__opt$__sep" "$__conf" >/dev/null; then
22ea4004 1310 _debug3 OK
4c2a3841 1311 if _contains "$__val" "&"; then
79a267ab 1312 __val="$(echo "$__val" | sed 's/&/\\&/g')"
4c3b3608 1313 fi
79a267ab 1314 text="$(cat "$__conf")"
4c2a3841 1315 echo "$text" | sed "s|^$__opt$__sep.*$|$__opt$__sep$__val$__end|" >"$__conf"
4c3b3608 1316
4c2a3841 1317 elif grep -n "^#$__opt$__sep" "$__conf" >/dev/null; then
1318 if _contains "$__val" "&"; then
79a267ab 1319 __val="$(echo "$__val" | sed 's/&/\\&/g')"
4c3b3608 1320 fi
79a267ab 1321 text="$(cat "$__conf")"
4c2a3841 1322 echo "$text" | sed "s|^#$__opt$__sep.*$|$__opt$__sep$__val$__end|" >"$__conf"
4c3b3608 1323
1324 else
22ea4004 1325 _debug3 APP
4c2a3841 1326 echo "$__opt$__sep$__val$__end" >>"$__conf"
4c3b3608 1327 fi
79a267ab 1328 _debug2 "$(grep -n "^$__opt$__sep" "$__conf")"
4c3b3608 1329}
1330
8a29fbc8 1331#_save_conf file key value
1332#save to conf
1333_save_conf() {
1334 _s_c_f="$1"
1335 _sdkey="$2"
1336 _sdvalue="$3"
4c2a3841 1337 if [ "$_s_c_f" ]; then
8a29fbc8 1338 _setopt "$_s_c_f" "$_sdkey" "=" "'$_sdvalue'"
4d2f38b0 1339 else
8a29fbc8 1340 _err "config file is empty, can not save $_sdkey=$_sdvalue"
4d2f38b0 1341 fi
1342}
1343
8a29fbc8 1344#_clear_conf file key
1345_clear_conf() {
1346 _c_c_f="$1"
1347 _sdkey="$2"
4c2a3841 1348 if [ "$_c_c_f" ]; then
20ea8591 1349 _conf_data="$(cat "$_c_c_f")"
fa574fe8 1350 echo "$_conf_data" | sed "s/^$_sdkey *=.*$//" >"$_c_c_f"
4c3b3608 1351 else
8a29fbc8 1352 _err "config file is empty, can not clear"
4c3b3608 1353 fi
1354}
1355
8a29fbc8 1356#_read_conf file key
1357_read_conf() {
1358 _r_c_f="$1"
1359 _sdkey="$2"
4c2a3841 1360 if [ -f "$_r_c_f" ]; then
1361 (
79a267ab 1362 eval "$(grep "^$_sdkey *=" "$_r_c_f")"
4c2a3841 1363 eval "printf \"%s\" \"\$$_sdkey\""
1364 )
61623d22 1365 else
57e58ce7 1366 _debug "config file is empty, can not read $_sdkey"
4c3b3608 1367 fi
4c3b3608 1368}
1369
1370#_savedomainconf key value
1371#save to domain.conf
1372_savedomainconf() {
8a29fbc8 1373 _save_conf "$DOMAIN_CONF" "$1" "$2"
4d2f38b0 1374}
1375
1376#_cleardomainconf key
1377_cleardomainconf() {
8a29fbc8 1378 _clear_conf "$DOMAIN_CONF" "$1"
4c3b3608 1379}
1380
61623d22 1381#_readdomainconf key
1382_readdomainconf() {
8a29fbc8 1383 _read_conf "$DOMAIN_CONF" "$1"
61623d22 1384}
1385
4c3b3608 1386#_saveaccountconf key value
1387_saveaccountconf() {
8a29fbc8 1388 _save_conf "$ACCOUNT_CONF_PATH" "$1" "$2"
4c3b3608 1389}
1390
fac1e367 1391#_clearaccountconf key
1392_clearaccountconf() {
8a29fbc8 1393 _clear_conf "$ACCOUNT_CONF_PATH" "$1"
1394}
1395
1396#_savecaconf key value
1397_savecaconf() {
1398 _save_conf "$CA_CONF" "$1" "$2"
1399}
1400
1401#_readcaconf key
1402_readcaconf() {
1403 _read_conf "$CA_CONF" "$1"
1404}
1405
1406#_clearaccountconf key
1407_clearcaconf() {
1408 _clear_conf "$CA_CONF" "$1"
fac1e367 1409}
1410
0463b5d6 1411# content localaddress
4c3b3608 1412_startserver() {
1413 content="$1"
0463b5d6 1414 ncaddr="$2"
1415 _debug "ncaddr" "$ncaddr"
1416
6fc1447f 1417 _debug "startserver: $$"
1b2e940d 1418 nchelp="$(nc -h 2>&1)"
4c2a3841 1419
39c8f79f 1420 _debug Le_HTTPPort "$Le_HTTPPort"
6ae0f7f5 1421 _debug Le_Listen_V4 "$Le_Listen_V4"
1422 _debug Le_Listen_V6 "$Le_Listen_V6"
f78babfa 1423 _NC="nc"
4c2a3841 1424
1425 if [ "$Le_Listen_V4" ]; then
6ae0f7f5 1426 _NC="$_NC -4"
4c2a3841 1427 elif [ "$Le_Listen_V6" ]; then
6ae0f7f5 1428 _NC="$_NC -6"
1429 fi
4c2a3841 1430
1431 if echo "$nchelp" | grep "\-q[ ,]" >/dev/null; then
f78babfa 1432 _NC="$_NC -q 1 -l $ncaddr"
1433 else
4c2a3841 1434 if echo "$nchelp" | grep "GNU netcat" >/dev/null && echo "$nchelp" | grep "\-c, \-\-close" >/dev/null; then
f78babfa 1435 _NC="$_NC -c -l $ncaddr"
4c2a3841 1436 elif echo "$nchelp" | grep "\-N" | grep "Shutdown the network socket after EOF on stdin" >/dev/null; then
f78babfa 1437 _NC="$_NC -N -l $ncaddr"
1438 else
1439 _NC="$_NC -l $ncaddr"
1440 fi
1441 fi
1442
6ae0f7f5 1443 _debug "_NC" "$_NC"
1444
c9febbdd 1445 #for centos ncat
4c2a3841 1446 if _contains "$nchelp" "nmap.org"; then
c9febbdd 1447 _debug "Using ncat: nmap.org"
3e5b1024 1448 if ! _exec "printf \"%s\r\n\r\n%s\" \"HTTP/1.1 200 OK\" \"$content\" | $_NC \"$Le_HTTPPort\" >&2"; then
1449 _exec_err
1450 return 1
c9febbdd 1451 fi
fa574fe8 1452 if [ "$DEBUG" ]; then
3e5b1024 1453 _exec_err
1454 fi
1455 return
c9febbdd 1456 fi
4c2a3841 1457
1458 # while true ; do
3e5b1024 1459 if ! _exec "printf \"%s\r\n\r\n%s\" \"HTTP/1.1 200 OK\" \"$content\" | $_NC -p \"$Le_HTTPPort\" >&2"; then
1460 _exec "printf \"%s\r\n\r\n%s\" \"HTTP/1.1 200 OK\" \"$content\" | $_NC \"$Le_HTTPPort\" >&2"
4c2a3841 1461 fi
3e5b1024 1462
4c2a3841 1463 if [ "$?" != "0" ]; then
1464 _err "nc listen error."
3e5b1024 1465 _exec_err
4c2a3841 1466 exit 1
1467 fi
fa574fe8 1468 if [ "$DEBUG" ]; then
3e5b1024 1469 _exec_err
1470 fi
4c2a3841 1471 # done
4c3b3608 1472}
1473
4c2a3841 1474_stopserver() {
4c3b3608 1475 pid="$1"
6fc1447f 1476 _debug "pid" "$pid"
4c2a3841 1477 if [ -z "$pid" ]; then
6fc1447f 1478 return
1479 fi
e22bcf7c 1480
dcf9cb58 1481 _debug2 "Le_HTTPPort" "$Le_HTTPPort"
4c2a3841 1482 if [ "$Le_HTTPPort" ]; then
1483 if [ "$DEBUG" ] && [ "$DEBUG" -gt "3" ]; then
22ea4004 1484 _get "http://localhost:$Le_HTTPPort" "" 1
dcf9cb58 1485 else
22ea4004 1486 _get "http://localhost:$Le_HTTPPort" "" 1 >/dev/null 2>&1
dcf9cb58 1487 fi
1488 fi
4c2a3841 1489
dcf9cb58 1490 _debug2 "Le_TLSPort" "$Le_TLSPort"
4c2a3841 1491 if [ "$Le_TLSPort" ]; then
1492 if [ "$DEBUG" ] && [ "$DEBUG" -gt "3" ]; then
75da0713 1493 _get "https://localhost:$Le_TLSPort" "" 1
1494 _get "https://localhost:$Le_TLSPort" "" 1
dcf9cb58 1495 else
75da0713 1496 _get "https://localhost:$Le_TLSPort" "" 1 >/dev/null 2>&1
1497 _get "https://localhost:$Le_TLSPort" "" 1 >/dev/null 2>&1
dcf9cb58 1498 fi
1499 fi
4c3b3608 1500}
1501
fdcb6b72 1502# sleep sec
1503_sleep() {
1504 _sleep_sec="$1"
4c2a3841 1505 if [ "$__INTERACTIVE" ]; then
fdcb6b72 1506 _sleep_c="$_sleep_sec"
4c2a3841 1507 while [ "$_sleep_c" -ge "0" ]; do
c583d6bb 1508 printf "\r \r"
fdcb6b72 1509 __green "$_sleep_c"
79a267ab 1510 _sleep_c="$(_math "$_sleep_c" - 1)"
fdcb6b72 1511 sleep 1
1512 done
c583d6bb 1513 printf "\r"
fdcb6b72 1514 else
1515 sleep "$_sleep_sec"
1516 fi
1517}
e22bcf7c 1518
6ae0f7f5 1519# _starttlsserver san_a san_b port content _ncaddr
e22bcf7c 1520_starttlsserver() {
1521 _info "Starting tls server."
1522 san_a="$1"
1523 san_b="$2"
1524 port="$3"
1525 content="$4"
6ae0f7f5 1526 opaddr="$5"
4c2a3841 1527
e22bcf7c 1528 _debug san_a "$san_a"
1529 _debug san_b "$san_b"
1530 _debug port "$port"
4c2a3841 1531
e22bcf7c 1532 #create key TLS_KEY
4c2a3841 1533 if ! _createkey "2048" "$TLS_KEY"; then
e22bcf7c 1534 _err "Create tls validation key error."
1535 return 1
1536 fi
4c2a3841 1537
e22bcf7c 1538 #create csr
1539 alt="$san_a"
4c2a3841 1540 if [ "$san_b" ]; then
e22bcf7c 1541 alt="$alt,$san_b"
1542 fi
4c2a3841 1543 if ! _createcsr "tls.acme.sh" "$alt" "$TLS_KEY" "$TLS_CSR" "$TLS_CONF"; then
e22bcf7c 1544 _err "Create tls validation csr error."
1545 return 1
1546 fi
4c2a3841 1547
e22bcf7c 1548 #self signed
4c2a3841 1549 if ! _signcsr "$TLS_KEY" "$TLS_CSR" "$TLS_CONF" "$TLS_CERT"; then
e22bcf7c 1550 _err "Create tls validation cert error."
1551 return 1
1552 fi
4c2a3841 1553
a746139c 1554 __S_OPENSSL="$OPENSSL_BIN s_server -cert $TLS_CERT -key $TLS_KEY "
4c2a3841 1555 if [ "$opaddr" ]; then
6ae0f7f5 1556 __S_OPENSSL="$__S_OPENSSL -accept $opaddr:$port"
1557 else
1558 __S_OPENSSL="$__S_OPENSSL -accept $port"
1559 fi
1560
1561 _debug Le_Listen_V4 "$Le_Listen_V4"
1562 _debug Le_Listen_V6 "$Le_Listen_V6"
4c2a3841 1563 if [ "$Le_Listen_V4" ]; then
6ae0f7f5 1564 __S_OPENSSL="$__S_OPENSSL -4"
4c2a3841 1565 elif [ "$Le_Listen_V6" ]; then
6ae0f7f5 1566 __S_OPENSSL="$__S_OPENSSL -6"
1567 fi
4c2a3841 1568
6ae0f7f5 1569 _debug "$__S_OPENSSL"
4c2a3841 1570 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
d5ec5f80 1571 (printf "%s\r\n\r\n%s" "HTTP/1.1 200 OK" "$content" | $__S_OPENSSL -tlsextdebug) &
331c4bb6 1572 else
d5ec5f80 1573 (printf "%s\r\n\r\n%s" "HTTP/1.1 200 OK" "$content" | $__S_OPENSSL >/dev/null 2>&1) &
331c4bb6 1574 fi
1575
e22bcf7c 1576 serverproc="$!"
5dbf664a 1577 sleep 1
d5ec5f80 1578 _debug serverproc "$serverproc"
e22bcf7c 1579}
1580
18e46962 1581#file
1582_readlink() {
1583 _rf="$1"
1584 if ! readlink -f "$_rf" 2>/dev/null; then
4c2a3841 1585 if _startswith "$_rf" "\./$PROJECT_ENTRY"; then
7da50703 1586 printf -- "%s" "$(pwd)/$PROJECT_ENTRY"
1587 return 0
1588 fi
4c2a3841 1589 readlink "$_rf"
18e46962 1590 fi
1591}
1592
5ea6e9c9 1593__initHome() {
4c2a3841 1594 if [ -z "$_SCRIPT_HOME" ]; then
1595 if _exists readlink && _exists dirname; then
66990cf8 1596 _debug "Lets find script dir."
f3e4cea3 1597 _debug "_SCRIPT_" "$_SCRIPT_"
18e46962 1598 _script="$(_readlink "$_SCRIPT_")"
f3e4cea3 1599 _debug "_script" "$_script"
1600 _script_home="$(dirname "$_script")"
1601 _debug "_script_home" "$_script_home"
4c2a3841 1602 if [ -d "$_script_home" ]; then
f3e4cea3 1603 _SCRIPT_HOME="$_script_home"
1604 else
1605 _err "It seems the script home is not correct:$_script_home"
1606 fi
1607 fi
1608 fi
1609
4c2a3841 1610 if [ -z "$LE_WORKING_DIR" ]; then
1611 if [ -f "$DEFAULT_INSTALL_HOME/account.conf" ]; then
7da50703 1612 _debug "It seems that $PROJECT_NAME is already installed in $DEFAULT_INSTALL_HOME"
f3e4cea3 1613 LE_WORKING_DIR="$DEFAULT_INSTALL_HOME"
1614 else
1615 LE_WORKING_DIR="$_SCRIPT_HOME"
1616 fi
4c3b3608 1617 fi
4c2a3841 1618
1619 if [ -z "$LE_WORKING_DIR" ]; then
f3e4cea3 1620 _debug "Using default home:$DEFAULT_INSTALL_HOME"
1621 LE_WORKING_DIR="$DEFAULT_INSTALL_HOME"
1622 fi
7da50703 1623 export LE_WORKING_DIR
f3e4cea3 1624
d53289d7 1625 _DEFAULT_ACCOUNT_CONF_PATH="$LE_WORKING_DIR/account.conf"
1626
4c2a3841 1627 if [ -z "$ACCOUNT_CONF_PATH" ]; then
1628 if [ -f "$_DEFAULT_ACCOUNT_CONF_PATH" ]; then
8663fb7e 1629 . "$_DEFAULT_ACCOUNT_CONF_PATH"
635695ec 1630 fi
d53289d7 1631 fi
4c2a3841 1632
1633 if [ -z "$ACCOUNT_CONF_PATH" ]; then
d53289d7 1634 ACCOUNT_CONF_PATH="$_DEFAULT_ACCOUNT_CONF_PATH"
4c3b3608 1635 fi
4c2a3841 1636
d0871bda 1637 DEFAULT_LOG_FILE="$LE_WORKING_DIR/$PROJECT_NAME.log"
4c2a3841 1638
5c48e139 1639 DEFAULT_CA_HOME="$LE_WORKING_DIR/ca"
4c2a3841 1640
1641 if [ -z "$LE_TEMP_DIR" ]; then
610e0f21 1642 LE_TEMP_DIR="$LE_WORKING_DIR/tmp"
1643 fi
5ea6e9c9 1644}
1645
1646#[domain] [keylength]
1647_initpath() {
1648
1649 __initHome
1650
4c2a3841 1651 if [ -f "$ACCOUNT_CONF_PATH" ]; then
8663fb7e 1652 . "$ACCOUNT_CONF_PATH"
4c3b3608 1653 fi
1654
4c2a3841 1655 if [ "$IN_CRON" ]; then
1656 if [ ! "$_USER_PATH_EXPORTED" ]; then
281aa349 1657 _USER_PATH_EXPORTED=1
1658 export PATH="$USER_PATH:$PATH"
1659 fi
1660 fi
4c2a3841 1661
1662 if [ -z "$CA_HOME" ]; then
5c48e139 1663 CA_HOME="$DEFAULT_CA_HOME"
1664 fi
281aa349 1665
4c2a3841 1666 if [ -z "$API" ]; then
1667 if [ -z "$STAGE" ]; then
4c3b3608 1668 API="$DEFAULT_CA"
1669 else
1670 API="$STAGE_CA"
1671 _info "Using stage api:$API"
4c2a3841 1672 fi
4c3b3608 1673 fi
4c2a3841 1674
5c48e139 1675 _API_HOST="$(echo "$API" | cut -d : -f 2 | tr -d '/')"
1676 CA_DIR="$CA_HOME/$_API_HOST"
4c2a3841 1677
5c48e139 1678 _DEFAULT_CA_CONF="$CA_DIR/ca.conf"
4c2a3841 1679
1680 if [ -z "$CA_CONF" ]; then
5c48e139 1681 CA_CONF="$_DEFAULT_CA_CONF"
1682 fi
4c2a3841 1683
1684 if [ -f "$CA_CONF" ]; then
5c48e139 1685 . "$CA_CONF"
1686 fi
1687
4c2a3841 1688 if [ -z "$ACME_DIR" ]; then
4c3b3608 1689 ACME_DIR="/home/.acme"
1690 fi
4c2a3841 1691
1692 if [ -z "$APACHE_CONF_BACKUP_DIR" ]; then
8a144f4d 1693 APACHE_CONF_BACKUP_DIR="$LE_WORKING_DIR"
4c3b3608 1694 fi
4c2a3841 1695
1696 if [ -z "$USER_AGENT" ]; then
bbbdcb09 1697 USER_AGENT="$DEFAULT_USER_AGENT"
1698 fi
4c2a3841 1699
1700 if [ -z "$HTTP_HEADER" ]; then
933c169d 1701 HTTP_HEADER="$LE_WORKING_DIR/http.header"
1702 fi
b2817897 1703
5c48e139 1704 _OLD_ACCOUNT_KEY="$LE_WORKING_DIR/account.key"
1705 _OLD_ACCOUNT_JSON="$LE_WORKING_DIR/account.json"
4c2a3841 1706
5c48e139 1707 _DEFAULT_ACCOUNT_KEY_PATH="$CA_DIR/account.key"
1708 _DEFAULT_ACCOUNT_JSON_PATH="$CA_DIR/account.json"
4c2a3841 1709 if [ -z "$ACCOUNT_KEY_PATH" ]; then
b2817897 1710 ACCOUNT_KEY_PATH="$_DEFAULT_ACCOUNT_KEY_PATH"
4c3b3608 1711 fi
4c2a3841 1712
1713 if [ -z "$ACCOUNT_JSON_PATH" ]; then
5c48e139 1714 ACCOUNT_JSON_PATH="$_DEFAULT_ACCOUNT_JSON_PATH"
1715 fi
4c2a3841 1716
a79b26af 1717 _DEFAULT_CERT_HOME="$LE_WORKING_DIR"
4c2a3841 1718 if [ -z "$CERT_HOME" ]; then
a79b26af
RD
1719 CERT_HOME="$_DEFAULT_CERT_HOME"
1720 fi
1721
a746139c 1722 if [ -z "$OPENSSL_BIN" ]; then
1723 OPENSSL_BIN="$DEFAULT_OPENSSL_BIN"
1724 fi
1725
4c2a3841 1726 if [ -z "$1" ]; then
4c3b3608 1727 return 0
1728 fi
4c2a3841 1729
5fbc47eb 1730 domain="$1"
1731 _ilength="$2"
4c3b3608 1732
4c2a3841 1733 if [ -z "$DOMAIN_PATH" ]; then
43822d37 1734 domainhome="$CERT_HOME/$domain"
1735 domainhomeecc="$CERT_HOME/$domain$ECC_SUFFIX"
4c2a3841 1736
4c3b3608 1737 DOMAIN_PATH="$domainhome"
4c2a3841 1738
1739 if _isEccKey "$_ilength"; then
43822d37 1740 DOMAIN_PATH="$domainhomeecc"
1741 else
4c2a3841 1742 if [ ! -d "$domainhome" ] && [ -d "$domainhomeecc" ]; then
6d4e903b 1743 _info "The domain '$domain' seems to have a ECC cert already, please add '$(__red "--ecc")' parameter if you want to use that cert."
43822d37 1744 fi
1745 fi
1746 _debug DOMAIN_PATH "$DOMAIN_PATH"
4c3b3608 1747 fi
4c2a3841 1748
4c2a3841 1749 if [ -z "$DOMAIN_CONF" ]; then
43822d37 1750 DOMAIN_CONF="$DOMAIN_PATH/$domain.conf"
4c3b3608 1751 fi
4c2a3841 1752
1753 if [ -z "$DOMAIN_SSL_CONF" ]; then
0c9546cc 1754 DOMAIN_SSL_CONF="$DOMAIN_PATH/$domain.csr.conf"
4c3b3608 1755 fi
4c2a3841 1756
1757 if [ -z "$CSR_PATH" ]; then
43822d37 1758 CSR_PATH="$DOMAIN_PATH/$domain.csr"
4c3b3608 1759 fi
4c2a3841 1760 if [ -z "$CERT_KEY_PATH" ]; then
43822d37 1761 CERT_KEY_PATH="$DOMAIN_PATH/$domain.key"
4c3b3608 1762 fi
4c2a3841 1763 if [ -z "$CERT_PATH" ]; then
43822d37 1764 CERT_PATH="$DOMAIN_PATH/$domain.cer"
4c3b3608 1765 fi
4c2a3841 1766 if [ -z "$CA_CERT_PATH" ]; then
43822d37 1767 CA_CERT_PATH="$DOMAIN_PATH/ca.cer"
4c3b3608 1768 fi
4c2a3841 1769 if [ -z "$CERT_FULLCHAIN_PATH" ]; then
43822d37 1770 CERT_FULLCHAIN_PATH="$DOMAIN_PATH/fullchain.cer"
caf1fc10 1771 fi
4c2a3841 1772 if [ -z "$CERT_PFX_PATH" ]; then
43822d37 1773 CERT_PFX_PATH="$DOMAIN_PATH/$domain.pfx"
ac2d5123 1774 fi
4c2a3841 1775
1776 if [ -z "$TLS_CONF" ]; then
43822d37 1777 TLS_CONF="$DOMAIN_PATH/tls.valdation.conf"
e22bcf7c 1778 fi
4c2a3841 1779 if [ -z "$TLS_CERT" ]; then
43822d37 1780 TLS_CERT="$DOMAIN_PATH/tls.valdation.cert"
e22bcf7c 1781 fi
4c2a3841 1782 if [ -z "$TLS_KEY" ]; then
43822d37 1783 TLS_KEY="$DOMAIN_PATH/tls.valdation.key"
e22bcf7c 1784 fi
4c2a3841 1785 if [ -z "$TLS_CSR" ]; then
43822d37 1786 TLS_CSR="$DOMAIN_PATH/tls.valdation.csr"
e22bcf7c 1787 fi
4c2a3841 1788
4c3b3608 1789}
1790
610e0f21 1791_exec() {
4c2a3841 1792 if [ -z "$_EXEC_TEMP_ERR" ]; then
610e0f21 1793 _EXEC_TEMP_ERR="$(_mktemp)"
1794 fi
1795
4c2a3841 1796 if [ "$_EXEC_TEMP_ERR" ]; then
3e5b1024 1797 eval "$@ 2>>$_EXEC_TEMP_ERR"
610e0f21 1798 else
3e5b1024 1799 eval "$@"
610e0f21 1800 fi
1801}
1802
1803_exec_err() {
3e5b1024 1804 [ "$_EXEC_TEMP_ERR" ] && _err "$(cat "$_EXEC_TEMP_ERR")" && echo "" >"$_EXEC_TEMP_ERR"
610e0f21 1805}
4c3b3608 1806
1807_apachePath() {
c3dd3ef0 1808 _APACHECTL="apachectl"
4c2a3841 1809 if ! _exists apachectl; then
1810 if _exists apache2ctl; then
1811 _APACHECTL="apache2ctl"
e4a19585 1812 else
bc96082f 1813 _err "'apachectl not found. It seems that apache is not installed, or you are not root user.'"
e4a19585 1814 _err "Please use webroot mode to try again."
1815 return 1
1816 fi
80a0a7b5 1817 fi
4c2a3841 1818
1819 if ! _exec $_APACHECTL -V >/dev/null; then
610e0f21 1820 _exec_err
1821 return 1
1822 fi
4c2a3841 1823
1824 if [ "$APACHE_HTTPD_CONF" ]; then
5be1449d 1825 _saveaccountconf APACHE_HTTPD_CONF "$APACHE_HTTPD_CONF"
1826 httpdconf="$APACHE_HTTPD_CONF"
79a267ab 1827 httpdconfname="$(basename "$httpdconfname")"
d62ee940 1828 else
4c2a3841 1829 httpdconfname="$($_APACHECTL -V | grep SERVER_CONFIG_FILE= | cut -d = -f 2 | tr -d '"')"
5be1449d 1830 _debug httpdconfname "$httpdconfname"
4c2a3841 1831
1832 if [ -z "$httpdconfname" ]; then
5be1449d 1833 _err "Can not read apache config file."
1834 return 1
1835 fi
4c2a3841 1836
1837 if _startswith "$httpdconfname" '/'; then
5be1449d 1838 httpdconf="$httpdconfname"
79a267ab 1839 httpdconfname="$(basename "$httpdconfname")"
5be1449d 1840 else
4c2a3841 1841 httpdroot="$($_APACHECTL -V | grep HTTPD_ROOT= | cut -d = -f 2 | tr -d '"')"
5be1449d 1842 _debug httpdroot "$httpdroot"
1843 httpdconf="$httpdroot/$httpdconfname"
79a267ab 1844 httpdconfname="$(basename "$httpdconfname")"
5be1449d 1845 fi
d62ee940 1846 fi
78768e98 1847 _debug httpdconf "$httpdconf"
8f63baf7 1848 _debug httpdconfname "$httpdconfname"
4c2a3841 1849 if [ ! -f "$httpdconf" ]; then
78768e98 1850 _err "Apache Config file not found" "$httpdconf"
4c3b3608 1851 return 1
1852 fi
1853 return 0
1854}
1855
1856_restoreApache() {
4c2a3841 1857 if [ -z "$usingApache" ]; then
4c3b3608 1858 return 0
1859 fi
1860 _initpath
4c2a3841 1861 if ! _apachePath; then
4c3b3608 1862 return 1
1863 fi
4c2a3841 1864
1865 if [ ! -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname" ]; then
4c3b3608 1866 _debug "No config file to restore."
1867 return 0
1868 fi
4c2a3841 1869
1870 cat "$APACHE_CONF_BACKUP_DIR/$httpdconfname" >"$httpdconf"
5ef501c5 1871 _debug "Restored: $httpdconf."
4c2a3841 1872 if ! _exec $_APACHECTL -t; then
610e0f21 1873 _exec_err
4c3b3608 1874 _err "Sorry, restore apache config error, please contact me."
4c2a3841 1875 return 1
4c3b3608 1876 fi
5ef501c5 1877 _debug "Restored successfully."
4c3b3608 1878 rm -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname"
4c2a3841 1879 return 0
4c3b3608 1880}
1881
1882_setApache() {
1883 _initpath
4c2a3841 1884 if ! _apachePath; then
4c3b3608 1885 return 1
1886 fi
1887
5fc5016d 1888 #test the conf first
869578ce 1889 _info "Checking if there is an error in the apache config file before starting."
4c2a3841 1890
44edb2bd 1891 if ! _exec "$_APACHECTL" -t >/dev/null; then
610e0f21 1892 _exec_err
1893 _err "The apache config file has error, please fix it first, then try again."
869578ce 1894 _err "Don't worry, there is nothing changed to your system."
4c2a3841 1895 return 1
5fc5016d 1896 else
1897 _info "OK"
1898 fi
4c2a3841 1899
4c3b3608 1900 #backup the conf
5778811a 1901 _debug "Backup apache config file" "$httpdconf"
4c2a3841 1902 if ! cp "$httpdconf" "$APACHE_CONF_BACKUP_DIR/"; then
869578ce 1903 _err "Can not backup apache config file, so abort. Don't worry, the apache config is not changed."
8f63baf7 1904 _err "This might be a bug of $PROJECT_NAME , pleae report issue: $PROJECT"
1905 return 1
1906 fi
4c3b3608 1907 _info "JFYI, Config file $httpdconf is backuped to $APACHE_CONF_BACKUP_DIR/$httpdconfname"
1908 _info "In case there is an error that can not be restored automatically, you may try restore it yourself."
1909 _info "The backup file will be deleted on sucess, just forget it."
4c2a3841 1910
4c3b3608 1911 #add alias
4c2a3841 1912
1913 apacheVer="$($_APACHECTL -V | grep "Server version:" | cut -d : -f 2 | cut -d " " -f 2 | cut -d '/' -f 2)"
b09d597c 1914 _debug "apacheVer" "$apacheVer"
1915 apacheMajer="$(echo "$apacheVer" | cut -d . -f 1)"
1916 apacheMinor="$(echo "$apacheVer" | cut -d . -f 2)"
1917
4c2a3841 1918 if [ "$apacheVer" ] && [ "$apacheMajer$apacheMinor" -ge "24" ]; then
b09d597c 1919 echo "
4c3b3608 1920Alias /.well-known/acme-challenge $ACME_DIR
1921
1922<Directory $ACME_DIR >
1923Require all granted
b09d597c 1924</Directory>
4c2a3841 1925 " >>"$httpdconf"
b09d597c 1926 else
1927 echo "
1928Alias /.well-known/acme-challenge $ACME_DIR
1929
1930<Directory $ACME_DIR >
1931Order allow,deny
1932Allow from all
4c3b3608 1933</Directory>
4c2a3841 1934 " >>"$httpdconf"
b09d597c 1935 fi
1936
4c2a3841 1937 _msg="$($_APACHECTL -t 2>&1)"
1938 if [ "$?" != "0" ]; then
5fc5016d 1939 _err "Sorry, apache config error"
4c2a3841 1940 if _restoreApache; then
869578ce 1941 _err "The apache config file is restored."
5fc5016d 1942 else
869578ce 1943 _err "Sorry, The apache config file can not be restored, please report bug."
5fc5016d 1944 fi
4c2a3841 1945 return 1
4c3b3608 1946 fi
4c2a3841 1947
1948 if [ ! -d "$ACME_DIR" ]; then
4c3b3608 1949 mkdir -p "$ACME_DIR"
1950 chmod 755 "$ACME_DIR"
1951 fi
4c2a3841 1952
44edb2bd 1953 if ! _exec "$_APACHECTL" graceful; then
4c2a3841 1954 _exec_err
610e0f21 1955 _err "$_APACHECTL graceful error, please contact me."
4c3b3608 1956 _restoreApache
4c2a3841 1957 return 1
4c3b3608 1958 fi
1959 usingApache="1"
1960 return 0
1961}
1962
5ef501c5 1963_clearup() {
44edb2bd 1964 _stopserver "$serverproc"
4c3b3608 1965 serverproc=""
1966 _restoreApache
800e3f45 1967 _clearupdns
4c2a3841 1968 if [ -z "$DEBUG" ]; then
e22bcf7c 1969 rm -f "$TLS_CONF"
1970 rm -f "$TLS_CERT"
1971 rm -f "$TLS_KEY"
1972 rm -f "$TLS_CSR"
1973 fi
4c3b3608 1974}
1975
800e3f45 1976_clearupdns() {
1977 _debug "_clearupdns"
4c2a3841 1978 if [ "$dnsadded" != 1 ] || [ -z "$vlist" ]; then
93fc48a2 1979 _debug "Dns not added, skip."
800e3f45 1980 return
1981 fi
1982
4c2a3841 1983 ventries=$(echo "$vlist" | tr ',' ' ')
1984 for ventry in $ventries; do
0c538f75 1985 d=$(echo "$ventry" | cut -d "$sep" -f 1)
1986 keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2)
1987 vtype=$(echo "$ventry" | cut -d "$sep" -f 4)
1988 _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5)
800e3f45 1989
4c2a3841 1990 if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
800e3f45 1991 _info "$d is already verified, skip $vtype."
1992 continue
1993 fi
1994
4c2a3841 1995 if [ "$vtype" != "$VTYPE_DNS" ]; then
800e3f45 1996 _info "Skip $d for $vtype"
1997 continue
1998 fi
4c2a3841 1999
0c538f75 2000 d_api="$(_findHook "$d" dnsapi "$_currentRoot")"
800e3f45 2001 _debug d_api "$d_api"
4c2a3841 2002
2003 if [ -z "$d_api" ]; then
800e3f45 2004 _info "Not Found domain api file: $d_api"
2005 continue
2006 fi
4c2a3841 2007
800e3f45 2008 (
d5ec5f80 2009 if ! . "$d_api"; then
800e3f45 2010 _err "Load file $d_api error. Please check your api file and try again."
2011 return 1
2012 fi
4c2a3841 2013
800e3f45 2014 rmcommand="${_currentRoot}_rm"
d5ec5f80 2015 if ! _exists "$rmcommand"; then
800e3f45 2016 _err "It seems that your api file doesn't define $rmcommand"
2017 return 1
2018 fi
4c2a3841 2019
800e3f45 2020 txtdomain="_acme-challenge.$d"
4c2a3841 2021
d5ec5f80 2022 if ! $rmcommand "$txtdomain"; then
800e3f45 2023 _err "Error removing txt for domain:$txtdomain"
2024 return 1
2025 fi
2026 )
4c2a3841 2027
800e3f45 2028 done
2029}
2030
4c3b3608 2031# webroot removelevel tokenfile
2032_clearupwebbroot() {
2033 __webroot="$1"
4c2a3841 2034 if [ -z "$__webroot" ]; then
4c3b3608 2035 _debug "no webroot specified, skip"
2036 return 0
2037 fi
4c2a3841 2038
dcf9cb58 2039 _rmpath=""
4c2a3841 2040 if [ "$2" = '1' ]; then
dcf9cb58 2041 _rmpath="$__webroot/.well-known"
4c2a3841 2042 elif [ "$2" = '2' ]; then
dcf9cb58 2043 _rmpath="$__webroot/.well-known/acme-challenge"
4c2a3841 2044 elif [ "$2" = '3' ]; then
dcf9cb58 2045 _rmpath="$__webroot/.well-known/acme-challenge/$3"
4c3b3608 2046 else
cc179731 2047 _debug "Skip for removelevel:$2"
4c3b3608 2048 fi
4c2a3841 2049
2050 if [ "$_rmpath" ]; then
2051 if [ "$DEBUG" ]; then
dcf9cb58 2052 _debug "Debugging, skip removing: $_rmpath"
2053 else
2054 rm -rf "$_rmpath"
2055 fi
2056 fi
4c2a3841 2057
4c3b3608 2058 return 0
2059
2060}
2061
b0070f03 2062_on_before_issue() {
30c2d84c 2063 _debug _on_before_issue
4c2a3841 2064 if _hasfield "$Le_Webroot" "$NO_VALUE"; then
2065 if ! _exists "nc"; then
0463b5d6 2066 _err "Please install netcat(nc) tools first."
2067 return 1
2068 fi
0463b5d6 2069 fi
2070
2071 _debug Le_LocalAddress "$Le_LocalAddress"
4c2a3841 2072
2073 alldomains=$(echo "$Le_Domain,$Le_Alt" | tr ',' ' ')
0463b5d6 2074 _index=1
2075 _currentRoot=""
2076 _addrIndex=1
4c2a3841 2077 for d in $alldomains; do
d5ec5f80 2078 _debug "Check for domain" "$d"
0463b5d6 2079 _currentRoot="$(_getfield "$Le_Webroot" $_index)"
2080 _debug "_currentRoot" "$_currentRoot"
2081 _index=$(_math $_index + 1)
2082 _checkport=""
4c2a3841 2083 if [ "$_currentRoot" = "$NO_VALUE" ]; then
0463b5d6 2084 _info "Standalone mode."
4c2a3841 2085 if [ -z "$Le_HTTPPort" ]; then
0463b5d6 2086 Le_HTTPPort=80
2087 else
4c2a3841 2088 _savedomainconf "Le_HTTPPort" "$Le_HTTPPort"
0463b5d6 2089 fi
2090 _checkport="$Le_HTTPPort"
4c2a3841 2091 elif [ "$_currentRoot" = "$W_TLS" ]; then
0463b5d6 2092 _info "Standalone tls mode."
4c2a3841 2093 if [ -z "$Le_TLSPort" ]; then
0463b5d6 2094 Le_TLSPort=443
2095 else
4c2a3841 2096 _savedomainconf "Le_TLSPort" "$Le_TLSPort"
0463b5d6 2097 fi
2098 _checkport="$Le_TLSPort"
2099 fi
4c2a3841 2100
2101 if [ "$_checkport" ]; then
0463b5d6 2102 _debug _checkport "$_checkport"
2103 _checkaddr="$(_getfield "$Le_LocalAddress" $_addrIndex)"
2104 _debug _checkaddr "$_checkaddr"
4c2a3841 2105
0463b5d6 2106 _addrIndex="$(_math $_addrIndex + 1)"
4c2a3841 2107
0463b5d6 2108 _netprc="$(_ss "$_checkport" | grep "$_checkport")"
2109 netprc="$(echo "$_netprc" | grep "$_checkaddr")"
4c2a3841 2110 if [ -z "$netprc" ]; then
0463b5d6 2111 netprc="$(echo "$_netprc" | grep "$LOCAL_ANY_ADDRESS")"
2112 fi
4c2a3841 2113 if [ "$netprc" ]; then
0463b5d6 2114 _err "$netprc"
4c2a3841 2115 _err "tcp port $_checkport is already used by $(echo "$netprc" | cut -d : -f 4)"
0463b5d6 2116 _err "Please stop it first"
2117 return 1
2118 fi
2119 fi
2120 done
2121
4c2a3841 2122 if _hasfield "$Le_Webroot" "apache"; then
2123 if ! _setApache; then
0463b5d6 2124 _err "set up apache error. Report error to me."
2125 return 1
2126 fi
2127 else
2128 usingApache=""
2129 fi
2130
b0070f03 2131 #run pre hook
4c2a3841 2132 if [ "$Le_PreHook" ]; then
b0070f03 2133 _info "Run pre hook:'$Le_PreHook'"
2134 if ! (
2135 cd "$DOMAIN_PATH" && eval "$Le_PreHook"
4c2a3841 2136 ); then
b0070f03 2137 _err "Error when run pre hook."
2138 return 1
2139 fi
2140 fi
2141}
2142
2143_on_issue_err() {
30c2d84c 2144 _debug _on_issue_err
4c2a3841 2145 if [ "$LOG_FILE" ]; then
a73c5b33 2146 _err "Please check log file for more details: $LOG_FILE"
2147 else
54ae008d 2148 _err "Please add '--debug' or '--log' to check more details."
a73c5b33 2149 _err "See: $_DEBUG_WIKI"
2150 fi
4c2a3841 2151
2152 if [ "$DEBUG" ] && [ "$DEBUG" -gt "0" ]; then
9d548d81 2153 _debug "$(_dlg_versions)"
2154 fi
4c2a3841 2155
b0070f03 2156 #run the post hook
4c2a3841 2157 if [ "$Le_PostHook" ]; then
b0070f03 2158 _info "Run post hook:'$Le_PostHook'"
2159 if ! (
2160 cd "$DOMAIN_PATH" && eval "$Le_PostHook"
4c2a3841 2161 ); then
b0070f03 2162 _err "Error when run post hook."
2163 return 1
2164 fi
2165 fi
2166}
2167
2168_on_issue_success() {
30c2d84c 2169 _debug _on_issue_success
b0070f03 2170 #run the post hook
4c2a3841 2171 if [ "$Le_PostHook" ]; then
b0070f03 2172 _info "Run post hook:'$Le_PostHook'"
2173 if ! (
2174 cd "$DOMAIN_PATH" && eval "$Le_PostHook"
4c2a3841 2175 ); then
b0070f03 2176 _err "Error when run post hook."
2177 return 1
2178 fi
2179 fi
4c2a3841 2180
b0070f03 2181 #run renew hook
4c2a3841 2182 if [ "$IS_RENEW" ] && [ "$Le_RenewHook" ]; then
b0070f03 2183 _info "Run renew hook:'$Le_RenewHook'"
2184 if ! (
2185 cd "$DOMAIN_PATH" && eval "$Le_RenewHook"
4c2a3841 2186 ); then
b0070f03 2187 _err "Error when run renew hook."
2188 return 1
2189 fi
4c2a3841 2190 fi
2191
b0070f03 2192}
2193
eb59817e 2194updateaccount() {
2195 _initpath
2196 _regAccount
2197}
b0070f03 2198
eb59817e 2199registeraccount() {
57e58ce7 2200 _reg_length="$1"
eb59817e 2201 _initpath
57e58ce7 2202 _regAccount "$_reg_length"
eb59817e 2203}
d404e92d 2204
8a29fbc8 2205__calcAccountKeyHash() {
ca7202eb 2206 [ -f "$ACCOUNT_KEY_PATH" ] && _digest sha256 <"$ACCOUNT_KEY_PATH"
8a29fbc8 2207}
2208
57e58ce7 2209#keylength
d404e92d 2210_regAccount() {
2211 _initpath
57e58ce7 2212 _reg_length="$1"
4c2a3841 2213
5c48e139 2214 if [ ! -f "$ACCOUNT_KEY_PATH" ] && [ -f "$_OLD_ACCOUNT_KEY" ]; then
2215 _info "mv $_OLD_ACCOUNT_KEY to $ACCOUNT_KEY_PATH"
2216 mv "$_OLD_ACCOUNT_KEY" "$ACCOUNT_KEY_PATH"
2217 fi
4c2a3841 2218
5c48e139 2219 if [ ! -f "$ACCOUNT_JSON_PATH" ] && [ -f "$_OLD_ACCOUNT_JSON" ]; then
2220 _info "mv $_OLD_ACCOUNT_JSON to $ACCOUNT_JSON_PATH"
2221 mv "$_OLD_ACCOUNT_JSON" "$ACCOUNT_JSON_PATH"
2222 fi
4c2a3841 2223
2224 if [ ! -f "$ACCOUNT_KEY_PATH" ]; then
2225 if ! _create_account_key "$_reg_length"; then
d404e92d 2226 _err "Create account key error."
2227 return 1
2228 fi
2229 fi
4c2a3841 2230
2231 if ! _calcjwk "$ACCOUNT_KEY_PATH"; then
d404e92d 2232 return 1
2233 fi
2234
2235 _updateTos=""
2236 _reg_res="new-reg"
4c2a3841 2237 while true; do
d404e92d 2238 _debug AGREEMENT "$AGREEMENT"
4c2a3841 2239
d404e92d 2240 regjson='{"resource": "'$_reg_res'", "agreement": "'$AGREEMENT'"}'
2241
4c2a3841 2242 if [ "$ACCOUNT_EMAIL" ]; then
d404e92d 2243 regjson='{"resource": "'$_reg_res'", "contact": ["mailto: '$ACCOUNT_EMAIL'"], "agreement": "'$AGREEMENT'"}'
2244 fi
2245
4c2a3841 2246 if [ -z "$_updateTos" ]; then
d404e92d 2247 _info "Registering account"
2248
4c2a3841 2249 if ! _send_signed_request "$API/acme/new-reg" "$regjson"; then
d404e92d 2250 _err "Register account Error: $response"
2251 return 1
2252 fi
2253
4c2a3841 2254 if [ "$code" = "" ] || [ "$code" = '201' ]; then
ca7202eb 2255 echo "$response" >"$ACCOUNT_JSON_PATH"
d404e92d 2256 _info "Registered"
4c2a3841 2257 elif [ "$code" = '409' ]; then
d404e92d 2258 _info "Already registered"
2259 else
2260 _err "Register account Error: $response"
2261 return 1
2262 fi
2263
4c2a3841 2264 _accUri="$(echo "$responseHeaders" | grep "^Location:" | _head_n 1 | cut -d ' ' -f 2 | tr -d "\r\n")"
d404e92d 2265 _debug "_accUri" "$_accUri"
d404e92d 2266
c2c8f320 2267 _tos="$(echo "$responseHeaders" | grep "^Link:.*rel=\"terms-of-service\"" | _head_n 1 | _egrep_o "<.*>" | tr -d '<>')"
d404e92d 2268 _debug "_tos" "$_tos"
4c2a3841 2269 if [ -z "$_tos" ]; then
d404e92d 2270 _debug "Use default tos: $DEFAULT_AGREEMENT"
2271 _tos="$DEFAULT_AGREEMENT"
2272 fi
2273 if [ "$_tos" != "$AGREEMENT" ]; then
2274 _updateTos=1
2275 AGREEMENT="$_tos"
2276 _reg_res="reg"
2277 continue
2278 fi
4c2a3841 2279
d404e92d 2280 else
2281 _debug "Update tos: $_tos"
4c2a3841 2282 if ! _send_signed_request "$_accUri" "$regjson"; then
d404e92d 2283 _err "Update tos error."
2284 return 1
2285 fi
4c2a3841 2286 if [ "$code" = '202' ]; then
eb59817e 2287 _info "Update success."
4c2a3841 2288
8a29fbc8 2289 CA_KEY_HASH="$(__calcAccountKeyHash)"
2290 _debug "Calc CA_KEY_HASH" "$CA_KEY_HASH"
2291 _savecaconf CA_KEY_HASH "$CA_KEY_HASH"
d404e92d 2292 else
800e3f45 2293 _err "Update account error."
d404e92d 2294 return 1
2295 fi
2296 fi
2297 return 0
2298 done
2299
2300}
2301
a61fe418 2302# domain folder file
2303_findHook() {
2304 _hookdomain="$1"
2305 _hookcat="$2"
2306 _hookname="$3"
2307
c7b16249 2308 if [ -f "$_SCRIPT_HOME/$_hookcat/$_hookname" ]; then
2309 d_api="$_SCRIPT_HOME/$_hookcat/$_hookname"
2310 elif [ -f "$_SCRIPT_HOME/$_hookcat/$_hookname.sh" ]; then
2311 d_api="$_SCRIPT_HOME/$_hookcat/$_hookname.sh"
4c2a3841 2312 elif [ -f "$LE_WORKING_DIR/$_hookdomain/$_hookname" ]; then
a61fe418 2313 d_api="$LE_WORKING_DIR/$_hookdomain/$_hookname"
4c2a3841 2314 elif [ -f "$LE_WORKING_DIR/$_hookdomain/$_hookname.sh" ]; then
a61fe418 2315 d_api="$LE_WORKING_DIR/$_hookdomain/$_hookname.sh"
4c2a3841 2316 elif [ -f "$LE_WORKING_DIR/$_hookname" ]; then
a61fe418 2317 d_api="$LE_WORKING_DIR/$_hookname"
4c2a3841 2318 elif [ -f "$LE_WORKING_DIR/$_hookname.sh" ]; then
a61fe418 2319 d_api="$LE_WORKING_DIR/$_hookname.sh"
4c2a3841 2320 elif [ -f "$LE_WORKING_DIR/$_hookcat/$_hookname" ]; then
a61fe418 2321 d_api="$LE_WORKING_DIR/$_hookcat/$_hookname"
4c2a3841 2322 elif [ -f "$LE_WORKING_DIR/$_hookcat/$_hookname.sh" ]; then
a61fe418 2323 d_api="$LE_WORKING_DIR/$_hookcat/$_hookname.sh"
2324 fi
2325
2326 printf "%s" "$d_api"
2327}
2328
f940b2a5 2329#domain
2330__get_domain_new_authz() {
2331 _gdnd="$1"
2332 _info "Getting new-authz for domain" "$_gdnd"
4c2a3841 2333
f940b2a5 2334 _Max_new_authz_retry_times=5
2335 _authz_i=0
4c2a3841 2336 while [ "$_authz_i" -lt "$_Max_new_authz_retry_times" ]; do
efd96153 2337 _debug "Try new-authz for the $_authz_i time."
4c2a3841 2338 if ! _send_signed_request "$API/acme/new-authz" "{\"resource\": \"new-authz\", \"identifier\": {\"type\": \"dns\", \"value\": \"$(_idn "$_gdnd")\"}}"; then
f940b2a5 2339 _err "Can not get domain new authz."
2340 return 1
2341 fi
4c2a3841 2342 if ! _contains "$response" "An error occurred while processing your request"; then
f940b2a5 2343 _info "The new-authz request is ok."
2344 break
2345 fi
2346 _authz_i="$(_math "$_authz_i" + 1)"
9e45ac93 2347 _info "The server is busy, Sleep $_authz_i to retry."
f940b2a5 2348 _sleep "$_authz_i"
4c2a3841 2349 done
f940b2a5 2350
4c2a3841 2351 if [ "$_authz_i" = "$_Max_new_authz_retry_times" ]; then
efd96153 2352 _err "new-authz retry reach the max $_Max_new_authz_retry_times times."
f940b2a5 2353 fi
4c2a3841 2354
2355 if [ ! -z "$code" ] && [ ! "$code" = '201' ]; then
f940b2a5 2356 _err "new-authz error: $response"
2357 return 1
2358 fi
2359
2360}
2361
10afcaca 2362#webroot, domain domainlist keylength
4c3b3608 2363issue() {
4c2a3841 2364 if [ -z "$2" ]; then
43822d37 2365 _usage "Usage: $PROJECT_ENTRY --issue -d a.com -w /path/to/webroot/a.com/ "
4c3b3608 2366 return 1
2367 fi
2368 Le_Webroot="$1"
2369 Le_Domain="$2"
2370 Le_Alt="$3"
2371 Le_Keylength="$4"
2372 Le_RealCertPath="$5"
2373 Le_RealKeyPath="$6"
2374 Le_RealCACertPath="$7"
2375 Le_ReloadCmd="$8"
a63b05a9 2376 Le_RealFullChainPath="$9"
b0070f03 2377 Le_PreHook="${10}"
2378 Le_PostHook="${11}"
2379 Le_RenewHook="${12}"
0463b5d6 2380 Le_LocalAddress="${13}"
4c2a3841 2381
eccec5f6 2382 #remove these later.
4c2a3841 2383 if [ "$Le_Webroot" = "dns-cf" ]; then
eccec5f6 2384 Le_Webroot="dns_cf"
2385 fi
4c2a3841 2386 if [ "$Le_Webroot" = "dns-dp" ]; then
eccec5f6 2387 Le_Webroot="dns_dp"
2388 fi
4c2a3841 2389 if [ "$Le_Webroot" = "dns-cx" ]; then
eccec5f6 2390 Le_Webroot="dns_cx"
2391 fi
950172dc 2392 _debug "Using api: $API"
4c2a3841 2393
2394 if [ ! "$IS_RENEW" ]; then
ca7202eb 2395 _initpath "$Le_Domain" "$Le_Keylength"
43822d37 2396 mkdir -p "$DOMAIN_PATH"
2397 fi
eccec5f6 2398
4c2a3841 2399 if [ -f "$DOMAIN_CONF" ]; then
61623d22 2400 Le_NextRenewTime=$(_readdomainconf Le_NextRenewTime)
a4270efa 2401 _debug Le_NextRenewTime "$Le_NextRenewTime"
95e06de5 2402 if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(_time)" -lt "$Le_NextRenewTime" ]; then
bb25febd 2403 _saved_domain=$(_readdomainconf Le_Domain)
2404 _debug _saved_domain "$_saved_domain"
2405 _saved_alt=$(_readdomainconf Le_Alt)
2406 _debug _saved_alt "$_saved_alt"
4c2a3841 2407 if [ "$_saved_domain,$_saved_alt" = "$Le_Domain,$Le_Alt" ]; then
bb25febd 2408 _info "Domains not changed."
2409 _info "Skip, Next renewal time is: $(__green "$(_readdomainconf Le_NextRenewTimeStr)")"
4c2a3841 2410 _info "Add '$(__red '--force')' to force to renew."
bb25febd 2411 return $RENEW_SKIP
2412 else
2413 _info "Domains have changed."
2414 fi
4c3b3608 2415 fi
2416 fi
96a46cfc 2417
4c2a3841 2418 _savedomainconf "Le_Domain" "$Le_Domain"
2419 _savedomainconf "Le_Alt" "$Le_Alt"
2420 _savedomainconf "Le_Webroot" "$Le_Webroot"
2421
2422 _savedomainconf "Le_PreHook" "$Le_PreHook"
2423 _savedomainconf "Le_PostHook" "$Le_PostHook"
2424 _savedomainconf "Le_RenewHook" "$Le_RenewHook"
2425
2426 if [ "$Le_LocalAddress" ]; then
2427 _savedomainconf "Le_LocalAddress" "$Le_LocalAddress"
72518d48 2428 else
2429 _cleardomainconf "Le_LocalAddress"
2430 fi
6ae0f7f5 2431
f6dcd989 2432 Le_API="$API"
2433 _savedomainconf "Le_API" "$Le_API"
4c2a3841 2434
2435 if [ "$Le_Alt" = "$NO_VALUE" ]; then
4c3b3608 2436 Le_Alt=""
2437 fi
4c2a3841 2438
2439 if [ "$Le_Keylength" = "$NO_VALUE" ]; then
d404e92d 2440 Le_Keylength=""
2441 fi
4c2a3841 2442
2443 if ! _on_before_issue; then
0463b5d6 2444 _err "_on_before_issue."
2445 return 1
4c3b3608 2446 fi
0463b5d6 2447
8a29fbc8 2448 _saved_account_key_hash="$(_readcaconf "CA_KEY_HASH")"
2449 _debug2 _saved_account_key_hash "$_saved_account_key_hash"
4c2a3841 2450
2451 if [ -z "$_saved_account_key_hash" ] || [ "$_saved_account_key_hash" != "$(__calcAccountKeyHash)" ]; then
57e58ce7 2452 if ! _regAccount "$_accountkeylength"; then
8a29fbc8 2453 _on_issue_err
2454 return 1
2455 fi
57e58ce7 2456 else
2457 _debug "_saved_account_key_hash is not changed, skip register account."
166096dc 2458 fi
166096dc 2459
4c2a3841 2460 if [ -f "$CSR_PATH" ] && [ ! -f "$CERT_KEY_PATH" ]; then
10afcaca 2461 _info "Signing from existing CSR."
2462 else
2463 _key=$(_readdomainconf Le_Keylength)
2464 _debug "Read key length:$_key"
4c2a3841 2465 if [ ! -f "$CERT_KEY_PATH" ] || [ "$Le_Keylength" != "$_key" ]; then
ca7202eb 2466 if ! createDomainKey "$Le_Domain" "$Le_Keylength"; then
10afcaca 2467 _err "Create domain key error."
2468 _clearup
b0070f03 2469 _on_issue_err
10afcaca 2470 return 1
2471 fi
2472 fi
2473
4c2a3841 2474 if ! _createcsr "$Le_Domain" "$Le_Alt" "$CERT_KEY_PATH" "$CSR_PATH" "$DOMAIN_SSL_CONF"; then
10afcaca 2475 _err "Create CSR error."
5ef501c5 2476 _clearup
b0070f03 2477 _on_issue_err
41e3eafa 2478 return 1
2479 fi
4c3b3608 2480 fi
10afcaca 2481
4c2a3841 2482 _savedomainconf "Le_Keylength" "$Le_Keylength"
2483
4c3b3608 2484 vlist="$Le_Vlist"
cae203be 2485
2486 _info "Getting domain auth token for each domain"
4c3b3608 2487 sep='#'
4c2a3841 2488 if [ -z "$vlist" ]; then
2489 alldomains=$(echo "$Le_Domain,$Le_Alt" | tr ',' ' ')
a63b05a9 2490 _index=1
2491 _currentRoot=""
4c2a3841 2492 for d in $alldomains; do
ca7202eb 2493 _info "Getting webroot for domain" "$d"
a63b05a9 2494 _w="$(echo $Le_Webroot | cut -d , -f $_index)"
0463b5d6 2495 _info _w "$_w"
4c2a3841 2496 if [ "$_w" ]; then
a63b05a9 2497 _currentRoot="$_w"
2498 fi
2499 _debug "_currentRoot" "$_currentRoot"
00a50605 2500 _index=$(_math $_index + 1)
4c2a3841 2501
a63b05a9 2502 vtype="$VTYPE_HTTP"
4c2a3841 2503 if _startswith "$_currentRoot" "dns"; then
a63b05a9 2504 vtype="$VTYPE_DNS"
2505 fi
4c2a3841 2506
2507 if [ "$_currentRoot" = "$W_TLS" ]; then
e22bcf7c 2508 vtype="$VTYPE_TLS"
2509 fi
c4d8fd83 2510
4c2a3841 2511 if ! __get_domain_new_authz "$d"; then
c4d8fd83 2512 _clearup
b0070f03 2513 _on_issue_err
c4d8fd83 2514 return 1
2515 fi
2516
4c2a3841 2517 if [ -z "$thumbprint" ]; then
2518 accountkey_json=$(printf "%s" "$jwk" | tr -d ' ')
ae2db62f 2519 thumbprint=$(printf "%s" "$accountkey_json" | _digest "sha256" | _urlencode)
4c3b3608 2520 fi
2521
4c2a3841 2522 entry="$(printf "%s\n" "$response" | _egrep_o '[^\{]*"type":"'$vtype'"[^\}]*')"
4c3b3608 2523 _debug entry "$entry"
4c2a3841 2524 if [ -z "$entry" ]; then
19539575 2525 _err "Error, can not get domain token $d"
2526 _clearup
b0070f03 2527 _on_issue_err
19539575 2528 return 1
2529 fi
22ea4004 2530 token="$(printf "%s\n" "$entry" | _egrep_o '"token":"[^"]*' | cut -d : -f 2 | tr -d '"')"
ca7202eb 2531 _debug token "$token"
4c2a3841 2532
2533 uri="$(printf "%s\n" "$entry" | _egrep_o '"uri":"[^"]*' | cut -d : -f 2,3 | tr -d '"')"
ca7202eb 2534 _debug uri "$uri"
cae203be 2535
4c3b3608 2536 keyauthorization="$token.$thumbprint"
2537 _debug keyauthorization "$keyauthorization"
2538
95e06de5 2539 if printf "%s" "$response" | grep '"status":"valid"' >/dev/null 2>&1; then
d35bf517 2540 _info "$d is already verified, skip."
ca7202eb 2541 keyauthorization="$STATE_VERIFIED"
d35bf517 2542 _debug keyauthorization "$keyauthorization"
ec603bee 2543 fi
2544
a63b05a9 2545 dvlist="$d$sep$keyauthorization$sep$uri$sep$vtype$sep$_currentRoot"
4c3b3608 2546 _debug dvlist "$dvlist"
4c2a3841 2547
4c3b3608 2548 vlist="$vlist$dvlist,"
2549
2550 done
2551
2552 #add entry
2553 dnsadded=""
4c2a3841 2554 ventries=$(echo "$vlist" | tr ',' ' ')
2555 for ventry in $ventries; do
ca7202eb 2556 d=$(echo "$ventry" | cut -d "$sep" -f 1)
2557 keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2)
2558 vtype=$(echo "$ventry" | cut -d "$sep" -f 4)
2559 _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5)
ec603bee 2560
4c2a3841 2561 if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
ec603bee 2562 _info "$d is already verified, skip $vtype."
2563 continue
2564 fi
2565
4c2a3841 2566 if [ "$vtype" = "$VTYPE_DNS" ]; then
4c3b3608 2567 dnsadded='0'
2568 txtdomain="_acme-challenge.$d"
2569 _debug txtdomain "$txtdomain"
22ea4004 2570 txt="$(printf "%s" "$keyauthorization" | _digest "sha256" | _urlencode)"
4c3b3608 2571 _debug txt "$txt"
a61fe418 2572
0c538f75 2573 d_api="$(_findHook "$d" dnsapi "$_currentRoot")"
a61fe418 2574
4c3b3608 2575 _debug d_api "$d_api"
4c2a3841 2576
2577 if [ "$d_api" ]; then
4c3b3608 2578 _info "Found domain api file: $d_api"
2579 else
2580 _err "Add the following TXT record:"
0c538f75 2581 _err "Domain: '$(__green "$txtdomain")'"
2582 _err "TXT value: '$(__green "$txt")'"
4c3b3608 2583 _err "Please be aware that you prepend _acme-challenge. before your domain"
2584 _err "so the resulting subdomain will be: $txtdomain"
2585 continue
2586 fi
4c2a3841 2587
73b8b120 2588 (
ca7202eb 2589 if ! . "$d_api"; then
73b8b120 2590 _err "Load file $d_api error. Please check your api file and try again."
2591 return 1
2592 fi
4c2a3841 2593
158f22f7 2594 addcommand="${_currentRoot}_add"
ca7202eb 2595 if ! _exists "$addcommand"; then
73b8b120 2596 _err "It seems that your api file is not correct, it must have a function named: $addcommand"
2597 return 1
2598 fi
4c2a3841 2599
ca7202eb 2600 if ! $addcommand "$txtdomain" "$txt"; then
73b8b120 2601 _err "Error add txt for domain:$txtdomain"
2602 return 1
2603 fi
2604 )
4c2a3841 2605
2606 if [ "$?" != "0" ]; then
5ef501c5 2607 _clearup
b0070f03 2608 _on_issue_err
4c3b3608 2609 return 1
2610 fi
2611 dnsadded='1'
2612 fi
2613 done
2614
4c2a3841 2615 if [ "$dnsadded" = '0' ]; then
2616 _savedomainconf "Le_Vlist" "$vlist"
4c3b3608 2617 _debug "Dns record not added yet, so, save to $DOMAIN_CONF and exit."
2618 _err "Please add the TXT records to the domains, and retry again."
5ef501c5 2619 _clearup
b0070f03 2620 _on_issue_err
4c3b3608 2621 return 1
2622 fi
4c2a3841 2623
4c3b3608 2624 fi
4c2a3841 2625
2626 if [ "$dnsadded" = '1' ]; then
2627 if [ -z "$Le_DNSSleep" ]; then
ca7202eb 2628 Le_DNSSleep="$DEFAULT_DNS_SLEEP"
0e38c60d 2629 else
4c2a3841 2630 _savedomainconf "Le_DNSSleep" "$Le_DNSSleep"
0e38c60d 2631 fi
2632
5fbc47eb 2633 _info "Sleep $(__green $Le_DNSSleep) seconds for the txt records to take effect"
ca7202eb 2634 _sleep "$Le_DNSSleep"
4c3b3608 2635 fi
4c2a3841 2636
4c3b3608 2637 _debug "ok, let's start to verify"
a63b05a9 2638
0463b5d6 2639 _ncIndex=1
4c2a3841 2640 ventries=$(echo "$vlist" | tr ',' ' ')
2641 for ventry in $ventries; do
ca7202eb 2642 d=$(echo "$ventry" | cut -d "$sep" -f 1)
2643 keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2)
2644 uri=$(echo "$ventry" | cut -d "$sep" -f 3)
2645 vtype=$(echo "$ventry" | cut -d "$sep" -f 4)
2646 _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5)
ec603bee 2647
4c2a3841 2648 if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
ec603bee 2649 _info "$d is already verified, skip $vtype."
2650 continue
2651 fi
2652
4c3b3608 2653 _info "Verifying:$d"
2654 _debug "d" "$d"
2655 _debug "keyauthorization" "$keyauthorization"
2656 _debug "uri" "$uri"
2657 removelevel=""
e22bcf7c 2658 token="$(printf "%s" "$keyauthorization" | cut -d '.' -f 1)"
a63b05a9 2659
2660 _debug "_currentRoot" "$_currentRoot"
2661
4c2a3841 2662 if [ "$vtype" = "$VTYPE_HTTP" ]; then
2663 if [ "$_currentRoot" = "$NO_VALUE" ]; then
4c3b3608 2664 _info "Standalone mode server"
4c2a3841 2665 _ncaddr="$(_getfield "$Le_LocalAddress" "$_ncIndex")"
0463b5d6 2666 _ncIndex="$(_math $_ncIndex + 1)"
2667 _startserver "$keyauthorization" "$_ncaddr" &
4c2a3841 2668 if [ "$?" != "0" ]; then
5ef501c5 2669 _clearup
b0070f03 2670 _on_issue_err
6fc1447f 2671 return 1
2672 fi
4c3b3608 2673 serverproc="$!"
5dbf664a 2674 sleep 1
ca7202eb 2675 _debug serverproc "$serverproc"
6fc1447f 2676
4c3b3608 2677 else
4c2a3841 2678 if [ "$_currentRoot" = "apache" ]; then
6f930641 2679 wellknown_path="$ACME_DIR"
2680 else
a63b05a9 2681 wellknown_path="$_currentRoot/.well-known/acme-challenge"
4c2a3841 2682 if [ ! -d "$_currentRoot/.well-known" ]; then
6f930641 2683 removelevel='1'
4c2a3841 2684 elif [ ! -d "$_currentRoot/.well-known/acme-challenge" ]; then
6f930641 2685 removelevel='2'
2686 else
2687 removelevel='3'
2688 fi
4c3b3608 2689 fi
6f930641 2690
4c3b3608 2691 _debug wellknown_path "$wellknown_path"
6f930641 2692
4c3b3608 2693 _debug "writing token:$token to $wellknown_path/$token"
2694
2695 mkdir -p "$wellknown_path"
93fc48a2 2696
4c2a3841 2697 if ! printf "%s" "$keyauthorization" >"$wellknown_path/$token"; then
93fc48a2 2698 _err "$d:Can not write token to file : $wellknown_path/$token"
2699 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
2700 _clearup
2701 _on_issue_err
2702 return 1
2703 fi
2704
4c2a3841 2705 if [ ! "$usingApache" ]; then
44edb2bd 2706 if webroot_owner=$(_stat "$_currentRoot"); then
32fdc196 2707 _debug "Changing owner/group of .well-known to $webroot_owner"
ca7202eb 2708 chown -R "$webroot_owner" "$_currentRoot/.well-known"
32fdc196 2709 else
4c2a3841 2710 _debug "not chaning owner/group of webroot"
32fdc196 2711 fi
df886ffa 2712 fi
4c2a3841 2713
4c3b3608 2714 fi
4c2a3841 2715
2716 elif [ "$vtype" = "$VTYPE_TLS" ]; then
e22bcf7c 2717 #create A
2718 #_hash_A="$(printf "%s" $token | _digest "sha256" "hex" )"
2719 #_debug2 _hash_A "$_hash_A"
2720 #_x="$(echo $_hash_A | cut -c 1-32)"
2721 #_debug2 _x "$_x"
2722 #_y="$(echo $_hash_A | cut -c 33-64)"
2723 #_debug2 _y "$_y"
2724 #_SAN_A="$_x.$_y.token.acme.invalid"
2725 #_debug2 _SAN_A "$_SAN_A"
4c2a3841 2726
e22bcf7c 2727 #create B
0c538f75 2728 _hash_B="$(printf "%s" "$keyauthorization" | _digest "sha256" "hex")"
e22bcf7c 2729 _debug2 _hash_B "$_hash_B"
0c538f75 2730 _x="$(echo "$_hash_B" | cut -c 1-32)"
e22bcf7c 2731 _debug2 _x "$_x"
0c538f75 2732 _y="$(echo "$_hash_B" | cut -c 33-64)"
e22bcf7c 2733 _debug2 _y "$_y"
4c2a3841 2734
e22bcf7c 2735 #_SAN_B="$_x.$_y.ka.acme.invalid"
4c2a3841 2736
e22bcf7c 2737 _SAN_B="$_x.$_y.acme.invalid"
2738 _debug2 _SAN_B "$_SAN_B"
4c2a3841 2739
2740 _ncaddr="$(_getfield "$Le_LocalAddress" "$_ncIndex")"
0c538f75 2741 _ncIndex="$(_math "$_ncIndex" + 1)"
0463b5d6 2742 if ! _starttlsserver "$_SAN_B" "$_SAN_A" "$Le_TLSPort" "$keyauthorization" "$_ncaddr"; then
e22bcf7c 2743 _err "Start tls server error."
2744 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
2745 _clearup
b0070f03 2746 _on_issue_err
e22bcf7c 2747 return 1
2748 fi
4c3b3608 2749 fi
4c2a3841 2750
ca7202eb 2751 if ! _send_signed_request "$uri" "{\"resource\": \"challenge\", \"keyAuthorization\": \"$keyauthorization\"}"; then
c4d8fd83 2752 _err "$d:Can not get challenge: $response"
2753 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
2754 _clearup
b0070f03 2755 _on_issue_err
c4d8fd83 2756 return 1
2757 fi
4c2a3841 2758
2759 if [ ! -z "$code" ] && [ ! "$code" = '202' ]; then
c60883ef 2760 _err "$d:Challenge error: $response"
a63b05a9 2761 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4c3b3608 2762 _clearup
b0070f03 2763 _on_issue_err
4c3b3608 2764 return 1
2765 fi
4c2a3841 2766
6fc1447f 2767 waittimes=0
4c2a3841 2768 if [ -z "$MAX_RETRY_TIMES" ]; then
6fc1447f 2769 MAX_RETRY_TIMES=30
2770 fi
4c2a3841 2771
2772 while true; do
0c538f75 2773 waittimes=$(_math "$waittimes" + 1)
4c2a3841 2774 if [ "$waittimes" -ge "$MAX_RETRY_TIMES" ]; then
6fc1447f 2775 _err "$d:Timeout"
2776 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
2777 _clearup
b0070f03 2778 _on_issue_err
6fc1447f 2779 return 1
2780 fi
4c2a3841 2781
5dbf664a 2782 _debug "sleep 2 secs to verify"
2783 sleep 2
4c3b3608 2784 _debug "checking"
44edb2bd 2785 response="$(_get "$uri")"
4c2a3841 2786 if [ "$?" != "0" ]; then
c60883ef 2787 _err "$d:Verify error:$response"
a63b05a9 2788 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4c3b3608 2789 _clearup
b0070f03 2790 _on_issue_err
4c3b3608 2791 return 1
2792 fi
9aaf36cd 2793 _debug2 original "$response"
4c2a3841 2794
2795 response="$(echo "$response" | _normalizeJson)"
7012b91f 2796 _debug2 response "$response"
4c2a3841 2797
2798 status=$(echo "$response" | _egrep_o '"status":"[^"]*' | cut -d : -f 2 | tr -d '"')
2799 if [ "$status" = "valid" ]; then
4c3b3608 2800 _info "Success"
ca7202eb 2801 _stopserver "$serverproc"
4c3b3608 2802 serverproc=""
a63b05a9 2803 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4c2a3841 2804 break
4c3b3608 2805 fi
4c2a3841 2806
2807 if [ "$status" = "invalid" ]; then
2808 error="$(echo "$response" | tr -d "\r\n" | _egrep_o '"error":\{[^\}]*')"
2809 _debug2 error "$error"
2810 errordetail="$(echo "$error" | _egrep_o '"detail": *"[^"]*' | cut -d '"' -f 4)"
2811 _debug2 errordetail "$errordetail"
2812 if [ "$errordetail" ]; then
2813 _err "$d:Verify error:$errordetail"
2814 else
2815 _err "$d:Verify error:$error"
2816 fi
2817 if [ "$DEBUG" ]; then
2818 if [ "$vtype" = "$VTYPE_HTTP" ]; then
2819 _debug "Debug: get token url."
2820 _get "http://$d/.well-known/acme-challenge/$token" "" 1
2821 fi
2822 fi
a63b05a9 2823 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4c3b3608 2824 _clearup
b0070f03 2825 _on_issue_err
4c2a3841 2826 return 1
4c3b3608 2827 fi
4c2a3841 2828
2829 if [ "$status" = "pending" ]; then
4c3b3608 2830 _info "Pending"
2831 else
4c2a3841 2832 _err "$d:Verify error:$response"
a63b05a9 2833 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4c3b3608 2834 _clearup
b0070f03 2835 _on_issue_err
4c3b3608 2836 return 1
2837 fi
4c2a3841 2838
4c3b3608 2839 done
4c2a3841 2840
4c3b3608 2841 done
2842
2843 _clearup
2844 _info "Verify finished, start to sign."
fa8311dc 2845 der="$(_getfile "${CSR_PATH}" "${BEGIN_CSR}" "${END_CSR}" | tr -d "\r\n" | _urlencode)"
4c2a3841 2846
2847 if ! _send_signed_request "$API/acme/new-cert" "{\"resource\": \"new-cert\", \"csr\": \"$der\"}" "needbase64"; then
c4d8fd83 2848 _err "Sign failed."
b0070f03 2849 _on_issue_err
c4d8fd83 2850 return 1
2851 fi
4c2a3841 2852
d404e92d 2853 _rcert="$response"
0c538f75 2854 Le_LinkCert="$(grep -i '^Location.*$' "$HTTP_HEADER" | _head_n 1 | tr -d "\r\n" | cut -d " " -f 2)"
4c2a3841 2855 _savedomainconf "Le_LinkCert" "$Le_LinkCert"
4c3b3608 2856
4c2a3841 2857 if [ "$Le_LinkCert" ]; then
2858 echo "$BEGIN_CERT" >"$CERT_PATH"
4c3b3608 2859
72518d48 2860 #if ! _get "$Le_LinkCert" | _base64 "multiline" >> "$CERT_PATH" ; then
2861 # _debug "Get cert failed. Let's try last response."
2862 # printf -- "%s" "$_rcert" | _dbase64 "multiline" | _base64 "multiline" >> "$CERT_PATH"
2863 #fi
4c2a3841 2864
2865 if ! printf -- "%s" "$_rcert" | _dbase64 "multiline" | _base64 "multiline" >>"$CERT_PATH"; then
72518d48 2866 _debug "Try cert link."
4c2a3841 2867 _get "$Le_LinkCert" | _base64 "multiline" >>"$CERT_PATH"
d404e92d 2868 fi
2869
4c2a3841 2870 echo "$END_CERT" >>"$CERT_PATH"
43822d37 2871 _info "$(__green "Cert success.")"
4c3b3608 2872 cat "$CERT_PATH"
5980ebc7 2873
4c2a3841 2874 _info "Your cert is in $(__green " $CERT_PATH ")"
2875
2876 if [ -f "$CERT_KEY_PATH" ]; then
2877 _info "Your cert key is in $(__green " $CERT_KEY_PATH ")"
5980ebc7 2878 fi
2879
caf1fc10 2880 cp "$CERT_PATH" "$CERT_FULLCHAIN_PATH"
281aa349 2881
4c2a3841 2882 if [ ! "$USER_PATH" ] || [ ! "$IN_CRON" ]; then
281aa349 2883 USER_PATH="$PATH"
2884 _saveaccountconf "USER_PATH" "$USER_PATH"
2885 fi
4c3b3608 2886 fi
4c3b3608 2887
4c2a3841 2888 if [ -z "$Le_LinkCert" ]; then
0c538f75 2889 response="$(echo "$response" | _dbase64 "multiline" | _normalizeJson)"
4c2a3841 2890 _err "Sign failed: $(echo "$response" | _egrep_o '"detail":"[^"]*"')"
b0070f03 2891 _on_issue_err
4c3b3608 2892 return 1
2893 fi
4c2a3841 2894
2895 _cleardomainconf "Le_Vlist"
2896
0c538f75 2897 Le_LinkIssuer=$(grep -i '^Link' "$HTTP_HEADER" | _head_n 1 | cut -d " " -f 2 | cut -d ';' -f 1 | tr -d '<>')
4c2a3841 2898 if ! _contains "$Le_LinkIssuer" ":"; then
fac1e367 2899 Le_LinkIssuer="$API$Le_LinkIssuer"
2900 fi
4c2a3841 2901
2902 _savedomainconf "Le_LinkIssuer" "$Le_LinkIssuer"
2903
2904 if [ "$Le_LinkIssuer" ]; then
2905 echo "$BEGIN_CERT" >"$CA_CERT_PATH"
2906 _get "$Le_LinkIssuer" | _base64 "multiline" >>"$CA_CERT_PATH"
2907 echo "$END_CERT" >>"$CA_CERT_PATH"
2908 _info "The intermediate CA cert is in $(__green " $CA_CERT_PATH ")"
2909 cat "$CA_CERT_PATH" >>"$CERT_FULLCHAIN_PATH"
2910 _info "And the full chain certs is there: $(__green " $CERT_FULLCHAIN_PATH ")"
4c3b3608 2911 fi
4c2a3841 2912
3aae1ae3 2913 Le_CertCreateTime=$(_time)
4c2a3841 2914 _savedomainconf "Le_CertCreateTime" "$Le_CertCreateTime"
2915
2916 Le_CertCreateTimeStr=$(date -u)
2917 _savedomainconf "Le_CertCreateTimeStr" "$Le_CertCreateTimeStr"
2918
2919 if [ -z "$Le_RenewalDays" ] || [ "$Le_RenewalDays" -lt "0" ] || [ "$Le_RenewalDays" -gt "$MAX_RENEW" ]; then
ca7202eb 2920 Le_RenewalDays="$MAX_RENEW"
054cb72e 2921 else
4c2a3841 2922 _savedomainconf "Le_RenewalDays" "$Le_RenewalDays"
13d7cae9 2923 fi
4c2a3841 2924
2925 if [ "$CA_BUNDLE" ]; then
78009539
PS
2926 _saveaccountconf CA_BUNDLE "$CA_BUNDLE"
2927 else
2928 _clearaccountconf "CA_BUNDLE"
2929 fi
2930
4c2a3841 2931 if [ "$HTTPS_INSECURE" ]; then
fac1e367 2932 _saveaccountconf HTTPS_INSECURE "$HTTPS_INSECURE"
2933 else
4c2a3841 2934 _clearaccountconf "HTTPS_INSECURE"
13d7cae9 2935 fi
00a50605 2936
4c2a3841 2937 if [ "$Le_Listen_V4" ]; then
2938 _savedomainconf "Le_Listen_V4" "$Le_Listen_V4"
50827188 2939 _cleardomainconf Le_Listen_V6
4c2a3841 2940 elif [ "$Le_Listen_V6" ]; then
2941 _savedomainconf "Le_Listen_V6" "$Le_Listen_V6"
50827188 2942 _cleardomainconf Le_Listen_V4
2943 fi
f6dcd989 2944
ca7202eb 2945 Le_NextRenewTime=$(_math "$Le_CertCreateTime" + "$Le_RenewalDays" \* 24 \* 60 \* 60)
4c2a3841 2946
ca7202eb 2947 Le_NextRenewTimeStr=$(_time2str "$Le_NextRenewTime")
4c2a3841 2948 _savedomainconf "Le_NextRenewTimeStr" "$Le_NextRenewTimeStr"
2949
ca7202eb 2950 Le_NextRenewTime=$(_math "$Le_NextRenewTime" - 86400)
4c2a3841 2951 _savedomainconf "Le_NextRenewTime" "$Le_NextRenewTime"
f6dcd989 2952
b0070f03 2953 _on_issue_success
4c3b3608 2954
4c2a3841 2955 if [ "$Le_RealCertPath$Le_RealKeyPath$Le_RealCACertPath$Le_ReloadCmd$Le_RealFullChainPath" ]; then
43822d37 2956 _installcert
01f54558 2957 fi
4c0d3f1b 2958
4c3b3608 2959}
2960
43822d37 2961#domain [isEcc]
4c3b3608 2962renew() {
2963 Le_Domain="$1"
4c2a3841 2964 if [ -z "$Le_Domain" ]; then
43822d37 2965 _usage "Usage: $PROJECT_ENTRY --renew -d domain.com [--ecc]"
4c3b3608 2966 return 1
2967 fi
2968
43822d37 2969 _isEcc="$2"
2970
e799ef29 2971 _initpath "$Le_Domain" "$_isEcc"
43822d37 2972
e2053b22 2973 _info "$(__green "Renew: '$Le_Domain'")"
4c2a3841 2974 if [ ! -f "$DOMAIN_CONF" ]; then
43822d37 2975 _info "'$Le_Domain' is not a issued domain, skip."
4c2a3841 2976 return 0
4c3b3608 2977 fi
4c2a3841 2978
2979 if [ "$Le_RenewalDays" ]; then
1e6b68f5 2980 _savedomainconf Le_RenewalDays "$Le_RenewalDays"
2981 fi
2982
8663fb7e 2983 . "$DOMAIN_CONF"
4c2a3841 2984
2985 if [ "$Le_API" ]; then
5c48e139 2986 API="$Le_API"
2987 fi
4c2a3841 2988
2989 if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(_time)" -lt "$Le_NextRenewTime" ]; then
e2053b22 2990 _info "Skip, Next renewal time is: $(__green "$Le_NextRenewTimeStr")"
2991 _info "Add '$(__red '--force')' to force to renew."
e799ef29 2992 return "$RENEW_SKIP"
4c3b3608 2993 fi
4c2a3841 2994
4c3b3608 2995 IS_RENEW="1"
0463b5d6 2996 issue "$Le_Webroot" "$Le_Domain" "$Le_Alt" "$Le_Keylength" "$Le_RealCertPath" "$Le_RealKeyPath" "$Le_RealCACertPath" "$Le_ReloadCmd" "$Le_RealFullChainPath" "$Le_PreHook" "$Le_PostHook" "$Le_RenewHook" "$Le_LocalAddress"
e799ef29 2997 res="$?"
4c2a3841 2998 if [ "$res" != "0" ]; then
e799ef29 2999 return "$res"
a61fe418 3000 fi
4c2a3841 3001
3002 if [ "$Le_DeployHook" ]; then
e799ef29 3003 deploy "$Le_Domain" "$Le_DeployHook" "$Le_Keylength"
3004 res="$?"
a61fe418 3005 fi
4c2a3841 3006
4c3b3608 3007 IS_RENEW=""
3008
e799ef29 3009 return "$res"
4c3b3608 3010}
3011
cc179731 3012#renewAll [stopRenewOnError]
4c3b3608 3013renewAll() {
3014 _initpath
cc179731 3015 _stopRenewOnError="$1"
3016 _debug "_stopRenewOnError" "$_stopRenewOnError"
3017 _ret="0"
43822d37 3018
e591d5cf 3019 for di in "${CERT_HOME}"/*.*/; do
3020 _debug di "$di"
44483dba 3021 if ! [ -d "$di" ]; then
3498a585 3022 _debug "Not directory, skip: $di"
3023 continue
3024 fi
e591d5cf 3025 d=$(basename "$di")
201aa244 3026 _debug d "$d"
43822d37 3027 (
201aa244 3028 if _endswith "$d" "$ECC_SUFFIX"; then
3029 _isEcc=$(echo "$d" | cut -d "$ECC_SEP" -f 2)
3030 d=$(echo "$d" | cut -d "$ECC_SEP" -f 1)
43822d37 3031 fi
3032 renew "$d" "$_isEcc"
4d2f38b0 3033 )
cc179731 3034 rc="$?"
3035 _debug "Return code: $rc"
4c2a3841 3036 if [ "$rc" != "0" ]; then
3037 if [ "$rc" = "$RENEW_SKIP" ]; then
cc179731 3038 _info "Skipped $d"
4c2a3841 3039 elif [ "$_stopRenewOnError" ]; then
cc179731 3040 _err "Error renew $d, stop now."
201aa244 3041 return "$rc"
cc179731 3042 else
3043 _ret="$rc"
3044 _err "Error renew $d, Go ahead to next one."
3045 fi
3046 fi
4c3b3608 3047 done
201aa244 3048 return "$_ret"
4c3b3608 3049}
3050
10afcaca 3051#csr webroot
4c2a3841 3052signcsr() {
10afcaca 3053 _csrfile="$1"
3054 _csrW="$2"
3055 if [ -z "$_csrfile" ] || [ -z "$_csrW" ]; then
3056 _usage "Usage: $PROJECT_ENTRY --signcsr --csr mycsr.csr -w /path/to/webroot/a.com/ "
3057 return 1
3058 fi
3059
3060 _initpath
3061
3062 _csrsubj=$(_readSubjectFromCSR "$_csrfile")
4c2a3841 3063 if [ "$?" != "0" ]; then
10afcaca 3064 _err "Can not read subject from csr: $_csrfile"
3065 return 1
3066 fi
ad752b31 3067 _debug _csrsubj "$_csrsubj"
10afcaca 3068
3069 _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile")
4c2a3841 3070 if [ "$?" != "0" ]; then
10afcaca 3071 _err "Can not read domain list from csr: $_csrfile"
3072 return 1
3073 fi
3074 _debug "_csrdomainlist" "$_csrdomainlist"
4c2a3841 3075
3076 if [ -z "$_csrsubj" ]; then
ad752b31 3077 _csrsubj="$(_getfield "$_csrdomainlist" 1)"
3078 _debug _csrsubj "$_csrsubj"
3079 _csrdomainlist="$(echo "$_csrdomainlist" | cut -d , -f 2-)"
3080 _debug "_csrdomainlist" "$_csrdomainlist"
3081 fi
4c2a3841 3082
3083 if [ -z "$_csrsubj" ]; then
ad752b31 3084 _err "Can not read subject from csr: $_csrfile"
3085 return 1
3086 fi
4c2a3841 3087
10afcaca 3088 _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile")
4c2a3841 3089 if [ "$?" != "0" ] || [ -z "$_csrkeylength" ]; then
10afcaca 3090 _err "Can not read key length from csr: $_csrfile"
3091 return 1
3092 fi
4c2a3841 3093
10afcaca 3094 _initpath "$_csrsubj" "$_csrkeylength"
3095 mkdir -p "$DOMAIN_PATH"
4c2a3841 3096
10afcaca 3097 _info "Copy csr to: $CSR_PATH"
3098 cp "$_csrfile" "$CSR_PATH"
4c2a3841 3099
10afcaca 3100 issue "$_csrW" "$_csrsubj" "$_csrdomainlist" "$_csrkeylength"
4c2a3841 3101
10afcaca 3102}
3103
3104showcsr() {
4c2a3841 3105 _csrfile="$1"
10afcaca 3106 _csrd="$2"
3107 if [ -z "$_csrfile" ] && [ -z "$_csrd" ]; then
3108 _usage "Usage: $PROJECT_ENTRY --showcsr --csr mycsr.csr"
3109 return 1
3110 fi
3111
3112 _initpath
4c2a3841 3113
10afcaca 3114 _csrsubj=$(_readSubjectFromCSR "$_csrfile")
4c2a3841 3115 if [ "$?" != "0" ] || [ -z "$_csrsubj" ]; then
10afcaca 3116 _err "Can not read subject from csr: $_csrfile"
3117 return 1
3118 fi
4c2a3841 3119
10afcaca 3120 _info "Subject=$_csrsubj"
3121
3122 _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile")
4c2a3841 3123 if [ "$?" != "0" ]; then
10afcaca 3124 _err "Can not read domain list from csr: $_csrfile"
3125 return 1
3126 fi
3127 _debug "_csrdomainlist" "$_csrdomainlist"
3128
3129 _info "SubjectAltNames=$_csrdomainlist"
3130
10afcaca 3131 _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile")
4c2a3841 3132 if [ "$?" != "0" ] || [ -z "$_csrkeylength" ]; then
10afcaca 3133 _err "Can not read key length from csr: $_csrfile"
3134 return 1
3135 fi
3136 _info "KeyLength=$_csrkeylength"
3137}
3138
6d7eda3e 3139list() {
22ea4004 3140 _raw="$1"
6d7eda3e 3141 _initpath
4c2a3841 3142
dcf4f8f6 3143 _sep="|"
4c2a3841 3144 if [ "$_raw" ]; then
d5ec5f80 3145 printf "%s\n" "Main_Domain${_sep}KeyLength${_sep}SAN_Domains${_sep}Created${_sep}Renew"
e591d5cf 3146 for di in "${CERT_HOME}"/*.*/; do
44483dba 3147 if ! [ -d "$di" ]; then
3498a585 3148 _debug "Not directory, skip: $di"
3149 continue
3150 fi
e591d5cf 3151 d=$(basename "$di")
201aa244 3152 _debug d "$d"
dcf4f8f6 3153 (
201aa244 3154 if _endswith "$d" "$ECC_SUFFIX"; then
3155 _isEcc=$(echo "$d" | cut -d "$ECC_SEP" -f 2)
3156 d=$(echo "$d" | cut -d "$ECC_SEP" -f 1)
43822d37 3157 fi
e591d5cf 3158 _initpath "$d" "$_isEcc"
4c2a3841 3159 if [ -f "$DOMAIN_CONF" ]; then
dcf4f8f6 3160 . "$DOMAIN_CONF"
d5ec5f80 3161 printf "%s\n" "$Le_Domain${_sep}\"$Le_Keylength\"${_sep}$Le_Alt${_sep}$Le_CertCreateTimeStr${_sep}$Le_NextRenewTimeStr"
dcf4f8f6 3162 fi
3163 )
3164 done
3165 else
4c2a3841 3166 if _exists column; then
22ea4004 3167 list "raw" | column -t -s "$_sep"
3168 else
43822d37 3169 list "raw" | tr "$_sep" '\t'
22ea4004 3170 fi
dcf4f8f6 3171 fi
6d7eda3e 3172
6d7eda3e 3173}
3174
a61fe418 3175deploy() {
3176 Le_Domain="$1"
3177 Le_DeployHook="$2"
3178 _isEcc="$3"
4c2a3841 3179 if [ -z "$Le_DeployHook" ]; then
a61fe418 3180 _usage "Usage: $PROJECT_ENTRY --deploy -d domain.com --deploy-hook cpanel [--ecc] "
3181 return 1
3182 fi
3183
e591d5cf 3184 _initpath "$Le_Domain" "$_isEcc"
4c2a3841 3185 if [ ! -d "$DOMAIN_PATH" ]; then
a61fe418 3186 _err "Domain is not valid:'$Le_Domain'"
3187 return 1
3188 fi
3189
e591d5cf 3190 _deployApi="$(_findHook "$Le_Domain" deploy "$Le_DeployHook")"
4c2a3841 3191 if [ -z "$_deployApi" ]; then
a61fe418 3192 _err "The deploy hook $Le_DeployHook is not found."
3193 return 1
3194 fi
3195 _debug _deployApi "$_deployApi"
4c2a3841 3196
a61fe418 3197 _savedomainconf Le_DeployHook "$Le_DeployHook"
4c2a3841 3198
a61fe418 3199 if ! (
e591d5cf 3200 if ! . "$_deployApi"; then
a61fe418 3201 _err "Load file $_deployApi error. Please check your api file and try again."
3202 return 1
3203 fi
4c2a3841 3204
a61fe418 3205 d_command="${Le_DeployHook}_deploy"
e591d5cf 3206 if ! _exists "$d_command"; then
a61fe418 3207 _err "It seems that your api file is not correct, it must have a function named: $d_command"
3208 return 1
3209 fi
4c2a3841 3210
e591d5cf 3211 if ! $d_command "$Le_Domain" "$CERT_KEY_PATH" "$CERT_PATH" "$CA_CERT_PATH" "$CERT_FULLCHAIN_PATH"; then
a61fe418 3212 _err "Error deploy for domain:$Le_Domain"
3213 _on_issue_err
3214 return 1
3215 fi
4c2a3841 3216 ); then
a61fe418 3217 _err "Deploy error."
3218 return 1
3219 else
3220 _info "$(__green Success)"
3221 fi
4c2a3841 3222
a61fe418 3223}
3224
4c3b3608 3225installcert() {
3226 Le_Domain="$1"
4c2a3841 3227 if [ -z "$Le_Domain" ]; then
43822d37 3228 _usage "Usage: $PROJECT_ENTRY --installcert -d domain.com [--ecc] [--certpath cert-file-path] [--keypath key-file-path] [--capath ca-cert-file-path] [ --reloadCmd reloadCmd] [--fullchainpath fullchain-path]"
4c3b3608 3229 return 1
3230 fi
3231
3232 Le_RealCertPath="$2"
3233 Le_RealKeyPath="$3"
3234 Le_RealCACertPath="$4"
3235 Le_ReloadCmd="$5"
a63b05a9 3236 Le_RealFullChainPath="$6"
43822d37 3237 _isEcc="$7"
3238
4bd31f49 3239 _initpath "$Le_Domain" "$_isEcc"
4c2a3841 3240 if [ ! -d "$DOMAIN_PATH" ]; then
43822d37 3241 _err "Domain is not valid:'$Le_Domain'"
3242 return 1
3243 fi
3244
3245 _installcert
3246}
4c3b3608 3247
43822d37 3248_installcert() {
4c2a3841 3249 _savedomainconf "Le_RealCertPath" "$Le_RealCertPath"
3250 _savedomainconf "Le_RealCACertPath" "$Le_RealCACertPath"
3251 _savedomainconf "Le_RealKeyPath" "$Le_RealKeyPath"
3252 _savedomainconf "Le_ReloadCmd" "$Le_ReloadCmd"
3253 _savedomainconf "Le_RealFullChainPath" "$Le_RealFullChainPath"
4c3b3608 3254
4c2a3841 3255 if [ "$Le_RealCertPath" = "$NO_VALUE" ]; then
4d2f38b0 3256 Le_RealCertPath=""
3257 fi
4c2a3841 3258 if [ "$Le_RealKeyPath" = "$NO_VALUE" ]; then
4d2f38b0 3259 Le_RealKeyPath=""
3260 fi
4c2a3841 3261 if [ "$Le_RealCACertPath" = "$NO_VALUE" ]; then
4d2f38b0 3262 Le_RealCACertPath=""
3263 fi
4c2a3841 3264 if [ "$Le_ReloadCmd" = "$NO_VALUE" ]; then
4d2f38b0 3265 Le_ReloadCmd=""
3266 fi
4c2a3841 3267 if [ "$Le_RealFullChainPath" = "$NO_VALUE" ]; then
4d2f38b0 3268 Le_RealFullChainPath=""
3269 fi
4c2a3841 3270
4c2a3841 3271 if [ "$Le_RealCertPath" ]; then
4bd31f49 3272
4d2f38b0 3273 _info "Installing cert to:$Le_RealCertPath"
4c2a3841 3274 if [ -f "$Le_RealCertPath" ] && [ ! "$IS_RENEW" ]; then
ff3bce32 3275 cp "$Le_RealCertPath" "$Le_RealCertPath".bak
4c3b3608 3276 fi
4c2a3841 3277 cat "$CERT_PATH" >"$Le_RealCertPath"
4c3b3608 3278 fi
4c2a3841 3279
3280 if [ "$Le_RealCACertPath" ]; then
4bd31f49 3281
4d2f38b0 3282 _info "Installing CA to:$Le_RealCACertPath"
4c2a3841 3283 if [ "$Le_RealCACertPath" = "$Le_RealCertPath" ]; then
3284 echo "" >>"$Le_RealCACertPath"
3285 cat "$CA_CERT_PATH" >>"$Le_RealCACertPath"
4c3b3608 3286 else
4c2a3841 3287 if [ -f "$Le_RealCACertPath" ] && [ ! "$IS_RENEW" ]; then
ff3bce32 3288 cp "$Le_RealCACertPath" "$Le_RealCACertPath".bak
78552b18 3289 fi
4c2a3841 3290 cat "$CA_CERT_PATH" >"$Le_RealCACertPath"
4c3b3608 3291 fi
3292 fi
3293
4c2a3841 3294 if [ "$Le_RealKeyPath" ]; then
4c3b3608 3295
4d2f38b0 3296 _info "Installing key to:$Le_RealKeyPath"
4c2a3841 3297 if [ -f "$Le_RealKeyPath" ] && [ ! "$IS_RENEW" ]; then
ff3bce32 3298 cp "$Le_RealKeyPath" "$Le_RealKeyPath".bak
4c3b3608 3299 fi
4c2a3841 3300 cat "$CERT_KEY_PATH" >"$Le_RealKeyPath"
4c3b3608 3301 fi
4c2a3841 3302
3303 if [ "$Le_RealFullChainPath" ]; then
4bd31f49 3304
4d2f38b0 3305 _info "Installing full chain to:$Le_RealFullChainPath"
4c2a3841 3306 if [ -f "$Le_RealFullChainPath" ] && [ ! "$IS_RENEW" ]; then
ff3bce32 3307 cp "$Le_RealFullChainPath" "$Le_RealFullChainPath".bak
a63b05a9 3308 fi
4c2a3841 3309 cat "$CERT_FULLCHAIN_PATH" >"$Le_RealFullChainPath"
3310 fi
4c3b3608 3311
4c2a3841 3312 if [ "$Le_ReloadCmd" ]; then
4c3b3608 3313
4c3b3608 3314 _info "Run Le_ReloadCmd: $Le_ReloadCmd"
4c2a3841 3315 if (cd "$DOMAIN_PATH" && eval "$Le_ReloadCmd"); then
43822d37 3316 _info "$(__green "Reload success")"
4d2f38b0 3317 else
3318 _err "Reload error for :$Le_Domain"
3319 fi
3320 fi
3321
4c3b3608 3322}
3323
3324installcronjob() {
3325 _initpath
4c2a3841 3326 if ! _exists "crontab"; then
77546ea5 3327 _err "crontab doesn't exist, so, we can not install cron jobs."
3328 _err "All your certs will not be renewed automatically."
a7b7355d 3329 _err "You must add your own cron job to call '$PROJECT_ENTRY --cron' everyday."
77546ea5 3330 return 1
3331 fi
3332
4c3b3608 3333 _info "Installing cron job"
4c2a3841 3334 if ! crontab -l | grep "$PROJECT_ENTRY --cron"; then
3335 if [ -f "$LE_WORKING_DIR/$PROJECT_ENTRY" ]; then
a7b7355d 3336 lesh="\"$LE_WORKING_DIR\"/$PROJECT_ENTRY"
4c3b3608 3337 else
a7b7355d 3338 _err "Can not install cronjob, $PROJECT_ENTRY not found."
4c3b3608 3339 return 1
3340 fi
4c2a3841 3341 if _exists uname && uname -a | grep solaris >/dev/null; then
3342 crontab -l | {
3343 cat
3344 echo "0 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null"
3345 } | crontab --
22ea4004 3346 else
4c2a3841 3347 crontab -l | {
3348 cat
3349 echo "0 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null"
3350 } | crontab -
22ea4004 3351 fi
4c3b3608 3352 fi
4c2a3841 3353 if [ "$?" != "0" ]; then
4c3b3608 3354 _err "Install cron job failed. You need to manually renew your certs."
3355 _err "Or you can add cronjob by yourself:"
a7b7355d 3356 _err "$lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null"
4c3b3608 3357 return 1
3358 fi
3359}
3360
3361uninstallcronjob() {
4c2a3841 3362 if ! _exists "crontab"; then
37db5b81 3363 return
3364 fi
4c3b3608 3365 _info "Removing cron job"
a7b7355d 3366 cr="$(crontab -l | grep "$PROJECT_ENTRY --cron")"
4c2a3841 3367 if [ "$cr" ]; then
3368 if _exists uname && uname -a | grep solaris >/dev/null; then
22ea4004 3369 crontab -l | sed "/$PROJECT_ENTRY --cron/d" | crontab --
3370 else
3371 crontab -l | sed "/$PROJECT_ENTRY --cron/d" | crontab -
3372 fi
a7b7355d 3373 LE_WORKING_DIR="$(echo "$cr" | cut -d ' ' -f 9 | tr -d '"')"
4c3b3608 3374 _info LE_WORKING_DIR "$LE_WORKING_DIR"
4c2a3841 3375 fi
4c3b3608 3376 _initpath
a7b7355d 3377
4c3b3608 3378}
3379
6cb415f5 3380revoke() {
3381 Le_Domain="$1"
4c2a3841 3382 if [ -z "$Le_Domain" ]; then
43822d37 3383 _usage "Usage: $PROJECT_ENTRY --revoke -d domain.com"
6cb415f5 3384 return 1
3385 fi
4c2a3841 3386
43822d37 3387 _isEcc="$2"
3388
c4a375b3 3389 _initpath "$Le_Domain" "$_isEcc"
4c2a3841 3390 if [ ! -f "$DOMAIN_CONF" ]; then
6cb415f5 3391 _err "$Le_Domain is not a issued domain, skip."
4c2a3841 3392 return 1
6cb415f5 3393 fi
4c2a3841 3394
3395 if [ ! -f "$CERT_PATH" ]; then
6cb415f5 3396 _err "Cert for $Le_Domain $CERT_PATH is not found, skip."
3397 return 1
3398 fi
6cb415f5 3399
4c2a3841 3400 cert="$(_getfile "${CERT_PATH}" "${BEGIN_CERT}" "${END_CERT}" | tr -d "\r\n" | _urlencode)"
3401
3402 if [ -z "$cert" ]; then
6cb415f5 3403 _err "Cert for $Le_Domain is empty found, skip."
3404 return 1
3405 fi
4c2a3841 3406
6cb415f5 3407 data="{\"resource\": \"revoke-cert\", \"certificate\": \"$cert\"}"
3408 uri="$API/acme/revoke-cert"
3409
4c2a3841 3410 if [ -f "$CERT_KEY_PATH" ]; then
1befee5a 3411 _info "Try domain key first."
c4a375b3 3412 if _send_signed_request "$uri" "$data" "" "$CERT_KEY_PATH"; then
4c2a3841 3413 if [ -z "$response" ]; then
1befee5a 3414 _info "Revoke success."
c4a375b3 3415 rm -f "$CERT_PATH"
1befee5a 3416 return 0
4c2a3841 3417 else
1befee5a 3418 _err "Revoke error by domain key."
3419 _err "$response"
3420 fi
6cb415f5 3421 fi
4c2a3841 3422 else
1befee5a 3423 _info "Domain key file doesn't exists."
6cb415f5 3424 fi
6cb415f5 3425
1befee5a 3426 _info "Try account key."
6cb415f5 3427
c4a375b3 3428 if _send_signed_request "$uri" "$data" "" "$ACCOUNT_KEY_PATH"; then
4c2a3841 3429 if [ -z "$response" ]; then
6cb415f5 3430 _info "Revoke success."
c4a375b3 3431 rm -f "$CERT_PATH"
6cb415f5 3432 return 0
4c2a3841 3433 else
6cb415f5 3434 _err "Revoke error."
c9c31c04 3435 _debug "$response"
6cb415f5 3436 fi
3437 fi
3438 return 1
3439}
4c3b3608 3440
0c00e870 3441#domain vtype
3442_deactivate() {
3443 _d_domain="$1"
3444 _d_type="$2"
3445 _initpath
4c2a3841 3446
0c00e870 3447 _d_i=0
3448 _d_max_retry=9
4c2a3841 3449 while [ "$_d_i" -lt "$_d_max_retry" ]; do
0407c4e0 3450 _info "Deactivate: $_d_domain"
0c00e870 3451 _d_i="$(_math $_d_i + 1)"
4c2a3841 3452
3453 if ! __get_domain_new_authz "$_d_domain"; then
f940b2a5 3454 _err "Can not get domain new authz token."
0c00e870 3455 return 1
3456 fi
4c2a3841 3457
c2c8f320 3458 authzUri="$(echo "$responseHeaders" | grep "^Location:" | _head_n 1 | cut -d ' ' -f 2 | tr -d "\r\n")"
3f4513b3 3459 _debug "authzUri" "$authzUri"
0c00e870 3460
4c2a3841 3461 if [ ! -z "$code" ] && [ ! "$code" = '201' ]; then
0c00e870 3462 _err "new-authz error: $response"
3463 return 1
3464 fi
4c2a3841 3465
3466 entry="$(printf "%s\n" "$response" | _egrep_o '[^\{]*"status":"valid","uri"[^\}]*')"
0c00e870 3467 _debug entry "$entry"
4c2a3841 3468
3469 if [ -z "$entry" ]; then
fb2029e7 3470 _info "No more valid entry found."
0c00e870 3471 break
3472 fi
4c2a3841 3473
0c00e870 3474 _vtype="$(printf "%s\n" "$entry" | _egrep_o '"type": *"[^"]*"' | cut -d : -f 2 | tr -d '"')"
c4a375b3 3475 _debug _vtype "$_vtype"
0c00e870 3476 _info "Found $_vtype"
3477
4c2a3841 3478 uri="$(printf "%s\n" "$entry" | _egrep_o '"uri":"[^"]*' | cut -d : -f 2,3 | tr -d '"')"
c4a375b3 3479 _debug uri "$uri"
4c2a3841 3480
3481 if [ "$_d_type" ] && [ "$_d_type" != "$_vtype" ]; then
0c00e870 3482 _info "Skip $_vtype"
3483 continue
3484 fi
4c2a3841 3485
0c00e870 3486 _info "Deactivate: $_vtype"
4c2a3841 3487
3488 if ! _send_signed_request "$authzUri" "{\"resource\": \"authz\", \"status\":\"deactivated\"}"; then
0c00e870 3489 _err "Can not deactivate $_vtype."
3490 return 1
3491 fi
4c2a3841 3492
fb2029e7 3493 _info "Deactivate: $_vtype success."
4c2a3841 3494
0c00e870 3495 done
3496 _debug "$_d_i"
4c2a3841 3497 if [ "$_d_i" -lt "$_d_max_retry" ]; then
0c00e870 3498 _info "Deactivated success!"
3499 else
3500 _err "Deactivate failed."
3501 fi
3502
3503}
3504
3505deactivate() {
3f4513b3 3506 _d_domain_list="$1"
0c00e870 3507 _d_type="$2"
3508 _initpath
3f4513b3 3509 _debug _d_domain_list "$_d_domain_list"
4c2a3841 3510 if [ -z "$(echo $_d_domain_list | cut -d , -f 1)" ]; then
3f4513b3 3511 _usage "Usage: $PROJECT_ENTRY --deactivate -d domain.com [-d domain.com]"
0c00e870 3512 return 1
3513 fi
4c2a3841 3514 for _d_dm in $(echo "$_d_domain_list" | tr ',' ' '); do
3515 if [ -z "$_d_dm" ] || [ "$_d_dm" = "$NO_VALUE" ]; then
3f4513b3 3516 continue
3517 fi
c4a375b3 3518 if ! _deactivate "$_d_dm" "$_d_type"; then
86c017ec 3519 return 1
3520 fi
3f4513b3 3521 done
0c00e870 3522}
3523
4c3b3608 3524# Detect profile file if not specified as environment variable
3525_detect_profile() {
4c2a3841 3526 if [ -n "$PROFILE" -a -f "$PROFILE" ]; then
4c3b3608 3527 echo "$PROFILE"
3528 return
3529 fi
3530
4c3b3608 3531 DETECTED_PROFILE=''
4c3b3608 3532 SHELLTYPE="$(basename "/$SHELL")"
3533
4c2a3841 3534 if [ "$SHELLTYPE" = "bash" ]; then
3535 if [ -f "$HOME/.bashrc" ]; then
4c3b3608 3536 DETECTED_PROFILE="$HOME/.bashrc"
4c2a3841 3537 elif [ -f "$HOME/.bash_profile" ]; then
4c3b3608 3538 DETECTED_PROFILE="$HOME/.bash_profile"
3539 fi
4c2a3841 3540 elif [ "$SHELLTYPE" = "zsh" ]; then
4c3b3608 3541 DETECTED_PROFILE="$HOME/.zshrc"
3542 fi
3543
4c2a3841 3544 if [ -z "$DETECTED_PROFILE" ]; then
3545 if [ -f "$HOME/.profile" ]; then
4c3b3608 3546 DETECTED_PROFILE="$HOME/.profile"
4c2a3841 3547 elif [ -f "$HOME/.bashrc" ]; then
4c3b3608 3548 DETECTED_PROFILE="$HOME/.bashrc"
4c2a3841 3549 elif [ -f "$HOME/.bash_profile" ]; then
4c3b3608 3550 DETECTED_PROFILE="$HOME/.bash_profile"
4c2a3841 3551 elif [ -f "$HOME/.zshrc" ]; then
4c3b3608 3552 DETECTED_PROFILE="$HOME/.zshrc"
3553 fi
3554 fi
3555
4c2a3841 3556 if [ ! -z "$DETECTED_PROFILE" ]; then
4c3b3608 3557 echo "$DETECTED_PROFILE"
3558 fi
3559}
3560
3561_initconf() {
3562 _initpath
4c2a3841 3563 if [ ! -f "$ACCOUNT_CONF_PATH" ]; then
d53289d7 3564 echo "#ACCOUNT_CONF_PATH=xxxx
3565
3566#Account configurations:
4c3b3608 3567#Here are the supported macros, uncomment them to make them take effect.
d53289d7 3568
caa2e45a 3569#ACCOUNT_EMAIL=aaa@example.com # the account email used to register account.
5fd3f21b 3570#ACCOUNT_KEY_PATH=\"/path/to/account.key\"
b2817897 3571#CERT_HOME=\"/path/to/cert/home\"
4c3b3608 3572
d404e92d 3573
3574
d0871bda 3575#LOG_FILE=\"$DEFAULT_LOG_FILE\"
6b500036 3576#LOG_LEVEL=1
5ea6e9c9 3577
251d1c5c 3578#AUTO_UPGRADE=\"1\"
89002ed2 3579
4c3b3608 3580#STAGE=1 # Use the staging api
3581#FORCE=1 # Force to issue cert
3582#DEBUG=1 # Debug mode
3583
a746139c 3584#OPENSSL_BIN=openssl
166096dc 3585
8814a348 3586#USER_AGENT=\"$USER_AGENT\"
281aa349 3587
c4a375b3 3588#USER_PATH=
281aa349 3589
4c3b3608 3590#dns api
3591#######################
3592#Cloudflare:
3593#api key
3d49985a 3594#CF_Key=\"sdfsdfsdfljlbjkljlkjsdfoiwje\"
4c3b3608 3595#account email
3d49985a 3596#CF_Email=\"xxxx@sss.com\"
4c3b3608 3597
3598#######################
3599#Dnspod.cn:
3600#api key id
3d49985a 3601#DP_Id=\"1234\"
4c3b3608 3602#api key
3d49985a 3603#DP_Key=\"sADDsdasdgdsf\"
4c3b3608 3604
3605#######################
3606#Cloudxns.com:
3d49985a 3607#CX_Key=\"1234\"
4c3b3608 3608#
3d49985a 3609#CX_Secret=\"sADDsdasdgdsf\"
30de13b4 3610
3611#######################
3612#Godaddy.com:
3613#GD_Key=\"sdfdsgdgdfdasfds\"
3614#
3615#GD_Secret=\"sADDsdasdfsdfdssdgdsf\"
4c3b3608 3616
2d279c4c
PK
3617#######################
3618#nsupdate:
3619#NSUPDATE_KEY=\"/path/to/update.key\"
3620#NSUPDATE_SERVER=\"192.168.0.1\"
3621
d6f0c2b5
MZ
3622#######################
3623#PowerDNS:
3624#PDNS_Url=\"http://ns.example.com:8081\"
3625#PDNS_ServerId=\"localhost\"
3626#PDNS_Token=\"0123456789ABCDEF\"
3627#PDNS_Ttl=60
3628
5b771039 3629#######################
3630#Amazon Route53:
3631#AWS_ACCESS_KEY_ID=XXXXXXXXXX
3632#AWS_SECRET_ACCESS_KEY=XXXXXXXXXXXXXXX
3633
d5ec5f80 3634 " >"$ACCOUNT_CONF_PATH"
4c3b3608 3635 fi
3636}
3637
c8e9a31e 3638# nocron
c60883ef 3639_precheck() {
c8e9a31e 3640 _nocron="$1"
4c2a3841 3641
3642 if ! _exists "curl" && ! _exists "wget"; then
c60883ef 3643 _err "Please install curl or wget first, we need to access http resources."
4c3b3608 3644 return 1
3645 fi
4c2a3841 3646
3647 if [ -z "$_nocron" ]; then
3648 if ! _exists "crontab"; then
c8e9a31e 3649 _err "It is recommended to install crontab first. try to install 'cron, crontab, crontabs or vixie-cron'."
3650 _err "We need to set cron job to renew the certs automatically."
3651 _err "Otherwise, your certs will not be able to be renewed automatically."
4c2a3841 3652 if [ -z "$FORCE" ]; then
c8e9a31e 3653 _err "Please add '--force' and try install again to go without crontab."
3654 _err "./$PROJECT_ENTRY --install --force"
3655 return 1
3656 fi
77546ea5 3657 fi
4c3b3608 3658 fi
4c2a3841 3659
a746139c 3660 if ! _exists "$OPENSSL_BIN"; then
c60883ef 3661 _err "Please install openssl first."
3662 _err "We need openssl to generate keys."
4c3b3608 3663 return 1
3664 fi
4c2a3841 3665
3666 if ! _exists "nc"; then
c60883ef 3667 _err "It is recommended to install nc first, try to install 'nc' or 'netcat'."
3668 _err "We use nc for standalone server if you use standalone mode."
3669 _err "If you don't use standalone mode, just ignore this warning."
3670 fi
4c2a3841 3671
c60883ef 3672 return 0
3673}
3674
0a7c9364 3675_setShebang() {
3676 _file="$1"
3677 _shebang="$2"
4c2a3841 3678 if [ -z "$_shebang" ]; then
43822d37 3679 _usage "Usage: file shebang"
0a7c9364 3680 return 1
3681 fi
3682 cp "$_file" "$_file.tmp"
4c2a3841 3683 echo "$_shebang" >"$_file"
3684 sed -n 2,99999p "$_file.tmp" >>"$_file"
3685 rm -f "$_file.tmp"
0a7c9364 3686}
3687
94dc5f33 3688_installalias() {
3689 _initpath
3690
3691 _envfile="$LE_WORKING_DIR/$PROJECT_ENTRY.env"
4c2a3841 3692 if [ "$_upgrading" ] && [ "$_upgrading" = "1" ]; then
44edb2bd 3693 echo "$(cat "$_envfile")" | sed "s|^LE_WORKING_DIR.*$||" >"$_envfile"
3694 echo "$(cat "$_envfile")" | sed "s|^alias le.*$||" >"$_envfile"
3695 echo "$(cat "$_envfile")" | sed "s|^alias le.sh.*$||" >"$_envfile"
94dc5f33 3696 fi
3697
1786a5e5 3698 _setopt "$_envfile" "export LE_WORKING_DIR" "=" "\"$LE_WORKING_DIR\""
94dc5f33 3699 _setopt "$_envfile" "alias $PROJECT_ENTRY" "=" "\"$LE_WORKING_DIR/$PROJECT_ENTRY\""
3700
3701 _profile="$(_detect_profile)"
4c2a3841 3702 if [ "$_profile" ]; then
94dc5f33 3703 _debug "Found profile: $_profile"
aba5c634 3704 _info "Installing alias to '$_profile'"
94dc5f33 3705 _setopt "$_profile" ". \"$_envfile\""
3706 _info "OK, Close and reopen your terminal to start using $PROJECT_NAME"
3707 else
3708 _info "No profile is found, you will need to go into $LE_WORKING_DIR to use $PROJECT_NAME"
3709 fi
94dc5f33 3710
3711 #for csh
3712 _cshfile="$LE_WORKING_DIR/$PROJECT_ENTRY.csh"
94dc5f33 3713 _csh_profile="$HOME/.cshrc"
4c2a3841 3714 if [ -f "$_csh_profile" ]; then
aba5c634 3715 _info "Installing alias to '$_csh_profile'"
6626371d 3716 _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\""
3717 _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY\""
4c2a3841 3718 _setopt "$_csh_profile" "source \"$_cshfile\""
94dc5f33 3719 fi
4c2a3841 3720
acafa585 3721 #for tcsh
3722 _tcsh_profile="$HOME/.tcshrc"
4c2a3841 3723 if [ -f "$_tcsh_profile" ]; then
aba5c634 3724 _info "Installing alias to '$_tcsh_profile'"
acafa585 3725 _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\""
3726 _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY\""
4c2a3841 3727 _setopt "$_tcsh_profile" "source \"$_cshfile\""
acafa585 3728 fi
94dc5f33 3729
3730}
3731
c8e9a31e 3732# nocron
c60883ef 3733install() {
f3e4cea3 3734
4c2a3841 3735 if [ -z "$LE_WORKING_DIR" ]; then
f3e4cea3 3736 LE_WORKING_DIR="$DEFAULT_INSTALL_HOME"
3737 fi
4c2a3841 3738
c8e9a31e 3739 _nocron="$1"
4c2a3841 3740 if ! _initpath; then
c60883ef 3741 _err "Install failed."
4c3b3608 3742 return 1
3743 fi
4c2a3841 3744 if [ "$_nocron" ]; then
52677b0a 3745 _debug "Skip install cron job"
3746 fi
4c2a3841 3747
3748 if ! _precheck "$_nocron"; then
c60883ef 3749 _err "Pre-check failed, can not install."
4c3b3608 3750 return 1
3751 fi
4c2a3841 3752
6cc11ffb 3753 #convert from le
4c2a3841 3754 if [ -d "$HOME/.le" ]; then
3755 for envfile in "le.env" "le.sh.env"; do
3756 if [ -f "$HOME/.le/$envfile" ]; then
3757 if grep "le.sh" "$HOME/.le/$envfile" >/dev/null; then
3758 _upgrading="1"
3759 _info "You are upgrading from le.sh"
3760 _info "Renaming \"$HOME/.le\" to $LE_WORKING_DIR"
3761 mv "$HOME/.le" "$LE_WORKING_DIR"
3762 mv "$LE_WORKING_DIR/$envfile" "$LE_WORKING_DIR/$PROJECT_ENTRY.env"
3763 break
6cc11ffb 3764 fi
3765 fi
3766 done
3767 fi
3768
4c3b3608 3769 _info "Installing to $LE_WORKING_DIR"
635695ec 3770
4c2a3841 3771 if ! mkdir -p "$LE_WORKING_DIR"; then
90035252 3772 _err "Can not create working dir: $LE_WORKING_DIR"
4a0f23e2 3773 return 1
3774 fi
4c2a3841 3775
762978f8 3776 chmod 700 "$LE_WORKING_DIR"
3777
d5ec5f80 3778 cp "$PROJECT_ENTRY" "$LE_WORKING_DIR/" && chmod +x "$LE_WORKING_DIR/$PROJECT_ENTRY"
4c3b3608 3779
4c2a3841 3780 if [ "$?" != "0" ]; then
a7b7355d 3781 _err "Install failed, can not copy $PROJECT_ENTRY"
4c3b3608 3782 return 1
3783 fi
3784
a7b7355d 3785 _info "Installed to $LE_WORKING_DIR/$PROJECT_ENTRY"
4c3b3608 3786
94dc5f33 3787 _installalias
4c3b3608 3788
4c2a3841 3789 for subf in $_SUB_FOLDERS; do
3790 if [ -d "$subf" ]; then
d5ec5f80 3791 mkdir -p "$LE_WORKING_DIR/$subf"
3792 cp "$subf"/* "$LE_WORKING_DIR"/"$subf"/
a61fe418 3793 fi
3794 done
3795
4c2a3841 3796 if [ ! -f "$ACCOUNT_CONF_PATH" ]; then
4c3b3608 3797 _initconf
3798 fi
6cc11ffb 3799
4c2a3841 3800 if [ "$_DEFAULT_ACCOUNT_CONF_PATH" != "$ACCOUNT_CONF_PATH" ]; then
635695ec 3801 _setopt "$_DEFAULT_ACCOUNT_CONF_PATH" "ACCOUNT_CONF_PATH" "=" "\"$ACCOUNT_CONF_PATH\""
6cc11ffb 3802 fi
3803
4c2a3841 3804 if [ "$_DEFAULT_CERT_HOME" != "$CERT_HOME" ]; then
b2817897 3805 _saveaccountconf "CERT_HOME" "$CERT_HOME"
3806 fi
3807
4c2a3841 3808 if [ "$_DEFAULT_ACCOUNT_KEY_PATH" != "$ACCOUNT_KEY_PATH" ]; then
b2817897 3809 _saveaccountconf "ACCOUNT_KEY_PATH" "$ACCOUNT_KEY_PATH"
3810 fi
4c2a3841 3811
3812 if [ -z "$_nocron" ]; then
c8e9a31e 3813 installcronjob
3814 fi
0a7c9364 3815
4c2a3841 3816 if [ -z "$NO_DETECT_SH" ]; then
641989fd 3817 #Modify shebang
4c2a3841 3818 if _exists bash; then
66990cf8 3819 _info "Good, bash is found, so change the shebang to use bash as prefered."
641989fd 3820 _shebang='#!/usr/bin/env bash'
3821 _setShebang "$LE_WORKING_DIR/$PROJECT_ENTRY" "$_shebang"
4c2a3841 3822 for subf in $_SUB_FOLDERS; do
3823 if [ -d "$LE_WORKING_DIR/$subf" ]; then
3824 for _apifile in "$LE_WORKING_DIR/$subf/"*.sh; do
a61fe418 3825 _setShebang "$_apifile" "$_shebang"
3826 done
3827 fi
3828 done
0a7c9364 3829 fi
3830 fi
3831
4c3b3608 3832 _info OK
3833}
3834
52677b0a 3835# nocron
4c3b3608 3836uninstall() {
52677b0a 3837 _nocron="$1"
4c2a3841 3838 if [ -z "$_nocron" ]; then
52677b0a 3839 uninstallcronjob
3840 fi
4c3b3608 3841 _initpath
3842
9aa3be7f 3843 _uninstallalias
4c2a3841 3844
d5ec5f80 3845 rm -f "$LE_WORKING_DIR/$PROJECT_ENTRY"
9aa3be7f 3846 _info "The keys and certs are in $LE_WORKING_DIR, you can remove them by yourself."
3847
3848}
3849
3850_uninstallalias() {
3851 _initpath
3852
4c3b3608 3853 _profile="$(_detect_profile)"
4c2a3841 3854 if [ "$_profile" ]; then
9aa3be7f 3855 _info "Uninstalling alias from: '$_profile'"
d5ec5f80 3856 text="$(cat "$_profile")"
4c2a3841 3857 echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.env\"$||" >"$_profile"
4c3b3608 3858 fi
3859
94dc5f33 3860 _csh_profile="$HOME/.cshrc"
4c2a3841 3861 if [ -f "$_csh_profile" ]; then
9aa3be7f 3862 _info "Uninstalling alias from: '$_csh_profile'"
d5ec5f80 3863 text="$(cat "$_csh_profile")"
4c2a3841 3864 echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" >"$_csh_profile"
94dc5f33 3865 fi
4c2a3841 3866
acafa585 3867 _tcsh_profile="$HOME/.tcshrc"
4c2a3841 3868 if [ -f "$_tcsh_profile" ]; then
9aa3be7f 3869 _info "Uninstalling alias from: '$_csh_profile'"
d5ec5f80 3870 text="$(cat "$_tcsh_profile")"
4c2a3841 3871 echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" >"$_tcsh_profile"
acafa585 3872 fi
4c3b3608 3873
3874}
3875
3876cron() {
281aa349 3877 IN_CRON=1
89002ed2 3878 _initpath
4c2a3841 3879 if [ "$AUTO_UPGRADE" = "1" ]; then
89002ed2 3880 export LE_WORKING_DIR
3881 (
4c2a3841 3882 if ! upgrade; then
3883 _err "Cron:Upgrade failed!"
3884 return 1
3885 fi
89002ed2 3886 )
d5ec5f80 3887 . "$LE_WORKING_DIR/$PROJECT_ENTRY" >/dev/null
1ab63043 3888
4c2a3841 3889 if [ -t 1 ]; then
1ab63043 3890 __INTERACTIVE="1"
3891 fi
4c2a3841 3892
89002ed2 3893 _info "Auto upgraded to: $VER"
3894 fi
4c3b3608 3895 renewAll
cc179731 3896 _ret="$?"
281aa349 3897 IN_CRON=""
0ba95a3d 3898 exit $_ret
4c3b3608 3899}
3900
3901version() {
a63b05a9 3902 echo "$PROJECT"
3903 echo "v$VER"
4c3b3608 3904}
3905
3906showhelp() {
d0871bda 3907 _initpath
4c3b3608 3908 version
a7b7355d 3909 echo "Usage: $PROJECT_ENTRY command ...[parameters]....
a63b05a9 3910Commands:
3911 --help, -h Show this help message.
3912 --version, -v Show version info.
a7b7355d 3913 --install Install $PROJECT_NAME to your system.
3914 --uninstall Uninstall $PROJECT_NAME, and uninstall the cron job.
10afcaca 3915 --upgrade Upgrade $PROJECT_NAME to the latest code from $PROJECT .
a63b05a9 3916 --issue Issue a cert.
10afcaca 3917 --signcsr Issue a cert from an existing csr.
a61fe418 3918 --deploy Deploy the cert to your server.
a63b05a9 3919 --installcert Install the issued cert to apache/nginx or any other server.
3920 --renew, -r Renew a cert.
10afcaca 3921 --renewAll Renew all the certs.
a63b05a9 3922 --revoke Revoke a cert.
10afcaca 3923 --list List all the certs.
3924 --showcsr Show the content of a csr.
a63b05a9 3925 --installcronjob Install the cron job to renew certs, you don't need to call this. The 'install' command can automatically install the cron job.
3926 --uninstallcronjob Uninstall the cron job. The 'uninstall' command can do this automatically.
3927 --cron Run cron job to renew all the certs.
3928 --toPkcs Export the certificate and key to a pfx file.
eb59817e 3929 --updateaccount Update account info.
3930 --registeraccount Register account key.
a63b05a9 3931 --createAccountKey, -cak Create an account private key, professional use.
3932 --createDomainKey, -cdk Create an domain private key, professional use.
3933 --createCSR, -ccsr Create CSR , professional use.
0c00e870 3934 --deactivate Deactivate the domain authz, professional use.
a63b05a9 3935
3936Parameters:
3937 --domain, -d domain.tld Specifies a domain, used to issue, renew or revoke etc.
3938 --force, -f Used to force to install or force to renew a cert immediately.
3939 --staging, --test Use staging server, just for test.
3940 --debug Output debug info.
3941
3942 --webroot, -w /path/to/webroot Specifies the web root folder for web root mode.
3943 --standalone Use standalone mode.
e22bcf7c 3944 --tls Use standalone tls mode.
a63b05a9 3945 --apache Use apache mode.
eccec5f6 3946 --dns [dns_cf|dns_dp|dns_cx|/path/to/api/file] Use dns mode or dns api.
4a4dacb5 3947 --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.
a63b05a9 3948
3949 --keylength, -k [2048] Specifies the domain key length: 2048, 3072, 4096, 8192 or ec-256, ec-384.
3950 --accountkeylength, -ak [2048] Specifies the account key length.
d0871bda 3951 --log [/path/to/logfile] Specifies the log file. The default is: \"$DEFAULT_LOG_FILE\" if you don't give a file path here.
a73c5b33 3952 --log-level 1|2 Specifies the log level, default is 1.
a63b05a9 3953
3954 These parameters are to install the cert to nginx/apache or anyother server after issue/renew a cert:
3955
3956 --certpath /path/to/real/cert/file After issue/renew, the cert will be copied to this path.
3957 --keypath /path/to/real/key/file After issue/renew, the key will be copied to this path.
3958 --capath /path/to/real/ca/file After issue/renew, the intermediate cert will be copied to this path.
3959 --fullchainpath /path/to/fullchain/file After issue/renew, the fullchain cert will be copied to this path.
3960
3961 --reloadcmd \"service nginx reload\" After issue/renew, it's used to reload the server.
3962
3963 --accountconf Specifies a customized account config file.
635695ec 3964 --home Specifies the home dir for $PROJECT_NAME .
39c8f79f 3965 --certhome Specifies the home dir to save all the certs, only valid for '--install' command.
635695ec 3966 --useragent Specifies the user agent string. it will be saved for future use too.
b5eb4b90 3967 --accountemail Specifies the account email for registering, Only valid for the '--install' command.
06625071 3968 --accountkey Specifies the account key path, Only valid for the '--install' command.
523c7682 3969 --days Specifies the days to renew the cert when using '--issue' command. The max value is $MAX_RENEW days.
39c8f79f 3970 --httpport Specifies the standalone listening port. Only valid if the server is behind a reverse proxy or load balancer.
e22bcf7c 3971 --tlsport Specifies the standalone tls listening port. Only valid if the server is behind a reverse proxy or load balancer.
6ae0f7f5 3972 --local-address Specifies the standalone/tls server listening address, in case you have multiple ip addresses.
dcf4f8f6 3973 --listraw Only used for '--list' command, list the certs in raw format.
c8e9a31e 3974 --stopRenewOnError, -se Only valid for '--renewall' command. Stop if one cert has error in renewal.
13d7cae9 3975 --insecure Do not check the server certificate, in some devices, the api server's certificate may not be trusted.
78009539 3976 --ca-bundle Specifices the path to the CA certificate bundle to verify api server's certificate.
bc96082f 3977 --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.
43822d37 3978 --ecc Specifies to use the ECC cert. Valid for '--installcert', '--renew', '--revoke', '--toPkcs' and '--createCSR'
10afcaca 3979 --csr Specifies the input csr.
b0070f03 3980 --pre-hook Command to be run before obtaining any certificates.
3981 --post-hook Command to be run after attempting to obtain/renew certificates. No matter the obain/renew is success or failed.
3982 --renew-hook Command to be run once for each successfully renewed certificate.
a61fe418 3983 --deploy-hook The hook file to deploy cert
0c9546cc 3984 --ocsp-must-staple, --ocsp Generate ocsp must Staple extension.
6bf281f9 3985 --auto-upgrade [0|1] Valid for '--upgrade' command, indicating whether to upgrade automatically in future.
6ae0f7f5 3986 --listen-v4 Force standalone/tls server to listen at ipv4.
3987 --listen-v6 Force standalone/tls server to listen at ipv6.
a746139c 3988 --openssl-bin Specifies a custom openssl bin location.
4c3b3608 3989 "
3990}
3991
52677b0a 3992# nocron
4a0f23e2 3993_installOnline() {
3994 _info "Installing from online archive."
52677b0a 3995 _nocron="$1"
4c2a3841 3996 if [ ! "$BRANCH" ]; then
4a0f23e2 3997 BRANCH="master"
3998 fi
a8df88ab 3999
4a0f23e2 4000 target="$PROJECT/archive/$BRANCH.tar.gz"
4001 _info "Downloading $target"
4002 localname="$BRANCH.tar.gz"
4c2a3841 4003 if ! _get "$target" >$localname; then
df9547ae 4004 _err "Download error."
4a0f23e2 4005 return 1
4006 fi
0bbe6eef 4007 (
4c2a3841 4008 _info "Extracting $localname"
4009 tar xzf $localname
4010
4011 cd "$PROJECT_NAME-$BRANCH"
4012 chmod +x $PROJECT_ENTRY
4013 if ./$PROJECT_ENTRY install "$_nocron"; then
4014 _info "Install success!"
4015 fi
4016
4017 cd ..
4018
4019 rm -rf "$PROJECT_NAME-$BRANCH"
4020 rm -f "$localname"
0bbe6eef 4021 )
4a0f23e2 4022}
4023
52677b0a 4024upgrade() {
4025 if (
267f283a 4026 _initpath
4027 export LE_WORKING_DIR
d0b748a4 4028 cd "$LE_WORKING_DIR"
52677b0a 4029 _installOnline "nocron"
4c2a3841 4030 ); then
52677b0a 4031 _info "Upgrade success!"
096d8992 4032 exit 0
52677b0a 4033 else
4034 _err "Upgrade failed!"
096d8992 4035 exit 1
52677b0a 4036 fi
4037}
a63b05a9 4038
5ea6e9c9 4039_processAccountConf() {
4c2a3841 4040 if [ "$_useragent" ]; then
5ea6e9c9 4041 _saveaccountconf "USER_AGENT" "$_useragent"
4c2a3841 4042 elif [ "$USER_AGENT" ] && [ "$USER_AGENT" != "$DEFAULT_USER_AGENT" ]; then
d0871bda 4043 _saveaccountconf "USER_AGENT" "$USER_AGENT"
5ea6e9c9 4044 fi
4c2a3841 4045
4046 if [ "$_accountemail" ]; then
5ea6e9c9 4047 _saveaccountconf "ACCOUNT_EMAIL" "$_accountemail"
4c2a3841 4048 elif [ "$ACCOUNT_EMAIL" ] && [ "$ACCOUNT_EMAIL" != "$DEFAULT_ACCOUNT_EMAIL" ]; then
d0871bda 4049 _saveaccountconf "ACCOUNT_EMAIL" "$ACCOUNT_EMAIL"
5ea6e9c9 4050 fi
4c2a3841 4051
a746139c 4052 if [ "$_openssl_bin" ]; then
4053 _saveaccountconf "OPENSSL_BIN" "$_openssl_bin"
4054 elif [ "$OPENSSL_BIN" ] && [ "$OPENSSL_BIN" != "$DEFAULT_OPENSSL_BIN" ]; then
4055 _saveaccountconf "OPENSSL_BIN" "$OPENSSL_BIN"
4056 fi
4057
4c2a3841 4058 if [ "$_auto_upgrade" ]; then
6bf281f9 4059 _saveaccountconf "AUTO_UPGRADE" "$_auto_upgrade"
4c2a3841 4060 elif [ "$AUTO_UPGRADE" ]; then
6bf281f9 4061 _saveaccountconf "AUTO_UPGRADE" "$AUTO_UPGRADE"
4062 fi
4c2a3841 4063
5ea6e9c9 4064}
4065
a63b05a9 4066_process() {
4067 _CMD=""
4068 _domain=""
3f4513b3 4069 _altdomains="$NO_VALUE"
a63b05a9 4070 _webroot=""
bdbf323f 4071 _keylength=""
4072 _accountkeylength=""
4073 _certpath=""
4074 _keypath=""
4075 _capath=""
4076 _fullchainpath=""
4d2f38b0 4077 _reloadcmd=""
a63b05a9 4078 _password=""
635695ec 4079 _accountconf=""
4080 _useragent=""
b5eb4b90 4081 _accountemail=""
4082 _accountkey=""
b2817897 4083 _certhome=""
39c8f79f 4084 _httpport=""
e22bcf7c 4085 _tlsport=""
0e38c60d 4086 _dnssleep=""
dcf4f8f6 4087 _listraw=""
cc179731 4088 _stopRenewOnError=""
e3698edd 4089 #_insecure=""
78009539 4090 _ca_bundle=""
c8e9a31e 4091 _nocron=""
43822d37 4092 _ecc=""
10afcaca 4093 _csr=""
b0070f03 4094 _pre_hook=""
4095 _post_hook=""
4096 _renew_hook=""
a61fe418 4097 _deploy_hook=""
5ea6e9c9 4098 _logfile=""
d0871bda 4099 _log=""
0463b5d6 4100 _local_address=""
a73c5b33 4101 _log_level=""
6bf281f9 4102 _auto_upgrade=""
6ae0f7f5 4103 _listen_v4=""
4104 _listen_v6=""
a746139c 4105 _openssl_bin=""
4c2a3841 4106 while [ ${#} -gt 0 ]; do
a63b05a9 4107 case "${1}" in
4c2a3841 4108
4109 --help | -h)
a63b05a9 4110 showhelp
4111 return
4112 ;;
4c2a3841 4113 --version | -v)
a63b05a9 4114 version
4115 return
4116 ;;
4c2a3841 4117 --install)
a63b05a9 4118 _CMD="install"
4119 ;;
4c2a3841 4120 --uninstall)
a63b05a9 4121 _CMD="uninstall"
4122 ;;
4c2a3841 4123 --upgrade)
52677b0a 4124 _CMD="upgrade"
4125 ;;
4c2a3841 4126 --issue)
a63b05a9 4127 _CMD="issue"
4128 ;;
4c2a3841 4129 --deploy)
a61fe418 4130 _CMD="deploy"
4131 ;;
4c2a3841 4132 --signcsr)
10afcaca 4133 _CMD="signcsr"
4134 ;;
4c2a3841 4135 --showcsr)
10afcaca 4136 _CMD="showcsr"
4137 ;;
4c2a3841 4138 --installcert | -i)
a63b05a9 4139 _CMD="installcert"
4140 ;;
4c2a3841 4141 --renew | -r)
a63b05a9 4142 _CMD="renew"
4143 ;;
4c2a3841 4144 --renewAll | --renewall)
a63b05a9 4145 _CMD="renewAll"
4146 ;;
4c2a3841 4147 --revoke)
a63b05a9 4148 _CMD="revoke"
4149 ;;
4c2a3841 4150 --list)
6d7eda3e 4151 _CMD="list"
4152 ;;
4c2a3841 4153 --installcronjob)
a63b05a9 4154 _CMD="installcronjob"
4155 ;;
4c2a3841 4156 --uninstallcronjob)
a63b05a9 4157 _CMD="uninstallcronjob"
4158 ;;
4c2a3841 4159 --cron)
a63b05a9 4160 _CMD="cron"
4161 ;;
4c2a3841 4162 --toPkcs)
a63b05a9 4163 _CMD="toPkcs"
4c2a3841 4164 ;;
4165 --createAccountKey | --createaccountkey | -cak)
a63b05a9 4166 _CMD="createAccountKey"
4167 ;;
4c2a3841 4168 --createDomainKey | --createdomainkey | -cdk)
a63b05a9 4169 _CMD="createDomainKey"
4170 ;;
4c2a3841 4171 --createCSR | --createcsr | -ccr)
a63b05a9 4172 _CMD="createCSR"
4173 ;;
4c2a3841 4174 --deactivate)
0c00e870 4175 _CMD="deactivate"
4176 ;;
4c2a3841 4177 --updateaccount)
eb59817e 4178 _CMD="updateaccount"
4179 ;;
4c2a3841 4180 --registeraccount)
eb59817e 4181 _CMD="registeraccount"
4182 ;;
4c2a3841 4183 --domain | -d)
a63b05a9 4184 _dvalue="$2"
4c2a3841 4185
4186 if [ "$_dvalue" ]; then
4187 if _startswith "$_dvalue" "-"; then
ee1737a5 4188 _err "'$_dvalue' is not a valid domain for parameter '$1'"
4189 return 1
4190 fi
4c2a3841 4191 if _is_idn "$_dvalue" && ! _exists idn; then
9774b01b 4192 _err "It seems that $_dvalue is an IDN( Internationalized Domain Names), please install 'idn' command first."
4193 return 1
4194 fi
4c2a3841 4195
4196 if [ -z "$_domain" ]; then
ee1737a5 4197 _domain="$_dvalue"
a63b05a9 4198 else
4c2a3841 4199 if [ "$_altdomains" = "$NO_VALUE" ]; then
ee1737a5 4200 _altdomains="$_dvalue"
4201 else
4202 _altdomains="$_altdomains,$_dvalue"
4203 fi
a63b05a9 4204 fi
4205 fi
4c2a3841 4206
a63b05a9 4207 shift
4208 ;;
4209
4c2a3841 4210 --force | -f)
a63b05a9 4211 FORCE="1"
4212 ;;
4c2a3841 4213 --staging | --test)
a63b05a9 4214 STAGE="1"
4215 ;;
4c2a3841 4216 --debug)
4217 if [ -z "$2" ] || _startswith "$2" "-"; then
a63b05a9 4218 DEBUG="1"
4219 else
4220 DEBUG="$2"
4221 shift
4c2a3841 4222 fi
a63b05a9 4223 ;;
4c2a3841 4224 --webroot | -w)
a63b05a9 4225 wvalue="$2"
4c2a3841 4226 if [ -z "$_webroot" ]; then
a63b05a9 4227 _webroot="$wvalue"
4228 else
4229 _webroot="$_webroot,$wvalue"
4230 fi
4231 shift
4c2a3841 4232 ;;
4233 --standalone)
3f4513b3 4234 wvalue="$NO_VALUE"
4c2a3841 4235 if [ -z "$_webroot" ]; then
a63b05a9 4236 _webroot="$wvalue"
4237 else
4238 _webroot="$_webroot,$wvalue"
4239 fi
4240 ;;
4c2a3841 4241 --local-address)
0463b5d6 4242 lvalue="$2"
4243 _local_address="$_local_address$lvalue,"
4244 shift
4245 ;;
4c2a3841 4246 --apache)
a63b05a9 4247 wvalue="apache"
4c2a3841 4248 if [ -z "$_webroot" ]; then
a63b05a9 4249 _webroot="$wvalue"
4250 else
4251 _webroot="$_webroot,$wvalue"
4252 fi
4253 ;;
4c2a3841 4254 --tls)
e22bcf7c 4255 wvalue="$W_TLS"
4c2a3841 4256 if [ -z "$_webroot" ]; then
e22bcf7c 4257 _webroot="$wvalue"
4258 else
4259 _webroot="$_webroot,$wvalue"
4260 fi
4261 ;;
4c2a3841 4262 --dns)
a63b05a9 4263 wvalue="dns"
4c2a3841 4264 if ! _startswith "$2" "-"; then
a63b05a9 4265 wvalue="$2"
4266 shift
4267 fi
4c2a3841 4268 if [ -z "$_webroot" ]; then
a63b05a9 4269 _webroot="$wvalue"
4270 else
4271 _webroot="$_webroot,$wvalue"
4272 fi
4273 ;;
4c2a3841 4274 --dnssleep)
0e38c60d 4275 _dnssleep="$2"
4276 Le_DNSSleep="$_dnssleep"
4277 shift
4278 ;;
4c2a3841 4279
4280 --keylength | -k)
a63b05a9 4281 _keylength="$2"
a63b05a9 4282 shift
4283 ;;
4c2a3841 4284 --accountkeylength | -ak)
2ce87fe2 4285 _accountkeylength="$2"
a63b05a9 4286 shift
4287 ;;
4288
4c2a3841 4289 --certpath)
a63b05a9 4290 _certpath="$2"
4291 shift
4292 ;;
4c2a3841 4293 --keypath)
a63b05a9 4294 _keypath="$2"
4295 shift
4296 ;;
4c2a3841 4297 --capath)
a63b05a9 4298 _capath="$2"
4299 shift
4300 ;;
4c2a3841 4301 --fullchainpath)
a63b05a9 4302 _fullchainpath="$2"
4303 shift
4304 ;;
4c2a3841 4305 --reloadcmd | --reloadCmd)
a63b05a9 4306 _reloadcmd="$2"
4307 shift
4308 ;;
4c2a3841 4309 --password)
a63b05a9 4310 _password="$2"
4311 shift
4312 ;;
4c2a3841 4313 --accountconf)
635695ec 4314 _accountconf="$2"
4315 ACCOUNT_CONF_PATH="$_accountconf"
a7b7355d 4316 shift
a63b05a9 4317 ;;
4c2a3841 4318 --home)
a63b05a9 4319 LE_WORKING_DIR="$2"
a7b7355d 4320 shift
a63b05a9 4321 ;;
4c2a3841 4322 --certhome)
b2817897 4323 _certhome="$2"
4324 CERT_HOME="$_certhome"
4325 shift
4c2a3841 4326 ;;
4327 --useragent)
635695ec 4328 _useragent="$2"
4329 USER_AGENT="$_useragent"
4330 shift
4331 ;;
4c2a3841 4332 --accountemail)
b5eb4b90 4333 _accountemail="$2"
4334 ACCOUNT_EMAIL="$_accountemail"
4335 shift
4336 ;;
4c2a3841 4337 --accountkey)
b5eb4b90 4338 _accountkey="$2"
4339 ACCOUNT_KEY_PATH="$_accountkey"
4340 shift
4341 ;;
4c2a3841 4342 --days)
06625071 4343 _days="$2"
4344 Le_RenewalDays="$_days"
4345 shift
4346 ;;
4c2a3841 4347 --httpport)
39c8f79f 4348 _httpport="$2"
4349 Le_HTTPPort="$_httpport"
4350 shift
4351 ;;
4c2a3841 4352 --tlsport)
e22bcf7c 4353 _tlsport="$2"
4354 Le_TLSPort="$_tlsport"
4355 shift
4356 ;;
4c2a3841 4357
4358 --listraw)
dcf4f8f6 4359 _listraw="raw"
4c2a3841 4360 ;;
4361 --stopRenewOnError | --stoprenewonerror | -se)
cc179731 4362 _stopRenewOnError="1"
4363 ;;
4c2a3841 4364 --insecure)
e3698edd 4365 #_insecure="1"
fac1e367 4366 HTTPS_INSECURE="1"
13d7cae9 4367 ;;
4c2a3841 4368 --ca-bundle)
d5ec5f80 4369 _ca_bundle="$(readlink -f "$2")"
78009539
PS
4370 CA_BUNDLE="$_ca_bundle"
4371 shift
4372 ;;
4c2a3841 4373 --nocron)
c8e9a31e 4374 _nocron="1"
4375 ;;
4c2a3841 4376 --ecc)
43822d37 4377 _ecc="isEcc"
4378 ;;
4c2a3841 4379 --csr)
10afcaca 4380 _csr="$2"
4381 shift
4382 ;;
4c2a3841 4383 --pre-hook)
b0070f03 4384 _pre_hook="$2"
4385 shift
4386 ;;
4c2a3841 4387 --post-hook)
b0070f03 4388 _post_hook="$2"
4389 shift
4390 ;;
4c2a3841 4391 --renew-hook)
b0070f03 4392 _renew_hook="$2"
4393 shift
4394 ;;
4c2a3841 4395 --deploy-hook)
a61fe418 4396 _deploy_hook="$2"
4397 shift
4398 ;;
4c2a3841 4399 --ocsp-must-staple | --ocsp)
0c9546cc 4400 Le_OCSP_Stable="1"
4401 ;;
4c2a3841 4402 --log | --logfile)
d0871bda 4403 _log="1"
5ea6e9c9 4404 _logfile="$2"
4c2a3841 4405 if _startswith "$_logfile" '-'; then
d0871bda 4406 _logfile=""
4407 else
4408 shift
4409 fi
5ea6e9c9 4410 LOG_FILE="$_logfile"
4c2a3841 4411 if [ -z "$LOG_LEVEL" ]; then
a73c5b33 4412 LOG_LEVEL="$DEFAULT_LOG_LEVEL"
4413 fi
4414 ;;
4c2a3841 4415 --log-level)
30bfc2ce 4416 _log_level="$2"
a73c5b33 4417 LOG_LEVEL="$_log_level"
4418 shift
5ea6e9c9 4419 ;;
4c2a3841 4420 --auto-upgrade)
6bf281f9 4421 _auto_upgrade="$2"
4c2a3841 4422 if [ -z "$_auto_upgrade" ] || _startswith "$_auto_upgrade" '-'; then
6bf281f9 4423 _auto_upgrade="1"
4424 else
4425 shift
4426 fi
4427 AUTO_UPGRADE="$_auto_upgrade"
4428 ;;
4c2a3841 4429 --listen-v4)
6ae0f7f5 4430 _listen_v4="1"
4431 Le_Listen_V4="$_listen_v4"
4432 ;;
4c2a3841 4433 --listen-v6)
6ae0f7f5 4434 _listen_v6="1"
4435 Le_Listen_V6="$_listen_v6"
4436 ;;
a746139c 4437 --openssl-bin)
4438 _openssl_bin="$2"
4439 OPENSSL_BIN="$_openssl_bin"
4440 ;;
4c2a3841 4441 *)
a63b05a9 4442 _err "Unknown parameter : $1"
4443 return 1
4444 ;;
4445 esac
4446
4447 shift 1
4448 done
4449
4c2a3841 4450 if [ "${_CMD}" != "install" ]; then
5ea6e9c9 4451 __initHome
661f0583 4452 if [ "$_log" ]; then
4c2a3841 4453 if [ -z "$_logfile" ]; then
661f0583 4454 _logfile="$DEFAULT_LOG_FILE"
4455 fi
d0871bda 4456 fi
4c2a3841 4457 if [ "$_logfile" ]; then
5ea6e9c9 4458 _saveaccountconf "LOG_FILE" "$_logfile"
661f0583 4459 LOG_FILE="$_logfile"
5ea6e9c9 4460 fi
a73c5b33 4461
4c2a3841 4462 if [ "$_log_level" ]; then
a73c5b33 4463 _saveaccountconf "LOG_LEVEL" "$_log_level"
4464 LOG_LEVEL="$_log_level"
4465 fi
4c2a3841 4466
5ea6e9c9 4467 _processAccountConf
4468 fi
4c2a3841 4469
9d548d81 4470 _debug2 LE_WORKING_DIR "$LE_WORKING_DIR"
4c2a3841 4471
4472 if [ "$DEBUG" ]; then
dcf9cb58 4473 version
4474 fi
a63b05a9 4475
4476 case "${_CMD}" in
c8e9a31e 4477 install) install "$_nocron" ;;
bc96082f 4478 uninstall) uninstall "$_nocron" ;;
52677b0a 4479 upgrade) upgrade ;;
a63b05a9 4480 issue)
4c2a3841 4481 issue "$_webroot" "$_domain" "$_altdomains" "$_keylength" "$_certpath" "$_keypath" "$_capath" "$_reloadcmd" "$_fullchainpath" "$_pre_hook" "$_post_hook" "$_renew_hook" "$_local_address"
a63b05a9 4482 ;;
a61fe418 4483 deploy)
4484 deploy "$_domain" "$_deploy_hook" "$_ecc"
4485 ;;
10afcaca 4486 signcsr)
4487 signcsr "$_csr" "$_webroot"
4488 ;;
4489 showcsr)
4490 showcsr "$_csr" "$_domain"
4491 ;;
a63b05a9 4492 installcert)
43822d37 4493 installcert "$_domain" "$_certpath" "$_keypath" "$_capath" "$_reloadcmd" "$_fullchainpath" "$_ecc"
a63b05a9 4494 ;;
4c2a3841 4495 renew)
43822d37 4496 renew "$_domain" "$_ecc"
a63b05a9 4497 ;;
4c2a3841 4498 renewAll)
cc179731 4499 renewAll "$_stopRenewOnError"
a63b05a9 4500 ;;
4c2a3841 4501 revoke)
43822d37 4502 revoke "$_domain" "$_ecc"
a63b05a9 4503 ;;
4c2a3841 4504 deactivate)
3f4513b3 4505 deactivate "$_domain,$_altdomains"
eb59817e 4506 ;;
4c2a3841 4507 registeraccount)
57e58ce7 4508 registeraccount "$_accountkeylength"
eb59817e 4509 ;;
4c2a3841 4510 updateaccount)
eb59817e 4511 updateaccount
4512 ;;
4c2a3841 4513 list)
dcf4f8f6 4514 list "$_listraw"
6d7eda3e 4515 ;;
a63b05a9 4516 installcronjob) installcronjob ;;
4517 uninstallcronjob) uninstallcronjob ;;
4518 cron) cron ;;
4c2a3841 4519 toPkcs)
43822d37 4520 toPkcs "$_domain" "$_password" "$_ecc"
a63b05a9 4521 ;;
4c2a3841 4522 createAccountKey)
5fbc47eb 4523 createAccountKey "$_accountkeylength"
a63b05a9 4524 ;;
4c2a3841 4525 createDomainKey)
a63b05a9 4526 createDomainKey "$_domain" "$_keylength"
4527 ;;
4c2a3841 4528 createCSR)
43822d37 4529 createCSR "$_domain" "$_altdomains" "$_ecc"
a63b05a9 4530 ;;
4531
4532 *)
4533 _err "Invalid command: $_CMD"
4c2a3841 4534 showhelp
a63b05a9 4535 return 1
4c2a3841 4536 ;;
a63b05a9 4537 esac
d3595686 4538 _ret="$?"
4c2a3841 4539 if [ "$_ret" != "0" ]; then
d3595686 4540 return $_ret
4541 fi
4c2a3841 4542
4543 if [ "${_CMD}" = "install" ]; then
4544 if [ "$_log" ]; then
4545 if [ -z "$LOG_FILE" ]; then
d0871bda 4546 LOG_FILE="$DEFAULT_LOG_FILE"
4547 fi
4548 _saveaccountconf "LOG_FILE" "$LOG_FILE"
5ea6e9c9 4549 fi
4c2a3841 4550
4551 if [ "$_log_level" ]; then
a73c5b33 4552 _saveaccountconf "LOG_LEVEL" "$_log_level"
4553 fi
5ea6e9c9 4554 _processAccountConf
b5eb4b90 4555 fi
635695ec 4556
a63b05a9 4557}
4558
4c2a3841 4559if [ "$INSTALLONLINE" ]; then
d1f97fc8 4560 INSTALLONLINE=""
4a0f23e2 4561 _installOnline $BRANCH
4562 exit
4563fi
4c3b3608 4564
319e0ae3 4565main() {
4566 [ -z "$1" ] && showhelp && return
4c2a3841 4567 if _startswith "$1" '-'; then _process "$@"; else "$@"; fi
319e0ae3 4568}
e69a7c38 4569
aa7b82de 4570main "$@"