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