]> git.proxmox.com Git - mirror_acme.sh.git/blame - acme.sh
Merge pull request #499 from sercxanto/copycert
[mirror_acme.sh.git] / acme.sh
CommitLineData
0a7c9364 1#!/usr/bin/env sh
bfdf1f48 2
8f9a1881 3VER=2.6.5
a7b7355d 4
6cc11ffb 5PROJECT_NAME="acme.sh"
a7b7355d 6
6cc11ffb 7PROJECT_ENTRY="acme.sh"
8
9PROJECT="https://github.com/Neilpang/$PROJECT_NAME"
4c3b3608 10
f3e4cea3 11DEFAULT_INSTALL_HOME="$HOME/.$PROJECT_NAME"
12_SCRIPT_="$0"
13
a61fe418 14_SUB_FOLDERS="dnsapi deploy"
f3e4cea3 15
4c3b3608 16DEFAULT_CA="https://acme-v01.api.letsencrypt.org"
c93ec933 17DEFAULT_AGREEMENT="https://letsencrypt.org/documents/LE-SA-v1.1.1-August-1-2016.pdf"
4c3b3608 18
07af4247 19DEFAULT_USER_AGENT="$PROJECT_NAME/$VER ($PROJECT)"
d0871bda 20DEFAULT_ACCOUNT_EMAIL=""
bbbdcb09 21
57e58ce7 22DEFAULT_ACCOUNT_KEY_LENGTH=2048
23DEFAULT_DOMAIN_KEY_LENGTH=2048
24
a746139c 25DEFAULT_OPENSSL_BIN="openssl"
26
4c3b3608 27STAGE_CA="https://acme-staging.api.letsencrypt.org"
28
29VTYPE_HTTP="http-01"
30VTYPE_DNS="dns-01"
e22bcf7c 31VTYPE_TLS="tls-sni-01"
e591d5cf 32#VTYPE_TLS2="tls-sni-02"
e22bcf7c 33
0463b5d6 34LOCAL_ANY_ADDRESS="0.0.0.0"
35
656bd330 36MAX_RENEW=60
523c7682 37
4a4dacb5 38DEFAULT_DNS_SLEEP=120
39
3f4513b3 40NO_VALUE="no"
41
e22bcf7c 42W_TLS="tls"
4c3b3608 43
ec603bee 44STATE_VERIFIED="verified_ok"
45
88fab7d6 46BEGIN_CSR="-----BEGIN CERTIFICATE REQUEST-----"
47END_CSR="-----END CERTIFICATE REQUEST-----"
48
49BEGIN_CERT="-----BEGIN CERTIFICATE-----"
50END_CERT="-----END CERTIFICATE-----"
51
cc179731 52RENEW_SKIP=2
53
43822d37 54ECC_SEP="_"
55ECC_SUFFIX="${ECC_SEP}ecc"
56
a73c5b33 57LOG_LEVEL_1=1
58LOG_LEVEL_2=2
59LOG_LEVEL_3=3
60DEFAULT_LOG_LEVEL="$LOG_LEVEL_1"
61
62_DEBUG_WIKI="https://github.com/Neilpang/acme.sh/wiki/How-to-debug-acme.sh"
4c3b3608 63
08ee072f 64__INTERACTIVE=""
4c2a3841 65if [ -t 1 ]; then
08ee072f 66 __INTERACTIVE="1"
67fi
00a50605 68
43822d37 69__green() {
4c2a3841 70 if [ "$__INTERACTIVE" ]; then
2d12b689 71 printf '\033[1;31;32m'
72 fi
43822d37 73 printf -- "$1"
4c2a3841 74 if [ "$__INTERACTIVE" ]; then
2d12b689 75 printf '\033[0m'
76 fi
43822d37 77}
78
79__red() {
4c2a3841 80 if [ "$__INTERACTIVE" ]; then
2d12b689 81 printf '\033[1;31;40m'
82 fi
43822d37 83 printf -- "$1"
4c2a3841 84 if [ "$__INTERACTIVE" ]; then
2d12b689 85 printf '\033[0m'
86 fi
43822d37 87}
00a50605 88
a73c5b33 89_printargs() {
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"
c7b8f223 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
d53289d7 1637 _DEFAULT_ACCOUNT_CONF_PATH="$LE_WORKING_DIR/account.conf"
1638
4c2a3841 1639 if [ -z "$ACCOUNT_CONF_PATH" ]; then
1640 if [ -f "$_DEFAULT_ACCOUNT_CONF_PATH" ]; then
8663fb7e 1641 . "$_DEFAULT_ACCOUNT_CONF_PATH"
635695ec 1642 fi
d53289d7 1643 fi
4c2a3841 1644
1645 if [ -z "$ACCOUNT_CONF_PATH" ]; then
d53289d7 1646 ACCOUNT_CONF_PATH="$_DEFAULT_ACCOUNT_CONF_PATH"
4c3b3608 1647 fi
4c2a3841 1648
d0871bda 1649 DEFAULT_LOG_FILE="$LE_WORKING_DIR/$PROJECT_NAME.log"
4c2a3841 1650
5c48e139 1651 DEFAULT_CA_HOME="$LE_WORKING_DIR/ca"
4c2a3841 1652
1653 if [ -z "$LE_TEMP_DIR" ]; then
610e0f21 1654 LE_TEMP_DIR="$LE_WORKING_DIR/tmp"
1655 fi
5ea6e9c9 1656}
1657
1658#[domain] [keylength]
1659_initpath() {
1660
1661 __initHome
1662
4c2a3841 1663 if [ -f "$ACCOUNT_CONF_PATH" ]; then
8663fb7e 1664 . "$ACCOUNT_CONF_PATH"
4c3b3608 1665 fi
1666
4c2a3841 1667 if [ "$IN_CRON" ]; then
1668 if [ ! "$_USER_PATH_EXPORTED" ]; then
281aa349 1669 _USER_PATH_EXPORTED=1
1670 export PATH="$USER_PATH:$PATH"
1671 fi
1672 fi
4c2a3841 1673
1674 if [ -z "$CA_HOME" ]; then
5c48e139 1675 CA_HOME="$DEFAULT_CA_HOME"
1676 fi
281aa349 1677
4c2a3841 1678 if [ -z "$API" ]; then
1679 if [ -z "$STAGE" ]; then
4c3b3608 1680 API="$DEFAULT_CA"
1681 else
1682 API="$STAGE_CA"
1683 _info "Using stage api:$API"
4c2a3841 1684 fi
4c3b3608 1685 fi
4c2a3841 1686
5c48e139 1687 _API_HOST="$(echo "$API" | cut -d : -f 2 | tr -d '/')"
1688 CA_DIR="$CA_HOME/$_API_HOST"
4c2a3841 1689
5c48e139 1690 _DEFAULT_CA_CONF="$CA_DIR/ca.conf"
4c2a3841 1691
1692 if [ -z "$CA_CONF" ]; then
5c48e139 1693 CA_CONF="$_DEFAULT_CA_CONF"
1694 fi
c4236e58 1695 _debug3 CA_CONF "$CA_CONF"
4c2a3841 1696
1697 if [ -f "$CA_CONF" ]; then
5c48e139 1698 . "$CA_CONF"
1699 fi
1700
4c2a3841 1701 if [ -z "$ACME_DIR" ]; then
4c3b3608 1702 ACME_DIR="/home/.acme"
1703 fi
4c2a3841 1704
1705 if [ -z "$APACHE_CONF_BACKUP_DIR" ]; then
8a144f4d 1706 APACHE_CONF_BACKUP_DIR="$LE_WORKING_DIR"
4c3b3608 1707 fi
4c2a3841 1708
1709 if [ -z "$USER_AGENT" ]; then
bbbdcb09 1710 USER_AGENT="$DEFAULT_USER_AGENT"
1711 fi
4c2a3841 1712
1713 if [ -z "$HTTP_HEADER" ]; then
933c169d 1714 HTTP_HEADER="$LE_WORKING_DIR/http.header"
1715 fi
b2817897 1716
5c48e139 1717 _OLD_ACCOUNT_KEY="$LE_WORKING_DIR/account.key"
1718 _OLD_ACCOUNT_JSON="$LE_WORKING_DIR/account.json"
4c2a3841 1719
5c48e139 1720 _DEFAULT_ACCOUNT_KEY_PATH="$CA_DIR/account.key"
1721 _DEFAULT_ACCOUNT_JSON_PATH="$CA_DIR/account.json"
4c2a3841 1722 if [ -z "$ACCOUNT_KEY_PATH" ]; then
b2817897 1723 ACCOUNT_KEY_PATH="$_DEFAULT_ACCOUNT_KEY_PATH"
4c3b3608 1724 fi
4c2a3841 1725
1726 if [ -z "$ACCOUNT_JSON_PATH" ]; then
5c48e139 1727 ACCOUNT_JSON_PATH="$_DEFAULT_ACCOUNT_JSON_PATH"
1728 fi
4c2a3841 1729
a79b26af 1730 _DEFAULT_CERT_HOME="$LE_WORKING_DIR"
4c2a3841 1731 if [ -z "$CERT_HOME" ]; then
a79b26af
RD
1732 CERT_HOME="$_DEFAULT_CERT_HOME"
1733 fi
1734
a746139c 1735 if [ -z "$OPENSSL_BIN" ]; then
1736 OPENSSL_BIN="$DEFAULT_OPENSSL_BIN"
1737 fi
1738
4c2a3841 1739 if [ -z "$1" ]; then
4c3b3608 1740 return 0
1741 fi
4c2a3841 1742
5fbc47eb 1743 domain="$1"
1744 _ilength="$2"
4c3b3608 1745
4c2a3841 1746 if [ -z "$DOMAIN_PATH" ]; then
43822d37 1747 domainhome="$CERT_HOME/$domain"
1748 domainhomeecc="$CERT_HOME/$domain$ECC_SUFFIX"
4c2a3841 1749
4c3b3608 1750 DOMAIN_PATH="$domainhome"
4c2a3841 1751
1752 if _isEccKey "$_ilength"; then
43822d37 1753 DOMAIN_PATH="$domainhomeecc"
1754 else
4c2a3841 1755 if [ ! -d "$domainhome" ] && [ -d "$domainhomeecc" ]; then
6d4e903b 1756 _info "The domain '$domain' seems to have a ECC cert already, please add '$(__red "--ecc")' parameter if you want to use that cert."
43822d37 1757 fi
1758 fi
1759 _debug DOMAIN_PATH "$DOMAIN_PATH"
4c3b3608 1760 fi
4c2a3841 1761
4c2a3841 1762 if [ -z "$DOMAIN_CONF" ]; then
43822d37 1763 DOMAIN_CONF="$DOMAIN_PATH/$domain.conf"
4c3b3608 1764 fi
4c2a3841 1765
1766 if [ -z "$DOMAIN_SSL_CONF" ]; then
0c9546cc 1767 DOMAIN_SSL_CONF="$DOMAIN_PATH/$domain.csr.conf"
4c3b3608 1768 fi
4c2a3841 1769
1770 if [ -z "$CSR_PATH" ]; then
43822d37 1771 CSR_PATH="$DOMAIN_PATH/$domain.csr"
4c3b3608 1772 fi
4c2a3841 1773 if [ -z "$CERT_KEY_PATH" ]; then
43822d37 1774 CERT_KEY_PATH="$DOMAIN_PATH/$domain.key"
4c3b3608 1775 fi
4c2a3841 1776 if [ -z "$CERT_PATH" ]; then
43822d37 1777 CERT_PATH="$DOMAIN_PATH/$domain.cer"
4c3b3608 1778 fi
4c2a3841 1779 if [ -z "$CA_CERT_PATH" ]; then
43822d37 1780 CA_CERT_PATH="$DOMAIN_PATH/ca.cer"
4c3b3608 1781 fi
4c2a3841 1782 if [ -z "$CERT_FULLCHAIN_PATH" ]; then
43822d37 1783 CERT_FULLCHAIN_PATH="$DOMAIN_PATH/fullchain.cer"
caf1fc10 1784 fi
4c2a3841 1785 if [ -z "$CERT_PFX_PATH" ]; then
43822d37 1786 CERT_PFX_PATH="$DOMAIN_PATH/$domain.pfx"
ac2d5123 1787 fi
4c2a3841 1788
1789 if [ -z "$TLS_CONF" ]; then
43822d37 1790 TLS_CONF="$DOMAIN_PATH/tls.valdation.conf"
e22bcf7c 1791 fi
4c2a3841 1792 if [ -z "$TLS_CERT" ]; then
43822d37 1793 TLS_CERT="$DOMAIN_PATH/tls.valdation.cert"
e22bcf7c 1794 fi
4c2a3841 1795 if [ -z "$TLS_KEY" ]; then
43822d37 1796 TLS_KEY="$DOMAIN_PATH/tls.valdation.key"
e22bcf7c 1797 fi
4c2a3841 1798 if [ -z "$TLS_CSR" ]; then
43822d37 1799 TLS_CSR="$DOMAIN_PATH/tls.valdation.csr"
e22bcf7c 1800 fi
4c2a3841 1801
4c3b3608 1802}
1803
610e0f21 1804_exec() {
4c2a3841 1805 if [ -z "$_EXEC_TEMP_ERR" ]; then
610e0f21 1806 _EXEC_TEMP_ERR="$(_mktemp)"
1807 fi
1808
4c2a3841 1809 if [ "$_EXEC_TEMP_ERR" ]; then
3e5b1024 1810 eval "$@ 2>>$_EXEC_TEMP_ERR"
610e0f21 1811 else
3e5b1024 1812 eval "$@"
610e0f21 1813 fi
1814}
1815
1816_exec_err() {
3e5b1024 1817 [ "$_EXEC_TEMP_ERR" ] && _err "$(cat "$_EXEC_TEMP_ERR")" && echo "" >"$_EXEC_TEMP_ERR"
610e0f21 1818}
4c3b3608 1819
1820_apachePath() {
c3dd3ef0 1821 _APACHECTL="apachectl"
4c2a3841 1822 if ! _exists apachectl; then
1823 if _exists apache2ctl; then
1824 _APACHECTL="apache2ctl"
e4a19585 1825 else
bc96082f 1826 _err "'apachectl not found. It seems that apache is not installed, or you are not root user.'"
e4a19585 1827 _err "Please use webroot mode to try again."
1828 return 1
1829 fi
80a0a7b5 1830 fi
4c2a3841 1831
1832 if ! _exec $_APACHECTL -V >/dev/null; then
610e0f21 1833 _exec_err
1834 return 1
1835 fi
4c2a3841 1836
1837 if [ "$APACHE_HTTPD_CONF" ]; then
5be1449d 1838 _saveaccountconf APACHE_HTTPD_CONF "$APACHE_HTTPD_CONF"
1839 httpdconf="$APACHE_HTTPD_CONF"
79a267ab 1840 httpdconfname="$(basename "$httpdconfname")"
d62ee940 1841 else
4c2a3841 1842 httpdconfname="$($_APACHECTL -V | grep SERVER_CONFIG_FILE= | cut -d = -f 2 | tr -d '"')"
5be1449d 1843 _debug httpdconfname "$httpdconfname"
4c2a3841 1844
1845 if [ -z "$httpdconfname" ]; then
5be1449d 1846 _err "Can not read apache config file."
1847 return 1
1848 fi
4c2a3841 1849
1850 if _startswith "$httpdconfname" '/'; then
5be1449d 1851 httpdconf="$httpdconfname"
79a267ab 1852 httpdconfname="$(basename "$httpdconfname")"
5be1449d 1853 else
4c2a3841 1854 httpdroot="$($_APACHECTL -V | grep HTTPD_ROOT= | cut -d = -f 2 | tr -d '"')"
5be1449d 1855 _debug httpdroot "$httpdroot"
1856 httpdconf="$httpdroot/$httpdconfname"
79a267ab 1857 httpdconfname="$(basename "$httpdconfname")"
5be1449d 1858 fi
d62ee940 1859 fi
78768e98 1860 _debug httpdconf "$httpdconf"
8f63baf7 1861 _debug httpdconfname "$httpdconfname"
4c2a3841 1862 if [ ! -f "$httpdconf" ]; then
78768e98 1863 _err "Apache Config file not found" "$httpdconf"
4c3b3608 1864 return 1
1865 fi
1866 return 0
1867}
1868
1869_restoreApache() {
4c2a3841 1870 if [ -z "$usingApache" ]; then
4c3b3608 1871 return 0
1872 fi
1873 _initpath
4c2a3841 1874 if ! _apachePath; then
4c3b3608 1875 return 1
1876 fi
4c2a3841 1877
1878 if [ ! -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname" ]; then
4c3b3608 1879 _debug "No config file to restore."
1880 return 0
1881 fi
4c2a3841 1882
1883 cat "$APACHE_CONF_BACKUP_DIR/$httpdconfname" >"$httpdconf"
5ef501c5 1884 _debug "Restored: $httpdconf."
4c2a3841 1885 if ! _exec $_APACHECTL -t; then
610e0f21 1886 _exec_err
4c3b3608 1887 _err "Sorry, restore apache config error, please contact me."
4c2a3841 1888 return 1
4c3b3608 1889 fi
5ef501c5 1890 _debug "Restored successfully."
4c3b3608 1891 rm -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname"
4c2a3841 1892 return 0
4c3b3608 1893}
1894
1895_setApache() {
1896 _initpath
4c2a3841 1897 if ! _apachePath; then
4c3b3608 1898 return 1
1899 fi
1900
5fc5016d 1901 #test the conf first
869578ce 1902 _info "Checking if there is an error in the apache config file before starting."
4c2a3841 1903
44edb2bd 1904 if ! _exec "$_APACHECTL" -t >/dev/null; then
610e0f21 1905 _exec_err
1906 _err "The apache config file has error, please fix it first, then try again."
869578ce 1907 _err "Don't worry, there is nothing changed to your system."
4c2a3841 1908 return 1
5fc5016d 1909 else
1910 _info "OK"
1911 fi
4c2a3841 1912
4c3b3608 1913 #backup the conf
5778811a 1914 _debug "Backup apache config file" "$httpdconf"
4c2a3841 1915 if ! cp "$httpdconf" "$APACHE_CONF_BACKUP_DIR/"; then
869578ce 1916 _err "Can not backup apache config file, so abort. Don't worry, the apache config is not changed."
8f63baf7 1917 _err "This might be a bug of $PROJECT_NAME , pleae report issue: $PROJECT"
1918 return 1
1919 fi
4c3b3608 1920 _info "JFYI, Config file $httpdconf is backuped to $APACHE_CONF_BACKUP_DIR/$httpdconfname"
1921 _info "In case there is an error that can not be restored automatically, you may try restore it yourself."
329174b6 1922 _info "The backup file will be deleted on success, just forget it."
4c2a3841 1923
4c3b3608 1924 #add alias
4c2a3841 1925
1926 apacheVer="$($_APACHECTL -V | grep "Server version:" | cut -d : -f 2 | cut -d " " -f 2 | cut -d '/' -f 2)"
b09d597c 1927 _debug "apacheVer" "$apacheVer"
1928 apacheMajer="$(echo "$apacheVer" | cut -d . -f 1)"
1929 apacheMinor="$(echo "$apacheVer" | cut -d . -f 2)"
1930
4c2a3841 1931 if [ "$apacheVer" ] && [ "$apacheMajer$apacheMinor" -ge "24" ]; then
b09d597c 1932 echo "
4c3b3608 1933Alias /.well-known/acme-challenge $ACME_DIR
1934
1935<Directory $ACME_DIR >
1936Require all granted
b09d597c 1937</Directory>
4c2a3841 1938 " >>"$httpdconf"
b09d597c 1939 else
1940 echo "
1941Alias /.well-known/acme-challenge $ACME_DIR
1942
1943<Directory $ACME_DIR >
1944Order allow,deny
1945Allow from all
4c3b3608 1946</Directory>
4c2a3841 1947 " >>"$httpdconf"
b09d597c 1948 fi
1949
4c2a3841 1950 _msg="$($_APACHECTL -t 2>&1)"
1951 if [ "$?" != "0" ]; then
5fc5016d 1952 _err "Sorry, apache config error"
4c2a3841 1953 if _restoreApache; then
869578ce 1954 _err "The apache config file is restored."
5fc5016d 1955 else
869578ce 1956 _err "Sorry, The apache config file can not be restored, please report bug."
5fc5016d 1957 fi
4c2a3841 1958 return 1
4c3b3608 1959 fi
4c2a3841 1960
1961 if [ ! -d "$ACME_DIR" ]; then
4c3b3608 1962 mkdir -p "$ACME_DIR"
1963 chmod 755 "$ACME_DIR"
1964 fi
4c2a3841 1965
44edb2bd 1966 if ! _exec "$_APACHECTL" graceful; then
4c2a3841 1967 _exec_err
610e0f21 1968 _err "$_APACHECTL graceful error, please contact me."
4c3b3608 1969 _restoreApache
4c2a3841 1970 return 1
4c3b3608 1971 fi
1972 usingApache="1"
1973 return 0
1974}
1975
5ef501c5 1976_clearup() {
44edb2bd 1977 _stopserver "$serverproc"
4c3b3608 1978 serverproc=""
1979 _restoreApache
800e3f45 1980 _clearupdns
4c2a3841 1981 if [ -z "$DEBUG" ]; then
e22bcf7c 1982 rm -f "$TLS_CONF"
1983 rm -f "$TLS_CERT"
1984 rm -f "$TLS_KEY"
1985 rm -f "$TLS_CSR"
1986 fi
4c3b3608 1987}
1988
800e3f45 1989_clearupdns() {
1990 _debug "_clearupdns"
4c2a3841 1991 if [ "$dnsadded" != 1 ] || [ -z "$vlist" ]; then
93fc48a2 1992 _debug "Dns not added, skip."
800e3f45 1993 return
1994 fi
1995
4c2a3841 1996 ventries=$(echo "$vlist" | tr ',' ' ')
1997 for ventry in $ventries; do
0c538f75 1998 d=$(echo "$ventry" | cut -d "$sep" -f 1)
1999 keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2)
2000 vtype=$(echo "$ventry" | cut -d "$sep" -f 4)
2001 _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5)
21f201e3 2002 txt="$(printf "%s" "$keyauthorization" | _digest "sha256" | _urlencode)"
2003 _debug txt "$txt"
4c2a3841 2004 if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
800e3f45 2005 _info "$d is already verified, skip $vtype."
2006 continue
2007 fi
2008
4c2a3841 2009 if [ "$vtype" != "$VTYPE_DNS" ]; then
800e3f45 2010 _info "Skip $d for $vtype"
2011 continue
2012 fi
4c2a3841 2013
0c538f75 2014 d_api="$(_findHook "$d" dnsapi "$_currentRoot")"
800e3f45 2015 _debug d_api "$d_api"
4c2a3841 2016
2017 if [ -z "$d_api" ]; then
800e3f45 2018 _info "Not Found domain api file: $d_api"
2019 continue
2020 fi
4c2a3841 2021
800e3f45 2022 (
d5ec5f80 2023 if ! . "$d_api"; then
800e3f45 2024 _err "Load file $d_api error. Please check your api file and try again."
2025 return 1
2026 fi
4c2a3841 2027
800e3f45 2028 rmcommand="${_currentRoot}_rm"
d5ec5f80 2029 if ! _exists "$rmcommand"; then
800e3f45 2030 _err "It seems that your api file doesn't define $rmcommand"
2031 return 1
2032 fi
4c2a3841 2033
800e3f45 2034 txtdomain="_acme-challenge.$d"
4c2a3841 2035
21f201e3 2036 if ! $rmcommand "$txtdomain" "$txt"; then
800e3f45 2037 _err "Error removing txt for domain:$txtdomain"
2038 return 1
2039 fi
2040 )
4c2a3841 2041
800e3f45 2042 done
2043}
2044
4c3b3608 2045# webroot removelevel tokenfile
2046_clearupwebbroot() {
2047 __webroot="$1"
4c2a3841 2048 if [ -z "$__webroot" ]; then
4c3b3608 2049 _debug "no webroot specified, skip"
2050 return 0
2051 fi
4c2a3841 2052
dcf9cb58 2053 _rmpath=""
4c2a3841 2054 if [ "$2" = '1' ]; then
dcf9cb58 2055 _rmpath="$__webroot/.well-known"
4c2a3841 2056 elif [ "$2" = '2' ]; then
dcf9cb58 2057 _rmpath="$__webroot/.well-known/acme-challenge"
4c2a3841 2058 elif [ "$2" = '3' ]; then
dcf9cb58 2059 _rmpath="$__webroot/.well-known/acme-challenge/$3"
4c3b3608 2060 else
cc179731 2061 _debug "Skip for removelevel:$2"
4c3b3608 2062 fi
4c2a3841 2063
2064 if [ "$_rmpath" ]; then
2065 if [ "$DEBUG" ]; then
dcf9cb58 2066 _debug "Debugging, skip removing: $_rmpath"
2067 else
2068 rm -rf "$_rmpath"
2069 fi
2070 fi
4c2a3841 2071
4c3b3608 2072 return 0
2073
2074}
2075
b0070f03 2076_on_before_issue() {
30c2d84c 2077 _debug _on_before_issue
4c2a3841 2078 if _hasfield "$Le_Webroot" "$NO_VALUE"; then
2079 if ! _exists "nc"; then
0463b5d6 2080 _err "Please install netcat(nc) tools first."
2081 return 1
2082 fi
0463b5d6 2083 fi
2084
2085 _debug Le_LocalAddress "$Le_LocalAddress"
4c2a3841 2086
2087 alldomains=$(echo "$Le_Domain,$Le_Alt" | tr ',' ' ')
0463b5d6 2088 _index=1
2089 _currentRoot=""
2090 _addrIndex=1
4c2a3841 2091 for d in $alldomains; do
d5ec5f80 2092 _debug "Check for domain" "$d"
0463b5d6 2093 _currentRoot="$(_getfield "$Le_Webroot" $_index)"
2094 _debug "_currentRoot" "$_currentRoot"
2095 _index=$(_math $_index + 1)
2096 _checkport=""
4c2a3841 2097 if [ "$_currentRoot" = "$NO_VALUE" ]; then
0463b5d6 2098 _info "Standalone mode."
4c2a3841 2099 if [ -z "$Le_HTTPPort" ]; then
0463b5d6 2100 Le_HTTPPort=80
2101 else
4c2a3841 2102 _savedomainconf "Le_HTTPPort" "$Le_HTTPPort"
0463b5d6 2103 fi
2104 _checkport="$Le_HTTPPort"
4c2a3841 2105 elif [ "$_currentRoot" = "$W_TLS" ]; then
0463b5d6 2106 _info "Standalone tls mode."
4c2a3841 2107 if [ -z "$Le_TLSPort" ]; then
0463b5d6 2108 Le_TLSPort=443
2109 else
4c2a3841 2110 _savedomainconf "Le_TLSPort" "$Le_TLSPort"
0463b5d6 2111 fi
2112 _checkport="$Le_TLSPort"
2113 fi
4c2a3841 2114
2115 if [ "$_checkport" ]; then
0463b5d6 2116 _debug _checkport "$_checkport"
2117 _checkaddr="$(_getfield "$Le_LocalAddress" $_addrIndex)"
2118 _debug _checkaddr "$_checkaddr"
4c2a3841 2119
0463b5d6 2120 _addrIndex="$(_math $_addrIndex + 1)"
4c2a3841 2121
0463b5d6 2122 _netprc="$(_ss "$_checkport" | grep "$_checkport")"
2123 netprc="$(echo "$_netprc" | grep "$_checkaddr")"
4c2a3841 2124 if [ -z "$netprc" ]; then
0463b5d6 2125 netprc="$(echo "$_netprc" | grep "$LOCAL_ANY_ADDRESS")"
2126 fi
4c2a3841 2127 if [ "$netprc" ]; then
0463b5d6 2128 _err "$netprc"
4c2a3841 2129 _err "tcp port $_checkport is already used by $(echo "$netprc" | cut -d : -f 4)"
0463b5d6 2130 _err "Please stop it first"
2131 return 1
2132 fi
2133 fi
2134 done
2135
4c2a3841 2136 if _hasfield "$Le_Webroot" "apache"; then
2137 if ! _setApache; then
0463b5d6 2138 _err "set up apache error. Report error to me."
2139 return 1
2140 fi
2141 else
2142 usingApache=""
2143 fi
2144
b0070f03 2145 #run pre hook
4c2a3841 2146 if [ "$Le_PreHook" ]; then
b0070f03 2147 _info "Run pre hook:'$Le_PreHook'"
2148 if ! (
2149 cd "$DOMAIN_PATH" && eval "$Le_PreHook"
4c2a3841 2150 ); then
b0070f03 2151 _err "Error when run pre hook."
2152 return 1
2153 fi
2154 fi
2155}
2156
2157_on_issue_err() {
30c2d84c 2158 _debug _on_issue_err
4c2a3841 2159 if [ "$LOG_FILE" ]; then
a73c5b33 2160 _err "Please check log file for more details: $LOG_FILE"
2161 else
54ae008d 2162 _err "Please add '--debug' or '--log' to check more details."
a73c5b33 2163 _err "See: $_DEBUG_WIKI"
2164 fi
4c2a3841 2165
2166 if [ "$DEBUG" ] && [ "$DEBUG" -gt "0" ]; then
9d548d81 2167 _debug "$(_dlg_versions)"
2168 fi
4c2a3841 2169
b0070f03 2170 #run the post hook
4c2a3841 2171 if [ "$Le_PostHook" ]; then
b0070f03 2172 _info "Run post hook:'$Le_PostHook'"
2173 if ! (
2174 cd "$DOMAIN_PATH" && eval "$Le_PostHook"
4c2a3841 2175 ); then
b0070f03 2176 _err "Error when run post hook."
2177 return 1
2178 fi
2179 fi
2180}
2181
2182_on_issue_success() {
30c2d84c 2183 _debug _on_issue_success
b0070f03 2184 #run the post hook
4c2a3841 2185 if [ "$Le_PostHook" ]; then
b0070f03 2186 _info "Run post hook:'$Le_PostHook'"
2187 if ! (
2188 cd "$DOMAIN_PATH" && eval "$Le_PostHook"
4c2a3841 2189 ); then
b0070f03 2190 _err "Error when run post hook."
2191 return 1
2192 fi
2193 fi
4c2a3841 2194
b0070f03 2195 #run renew hook
4c2a3841 2196 if [ "$IS_RENEW" ] && [ "$Le_RenewHook" ]; then
b0070f03 2197 _info "Run renew hook:'$Le_RenewHook'"
2198 if ! (
2199 cd "$DOMAIN_PATH" && eval "$Le_RenewHook"
4c2a3841 2200 ); then
b0070f03 2201 _err "Error when run renew hook."
2202 return 1
2203 fi
4c2a3841 2204 fi
2205
b0070f03 2206}
2207
eb59817e 2208updateaccount() {
2209 _initpath
2210 _regAccount
2211}
b0070f03 2212
eb59817e 2213registeraccount() {
57e58ce7 2214 _reg_length="$1"
eb59817e 2215 _initpath
57e58ce7 2216 _regAccount "$_reg_length"
eb59817e 2217}
d404e92d 2218
8a29fbc8 2219__calcAccountKeyHash() {
ca7202eb 2220 [ -f "$ACCOUNT_KEY_PATH" ] && _digest sha256 <"$ACCOUNT_KEY_PATH"
8a29fbc8 2221}
2222
57e58ce7 2223#keylength
d404e92d 2224_regAccount() {
2225 _initpath
57e58ce7 2226 _reg_length="$1"
4c2a3841 2227
5c48e139 2228 if [ ! -f "$ACCOUNT_KEY_PATH" ] && [ -f "$_OLD_ACCOUNT_KEY" ]; then
08b6cf02 2229 mkdir -p "$CA_DIR"
5c48e139 2230 _info "mv $_OLD_ACCOUNT_KEY to $ACCOUNT_KEY_PATH"
2231 mv "$_OLD_ACCOUNT_KEY" "$ACCOUNT_KEY_PATH"
2232 fi
4c2a3841 2233
5c48e139 2234 if [ ! -f "$ACCOUNT_JSON_PATH" ] && [ -f "$_OLD_ACCOUNT_JSON" ]; then
08b6cf02 2235 mkdir -p "$CA_DIR"
5c48e139 2236 _info "mv $_OLD_ACCOUNT_JSON to $ACCOUNT_JSON_PATH"
2237 mv "$_OLD_ACCOUNT_JSON" "$ACCOUNT_JSON_PATH"
2238 fi
4c2a3841 2239
2240 if [ ! -f "$ACCOUNT_KEY_PATH" ]; then
2241 if ! _create_account_key "$_reg_length"; then
d404e92d 2242 _err "Create account key error."
2243 return 1
2244 fi
2245 fi
4c2a3841 2246
2247 if ! _calcjwk "$ACCOUNT_KEY_PATH"; then
d404e92d 2248 return 1
2249 fi
2250
2251 _updateTos=""
2252 _reg_res="new-reg"
4c2a3841 2253 while true; do
d404e92d 2254 _debug AGREEMENT "$AGREEMENT"
4c2a3841 2255
d404e92d 2256 regjson='{"resource": "'$_reg_res'", "agreement": "'$AGREEMENT'"}'
2257
4c2a3841 2258 if [ "$ACCOUNT_EMAIL" ]; then
d404e92d 2259 regjson='{"resource": "'$_reg_res'", "contact": ["mailto: '$ACCOUNT_EMAIL'"], "agreement": "'$AGREEMENT'"}'
2260 fi
2261
4c2a3841 2262 if [ -z "$_updateTos" ]; then
d404e92d 2263 _info "Registering account"
2264
4c2a3841 2265 if ! _send_signed_request "$API/acme/new-reg" "$regjson"; then
d404e92d 2266 _err "Register account Error: $response"
2267 return 1
2268 fi
2269
4c2a3841 2270 if [ "$code" = "" ] || [ "$code" = '201' ]; then
ca7202eb 2271 echo "$response" >"$ACCOUNT_JSON_PATH"
d404e92d 2272 _info "Registered"
4c2a3841 2273 elif [ "$code" = '409' ]; then
d404e92d 2274 _info "Already registered"
2275 else
2276 _err "Register account Error: $response"
2277 return 1
2278 fi
2279
4c2a3841 2280 _accUri="$(echo "$responseHeaders" | grep "^Location:" | _head_n 1 | cut -d ' ' -f 2 | tr -d "\r\n")"
d404e92d 2281 _debug "_accUri" "$_accUri"
d404e92d 2282
c2c8f320 2283 _tos="$(echo "$responseHeaders" | grep "^Link:.*rel=\"terms-of-service\"" | _head_n 1 | _egrep_o "<.*>" | tr -d '<>')"
d404e92d 2284 _debug "_tos" "$_tos"
4c2a3841 2285 if [ -z "$_tos" ]; then
d404e92d 2286 _debug "Use default tos: $DEFAULT_AGREEMENT"
2287 _tos="$DEFAULT_AGREEMENT"
2288 fi
2289 if [ "$_tos" != "$AGREEMENT" ]; then
2290 _updateTos=1
2291 AGREEMENT="$_tos"
2292 _reg_res="reg"
2293 continue
2294 fi
4c2a3841 2295
d404e92d 2296 else
2297 _debug "Update tos: $_tos"
4c2a3841 2298 if ! _send_signed_request "$_accUri" "$regjson"; then
d404e92d 2299 _err "Update tos error."
2300 return 1
2301 fi
4c2a3841 2302 if [ "$code" = '202' ]; then
eb59817e 2303 _info "Update success."
4c2a3841 2304
8a29fbc8 2305 CA_KEY_HASH="$(__calcAccountKeyHash)"
2306 _debug "Calc CA_KEY_HASH" "$CA_KEY_HASH"
2307 _savecaconf CA_KEY_HASH "$CA_KEY_HASH"
d404e92d 2308 else
800e3f45 2309 _err "Update account error."
d404e92d 2310 return 1
2311 fi
2312 fi
2313 return 0
2314 done
2315
2316}
2317
a61fe418 2318# domain folder file
2319_findHook() {
2320 _hookdomain="$1"
2321 _hookcat="$2"
2322 _hookname="$3"
2323
c7b16249 2324 if [ -f "$_SCRIPT_HOME/$_hookcat/$_hookname" ]; then
2325 d_api="$_SCRIPT_HOME/$_hookcat/$_hookname"
2326 elif [ -f "$_SCRIPT_HOME/$_hookcat/$_hookname.sh" ]; then
2327 d_api="$_SCRIPT_HOME/$_hookcat/$_hookname.sh"
4c2a3841 2328 elif [ -f "$LE_WORKING_DIR/$_hookdomain/$_hookname" ]; then
a61fe418 2329 d_api="$LE_WORKING_DIR/$_hookdomain/$_hookname"
4c2a3841 2330 elif [ -f "$LE_WORKING_DIR/$_hookdomain/$_hookname.sh" ]; then
a61fe418 2331 d_api="$LE_WORKING_DIR/$_hookdomain/$_hookname.sh"
4c2a3841 2332 elif [ -f "$LE_WORKING_DIR/$_hookname" ]; then
a61fe418 2333 d_api="$LE_WORKING_DIR/$_hookname"
4c2a3841 2334 elif [ -f "$LE_WORKING_DIR/$_hookname.sh" ]; then
a61fe418 2335 d_api="$LE_WORKING_DIR/$_hookname.sh"
4c2a3841 2336 elif [ -f "$LE_WORKING_DIR/$_hookcat/$_hookname" ]; then
a61fe418 2337 d_api="$LE_WORKING_DIR/$_hookcat/$_hookname"
4c2a3841 2338 elif [ -f "$LE_WORKING_DIR/$_hookcat/$_hookname.sh" ]; then
a61fe418 2339 d_api="$LE_WORKING_DIR/$_hookcat/$_hookname.sh"
2340 fi
2341
2342 printf "%s" "$d_api"
2343}
2344
f940b2a5 2345#domain
2346__get_domain_new_authz() {
2347 _gdnd="$1"
2348 _info "Getting new-authz for domain" "$_gdnd"
4c2a3841 2349
f940b2a5 2350 _Max_new_authz_retry_times=5
2351 _authz_i=0
4c2a3841 2352 while [ "$_authz_i" -lt "$_Max_new_authz_retry_times" ]; do
efd96153 2353 _debug "Try new-authz for the $_authz_i time."
4c2a3841 2354 if ! _send_signed_request "$API/acme/new-authz" "{\"resource\": \"new-authz\", \"identifier\": {\"type\": \"dns\", \"value\": \"$(_idn "$_gdnd")\"}}"; then
f940b2a5 2355 _err "Can not get domain new authz."
2356 return 1
2357 fi
4c2a3841 2358 if ! _contains "$response" "An error occurred while processing your request"; then
f940b2a5 2359 _info "The new-authz request is ok."
2360 break
2361 fi
2362 _authz_i="$(_math "$_authz_i" + 1)"
9e45ac93 2363 _info "The server is busy, Sleep $_authz_i to retry."
f940b2a5 2364 _sleep "$_authz_i"
4c2a3841 2365 done
f940b2a5 2366
4c2a3841 2367 if [ "$_authz_i" = "$_Max_new_authz_retry_times" ]; then
efd96153 2368 _err "new-authz retry reach the max $_Max_new_authz_retry_times times."
f940b2a5 2369 fi
4c2a3841 2370
2371 if [ ! -z "$code" ] && [ ! "$code" = '201' ]; then
f940b2a5 2372 _err "new-authz error: $response"
2373 return 1
2374 fi
2375
2376}
2377
10afcaca 2378#webroot, domain domainlist keylength
4c3b3608 2379issue() {
4c2a3841 2380 if [ -z "$2" ]; then
43822d37 2381 _usage "Usage: $PROJECT_ENTRY --issue -d a.com -w /path/to/webroot/a.com/ "
4c3b3608 2382 return 1
2383 fi
2384 Le_Webroot="$1"
2385 Le_Domain="$2"
2386 Le_Alt="$3"
2387 Le_Keylength="$4"
2388 Le_RealCertPath="$5"
2389 Le_RealKeyPath="$6"
2390 Le_RealCACertPath="$7"
2391 Le_ReloadCmd="$8"
a63b05a9 2392 Le_RealFullChainPath="$9"
b0070f03 2393 Le_PreHook="${10}"
2394 Le_PostHook="${11}"
2395 Le_RenewHook="${12}"
0463b5d6 2396 Le_LocalAddress="${13}"
4c2a3841 2397
eccec5f6 2398 #remove these later.
4c2a3841 2399 if [ "$Le_Webroot" = "dns-cf" ]; then
eccec5f6 2400 Le_Webroot="dns_cf"
2401 fi
4c2a3841 2402 if [ "$Le_Webroot" = "dns-dp" ]; then
eccec5f6 2403 Le_Webroot="dns_dp"
2404 fi
4c2a3841 2405 if [ "$Le_Webroot" = "dns-cx" ]; then
eccec5f6 2406 Le_Webroot="dns_cx"
2407 fi
950172dc 2408 _debug "Using api: $API"
4c2a3841 2409
2410 if [ ! "$IS_RENEW" ]; then
ca7202eb 2411 _initpath "$Le_Domain" "$Le_Keylength"
43822d37 2412 mkdir -p "$DOMAIN_PATH"
2413 fi
eccec5f6 2414
4c2a3841 2415 if [ -f "$DOMAIN_CONF" ]; then
61623d22 2416 Le_NextRenewTime=$(_readdomainconf Le_NextRenewTime)
a4270efa 2417 _debug Le_NextRenewTime "$Le_NextRenewTime"
95e06de5 2418 if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(_time)" -lt "$Le_NextRenewTime" ]; then
bb25febd 2419 _saved_domain=$(_readdomainconf Le_Domain)
2420 _debug _saved_domain "$_saved_domain"
2421 _saved_alt=$(_readdomainconf Le_Alt)
2422 _debug _saved_alt "$_saved_alt"
4c2a3841 2423 if [ "$_saved_domain,$_saved_alt" = "$Le_Domain,$Le_Alt" ]; then
bb25febd 2424 _info "Domains not changed."
2425 _info "Skip, Next renewal time is: $(__green "$(_readdomainconf Le_NextRenewTimeStr)")"
4c2a3841 2426 _info "Add '$(__red '--force')' to force to renew."
bb25febd 2427 return $RENEW_SKIP
2428 else
2429 _info "Domains have changed."
2430 fi
4c3b3608 2431 fi
2432 fi
96a46cfc 2433
4c2a3841 2434 _savedomainconf "Le_Domain" "$Le_Domain"
2435 _savedomainconf "Le_Alt" "$Le_Alt"
2436 _savedomainconf "Le_Webroot" "$Le_Webroot"
2437
2438 _savedomainconf "Le_PreHook" "$Le_PreHook"
2439 _savedomainconf "Le_PostHook" "$Le_PostHook"
2440 _savedomainconf "Le_RenewHook" "$Le_RenewHook"
2441
2442 if [ "$Le_LocalAddress" ]; then
2443 _savedomainconf "Le_LocalAddress" "$Le_LocalAddress"
72518d48 2444 else
2445 _cleardomainconf "Le_LocalAddress"
2446 fi
6ae0f7f5 2447
f6dcd989 2448 Le_API="$API"
2449 _savedomainconf "Le_API" "$Le_API"
4c2a3841 2450
2451 if [ "$Le_Alt" = "$NO_VALUE" ]; then
4c3b3608 2452 Le_Alt=""
2453 fi
4c2a3841 2454
2455 if [ "$Le_Keylength" = "$NO_VALUE" ]; then
d404e92d 2456 Le_Keylength=""
2457 fi
4c2a3841 2458
2459 if ! _on_before_issue; then
0463b5d6 2460 _err "_on_before_issue."
2461 return 1
4c3b3608 2462 fi
0463b5d6 2463
8a29fbc8 2464 _saved_account_key_hash="$(_readcaconf "CA_KEY_HASH")"
2465 _debug2 _saved_account_key_hash "$_saved_account_key_hash"
4c2a3841 2466
2467 if [ -z "$_saved_account_key_hash" ] || [ "$_saved_account_key_hash" != "$(__calcAccountKeyHash)" ]; then
57e58ce7 2468 if ! _regAccount "$_accountkeylength"; then
8a29fbc8 2469 _on_issue_err
2470 return 1
2471 fi
57e58ce7 2472 else
2473 _debug "_saved_account_key_hash is not changed, skip register account."
166096dc 2474 fi
166096dc 2475
4c2a3841 2476 if [ -f "$CSR_PATH" ] && [ ! -f "$CERT_KEY_PATH" ]; then
10afcaca 2477 _info "Signing from existing CSR."
2478 else
2479 _key=$(_readdomainconf Le_Keylength)
2480 _debug "Read key length:$_key"
4c2a3841 2481 if [ ! -f "$CERT_KEY_PATH" ] || [ "$Le_Keylength" != "$_key" ]; then
ca7202eb 2482 if ! createDomainKey "$Le_Domain" "$Le_Keylength"; then
10afcaca 2483 _err "Create domain key error."
2484 _clearup
b0070f03 2485 _on_issue_err
10afcaca 2486 return 1
2487 fi
2488 fi
2489
4c2a3841 2490 if ! _createcsr "$Le_Domain" "$Le_Alt" "$CERT_KEY_PATH" "$CSR_PATH" "$DOMAIN_SSL_CONF"; then
10afcaca 2491 _err "Create CSR error."
5ef501c5 2492 _clearup
b0070f03 2493 _on_issue_err
41e3eafa 2494 return 1
2495 fi
4c3b3608 2496 fi
10afcaca 2497
4c2a3841 2498 _savedomainconf "Le_Keylength" "$Le_Keylength"
2499
4c3b3608 2500 vlist="$Le_Vlist"
cae203be 2501
2502 _info "Getting domain auth token for each domain"
4c3b3608 2503 sep='#'
4c2a3841 2504 if [ -z "$vlist" ]; then
2505 alldomains=$(echo "$Le_Domain,$Le_Alt" | tr ',' ' ')
a63b05a9 2506 _index=1
2507 _currentRoot=""
4c2a3841 2508 for d in $alldomains; do
ca7202eb 2509 _info "Getting webroot for domain" "$d"
a63b05a9 2510 _w="$(echo $Le_Webroot | cut -d , -f $_index)"
0463b5d6 2511 _info _w "$_w"
4c2a3841 2512 if [ "$_w" ]; then
a63b05a9 2513 _currentRoot="$_w"
2514 fi
2515 _debug "_currentRoot" "$_currentRoot"
00a50605 2516 _index=$(_math $_index + 1)
4c2a3841 2517
a63b05a9 2518 vtype="$VTYPE_HTTP"
4c2a3841 2519 if _startswith "$_currentRoot" "dns"; then
a63b05a9 2520 vtype="$VTYPE_DNS"
2521 fi
4c2a3841 2522
2523 if [ "$_currentRoot" = "$W_TLS" ]; then
e22bcf7c 2524 vtype="$VTYPE_TLS"
2525 fi
c4d8fd83 2526
4c2a3841 2527 if ! __get_domain_new_authz "$d"; then
c4d8fd83 2528 _clearup
b0070f03 2529 _on_issue_err
c4d8fd83 2530 return 1
2531 fi
2532
4c2a3841 2533 if [ -z "$thumbprint" ]; then
2534 accountkey_json=$(printf "%s" "$jwk" | tr -d ' ')
ae2db62f 2535 thumbprint=$(printf "%s" "$accountkey_json" | _digest "sha256" | _urlencode)
4c3b3608 2536 fi
2537
4c2a3841 2538 entry="$(printf "%s\n" "$response" | _egrep_o '[^\{]*"type":"'$vtype'"[^\}]*')"
4c3b3608 2539 _debug entry "$entry"
4c2a3841 2540 if [ -z "$entry" ]; then
19539575 2541 _err "Error, can not get domain token $d"
2542 _clearup
b0070f03 2543 _on_issue_err
19539575 2544 return 1
2545 fi
22ea4004 2546 token="$(printf "%s\n" "$entry" | _egrep_o '"token":"[^"]*' | cut -d : -f 2 | tr -d '"')"
ca7202eb 2547 _debug token "$token"
4c2a3841 2548
2549 uri="$(printf "%s\n" "$entry" | _egrep_o '"uri":"[^"]*' | cut -d : -f 2,3 | tr -d '"')"
ca7202eb 2550 _debug uri "$uri"
cae203be 2551
4c3b3608 2552 keyauthorization="$token.$thumbprint"
2553 _debug keyauthorization "$keyauthorization"
2554
95e06de5 2555 if printf "%s" "$response" | grep '"status":"valid"' >/dev/null 2>&1; then
d35bf517 2556 _info "$d is already verified, skip."
ca7202eb 2557 keyauthorization="$STATE_VERIFIED"
d35bf517 2558 _debug keyauthorization "$keyauthorization"
ec603bee 2559 fi
2560
a63b05a9 2561 dvlist="$d$sep$keyauthorization$sep$uri$sep$vtype$sep$_currentRoot"
4c3b3608 2562 _debug dvlist "$dvlist"
4c2a3841 2563
4c3b3608 2564 vlist="$vlist$dvlist,"
2565
2566 done
2567
2568 #add entry
2569 dnsadded=""
4c2a3841 2570 ventries=$(echo "$vlist" | tr ',' ' ')
2571 for ventry in $ventries; do
ca7202eb 2572 d=$(echo "$ventry" | cut -d "$sep" -f 1)
2573 keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2)
2574 vtype=$(echo "$ventry" | cut -d "$sep" -f 4)
2575 _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5)
ec603bee 2576
4c2a3841 2577 if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
ec603bee 2578 _info "$d is already verified, skip $vtype."
2579 continue
2580 fi
2581
4c2a3841 2582 if [ "$vtype" = "$VTYPE_DNS" ]; then
4c3b3608 2583 dnsadded='0'
2584 txtdomain="_acme-challenge.$d"
2585 _debug txtdomain "$txtdomain"
22ea4004 2586 txt="$(printf "%s" "$keyauthorization" | _digest "sha256" | _urlencode)"
4c3b3608 2587 _debug txt "$txt"
a61fe418 2588
0c538f75 2589 d_api="$(_findHook "$d" dnsapi "$_currentRoot")"
a61fe418 2590
4c3b3608 2591 _debug d_api "$d_api"
4c2a3841 2592
2593 if [ "$d_api" ]; then
4c3b3608 2594 _info "Found domain api file: $d_api"
2595 else
2596 _err "Add the following TXT record:"
0c538f75 2597 _err "Domain: '$(__green "$txtdomain")'"
2598 _err "TXT value: '$(__green "$txt")'"
4c3b3608 2599 _err "Please be aware that you prepend _acme-challenge. before your domain"
2600 _err "so the resulting subdomain will be: $txtdomain"
2601 continue
2602 fi
4c2a3841 2603
73b8b120 2604 (
ca7202eb 2605 if ! . "$d_api"; then
73b8b120 2606 _err "Load file $d_api error. Please check your api file and try again."
2607 return 1
2608 fi
4c2a3841 2609
158f22f7 2610 addcommand="${_currentRoot}_add"
ca7202eb 2611 if ! _exists "$addcommand"; then
73b8b120 2612 _err "It seems that your api file is not correct, it must have a function named: $addcommand"
2613 return 1
2614 fi
4c2a3841 2615
ca7202eb 2616 if ! $addcommand "$txtdomain" "$txt"; then
73b8b120 2617 _err "Error add txt for domain:$txtdomain"
2618 return 1
2619 fi
2620 )
4c2a3841 2621
2622 if [ "$?" != "0" ]; then
5ef501c5 2623 _clearup
b0070f03 2624 _on_issue_err
4c3b3608 2625 return 1
2626 fi
2627 dnsadded='1'
2628 fi
2629 done
2630
4c2a3841 2631 if [ "$dnsadded" = '0' ]; then
2632 _savedomainconf "Le_Vlist" "$vlist"
4c3b3608 2633 _debug "Dns record not added yet, so, save to $DOMAIN_CONF and exit."
2634 _err "Please add the TXT records to the domains, and retry again."
5ef501c5 2635 _clearup
b0070f03 2636 _on_issue_err
4c3b3608 2637 return 1
2638 fi
4c2a3841 2639
4c3b3608 2640 fi
4c2a3841 2641
2642 if [ "$dnsadded" = '1' ]; then
2643 if [ -z "$Le_DNSSleep" ]; then
ca7202eb 2644 Le_DNSSleep="$DEFAULT_DNS_SLEEP"
0e38c60d 2645 else
4c2a3841 2646 _savedomainconf "Le_DNSSleep" "$Le_DNSSleep"
0e38c60d 2647 fi
2648
5fbc47eb 2649 _info "Sleep $(__green $Le_DNSSleep) seconds for the txt records to take effect"
ca7202eb 2650 _sleep "$Le_DNSSleep"
4c3b3608 2651 fi
4c2a3841 2652
4c3b3608 2653 _debug "ok, let's start to verify"
a63b05a9 2654
0463b5d6 2655 _ncIndex=1
4c2a3841 2656 ventries=$(echo "$vlist" | tr ',' ' ')
2657 for ventry in $ventries; do
ca7202eb 2658 d=$(echo "$ventry" | cut -d "$sep" -f 1)
2659 keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2)
2660 uri=$(echo "$ventry" | cut -d "$sep" -f 3)
2661 vtype=$(echo "$ventry" | cut -d "$sep" -f 4)
2662 _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5)
ec603bee 2663
4c2a3841 2664 if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
ec603bee 2665 _info "$d is already verified, skip $vtype."
2666 continue
2667 fi
2668
4c3b3608 2669 _info "Verifying:$d"
2670 _debug "d" "$d"
2671 _debug "keyauthorization" "$keyauthorization"
2672 _debug "uri" "$uri"
2673 removelevel=""
e22bcf7c 2674 token="$(printf "%s" "$keyauthorization" | cut -d '.' -f 1)"
a63b05a9 2675
2676 _debug "_currentRoot" "$_currentRoot"
2677
4c2a3841 2678 if [ "$vtype" = "$VTYPE_HTTP" ]; then
2679 if [ "$_currentRoot" = "$NO_VALUE" ]; then
4c3b3608 2680 _info "Standalone mode server"
4c2a3841 2681 _ncaddr="$(_getfield "$Le_LocalAddress" "$_ncIndex")"
0463b5d6 2682 _ncIndex="$(_math $_ncIndex + 1)"
2683 _startserver "$keyauthorization" "$_ncaddr" &
4c2a3841 2684 if [ "$?" != "0" ]; then
5ef501c5 2685 _clearup
b0070f03 2686 _on_issue_err
6fc1447f 2687 return 1
2688 fi
4c3b3608 2689 serverproc="$!"
5dbf664a 2690 sleep 1
ca7202eb 2691 _debug serverproc "$serverproc"
6fc1447f 2692
4c3b3608 2693 else
4c2a3841 2694 if [ "$_currentRoot" = "apache" ]; then
6f930641 2695 wellknown_path="$ACME_DIR"
2696 else
a63b05a9 2697 wellknown_path="$_currentRoot/.well-known/acme-challenge"
4c2a3841 2698 if [ ! -d "$_currentRoot/.well-known" ]; then
6f930641 2699 removelevel='1'
4c2a3841 2700 elif [ ! -d "$_currentRoot/.well-known/acme-challenge" ]; then
6f930641 2701 removelevel='2'
2702 else
2703 removelevel='3'
2704 fi
4c3b3608 2705 fi
6f930641 2706
4c3b3608 2707 _debug wellknown_path "$wellknown_path"
6f930641 2708
4c3b3608 2709 _debug "writing token:$token to $wellknown_path/$token"
2710
2711 mkdir -p "$wellknown_path"
93fc48a2 2712
4c2a3841 2713 if ! printf "%s" "$keyauthorization" >"$wellknown_path/$token"; then
93fc48a2 2714 _err "$d:Can not write token to file : $wellknown_path/$token"
2715 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
2716 _clearup
2717 _on_issue_err
2718 return 1
2719 fi
2720
4c2a3841 2721 if [ ! "$usingApache" ]; then
44edb2bd 2722 if webroot_owner=$(_stat "$_currentRoot"); then
32fdc196 2723 _debug "Changing owner/group of .well-known to $webroot_owner"
ca7202eb 2724 chown -R "$webroot_owner" "$_currentRoot/.well-known"
32fdc196 2725 else
4c2a3841 2726 _debug "not chaning owner/group of webroot"
32fdc196 2727 fi
df886ffa 2728 fi
4c2a3841 2729
4c3b3608 2730 fi
4c2a3841 2731
2732 elif [ "$vtype" = "$VTYPE_TLS" ]; then
e22bcf7c 2733 #create A
2734 #_hash_A="$(printf "%s" $token | _digest "sha256" "hex" )"
2735 #_debug2 _hash_A "$_hash_A"
2736 #_x="$(echo $_hash_A | cut -c 1-32)"
2737 #_debug2 _x "$_x"
2738 #_y="$(echo $_hash_A | cut -c 33-64)"
2739 #_debug2 _y "$_y"
2740 #_SAN_A="$_x.$_y.token.acme.invalid"
2741 #_debug2 _SAN_A "$_SAN_A"
4c2a3841 2742
e22bcf7c 2743 #create B
0c538f75 2744 _hash_B="$(printf "%s" "$keyauthorization" | _digest "sha256" "hex")"
e22bcf7c 2745 _debug2 _hash_B "$_hash_B"
0c538f75 2746 _x="$(echo "$_hash_B" | cut -c 1-32)"
e22bcf7c 2747 _debug2 _x "$_x"
0c538f75 2748 _y="$(echo "$_hash_B" | cut -c 33-64)"
e22bcf7c 2749 _debug2 _y "$_y"
4c2a3841 2750
e22bcf7c 2751 #_SAN_B="$_x.$_y.ka.acme.invalid"
4c2a3841 2752
e22bcf7c 2753 _SAN_B="$_x.$_y.acme.invalid"
2754 _debug2 _SAN_B "$_SAN_B"
4c2a3841 2755
2756 _ncaddr="$(_getfield "$Le_LocalAddress" "$_ncIndex")"
0c538f75 2757 _ncIndex="$(_math "$_ncIndex" + 1)"
0463b5d6 2758 if ! _starttlsserver "$_SAN_B" "$_SAN_A" "$Le_TLSPort" "$keyauthorization" "$_ncaddr"; then
e22bcf7c 2759 _err "Start tls server error."
2760 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
2761 _clearup
b0070f03 2762 _on_issue_err
e22bcf7c 2763 return 1
2764 fi
4c3b3608 2765 fi
4c2a3841 2766
ca7202eb 2767 if ! _send_signed_request "$uri" "{\"resource\": \"challenge\", \"keyAuthorization\": \"$keyauthorization\"}"; then
c4d8fd83 2768 _err "$d:Can not get challenge: $response"
2769 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
2770 _clearup
b0070f03 2771 _on_issue_err
c4d8fd83 2772 return 1
2773 fi
4c2a3841 2774
2775 if [ ! -z "$code" ] && [ ! "$code" = '202' ]; then
c60883ef 2776 _err "$d:Challenge error: $response"
a63b05a9 2777 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4c3b3608 2778 _clearup
b0070f03 2779 _on_issue_err
4c3b3608 2780 return 1
2781 fi
4c2a3841 2782
6fc1447f 2783 waittimes=0
4c2a3841 2784 if [ -z "$MAX_RETRY_TIMES" ]; then
6fc1447f 2785 MAX_RETRY_TIMES=30
2786 fi
4c2a3841 2787
2788 while true; do
0c538f75 2789 waittimes=$(_math "$waittimes" + 1)
4c2a3841 2790 if [ "$waittimes" -ge "$MAX_RETRY_TIMES" ]; then
6fc1447f 2791 _err "$d:Timeout"
2792 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
2793 _clearup
b0070f03 2794 _on_issue_err
6fc1447f 2795 return 1
2796 fi
4c2a3841 2797
5dbf664a 2798 _debug "sleep 2 secs to verify"
2799 sleep 2
4c3b3608 2800 _debug "checking"
44edb2bd 2801 response="$(_get "$uri")"
4c2a3841 2802 if [ "$?" != "0" ]; then
c60883ef 2803 _err "$d:Verify error:$response"
a63b05a9 2804 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4c3b3608 2805 _clearup
b0070f03 2806 _on_issue_err
4c3b3608 2807 return 1
2808 fi
9aaf36cd 2809 _debug2 original "$response"
4c2a3841 2810
2811 response="$(echo "$response" | _normalizeJson)"
7012b91f 2812 _debug2 response "$response"
4c2a3841 2813
2814 status=$(echo "$response" | _egrep_o '"status":"[^"]*' | cut -d : -f 2 | tr -d '"')
2815 if [ "$status" = "valid" ]; then
93f3098a 2816 _info "$(__green Success)"
ca7202eb 2817 _stopserver "$serverproc"
4c3b3608 2818 serverproc=""
a63b05a9 2819 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4c2a3841 2820 break
4c3b3608 2821 fi
4c2a3841 2822
2823 if [ "$status" = "invalid" ]; then
2824 error="$(echo "$response" | tr -d "\r\n" | _egrep_o '"error":\{[^\}]*')"
2825 _debug2 error "$error"
2826 errordetail="$(echo "$error" | _egrep_o '"detail": *"[^"]*' | cut -d '"' -f 4)"
2827 _debug2 errordetail "$errordetail"
2828 if [ "$errordetail" ]; then
2829 _err "$d:Verify error:$errordetail"
2830 else
2831 _err "$d:Verify error:$error"
2832 fi
2833 if [ "$DEBUG" ]; then
2834 if [ "$vtype" = "$VTYPE_HTTP" ]; then
2835 _debug "Debug: get token url."
2836 _get "http://$d/.well-known/acme-challenge/$token" "" 1
2837 fi
2838 fi
a63b05a9 2839 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4c3b3608 2840 _clearup
b0070f03 2841 _on_issue_err
4c2a3841 2842 return 1
4c3b3608 2843 fi
4c2a3841 2844
2845 if [ "$status" = "pending" ]; then
4c3b3608 2846 _info "Pending"
2847 else
4c2a3841 2848 _err "$d:Verify error:$response"
a63b05a9 2849 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4c3b3608 2850 _clearup
b0070f03 2851 _on_issue_err
4c3b3608 2852 return 1
2853 fi
4c2a3841 2854
4c3b3608 2855 done
4c2a3841 2856
4c3b3608 2857 done
2858
2859 _clearup
2860 _info "Verify finished, start to sign."
fa8311dc 2861 der="$(_getfile "${CSR_PATH}" "${BEGIN_CSR}" "${END_CSR}" | tr -d "\r\n" | _urlencode)"
4c2a3841 2862
2863 if ! _send_signed_request "$API/acme/new-cert" "{\"resource\": \"new-cert\", \"csr\": \"$der\"}" "needbase64"; then
c4d8fd83 2864 _err "Sign failed."
b0070f03 2865 _on_issue_err
c4d8fd83 2866 return 1
2867 fi
4c2a3841 2868
d404e92d 2869 _rcert="$response"
0c538f75 2870 Le_LinkCert="$(grep -i '^Location.*$' "$HTTP_HEADER" | _head_n 1 | tr -d "\r\n" | cut -d " " -f 2)"
4c2a3841 2871 _savedomainconf "Le_LinkCert" "$Le_LinkCert"
4c3b3608 2872
4c2a3841 2873 if [ "$Le_LinkCert" ]; then
2874 echo "$BEGIN_CERT" >"$CERT_PATH"
4c3b3608 2875
72518d48 2876 #if ! _get "$Le_LinkCert" | _base64 "multiline" >> "$CERT_PATH" ; then
2877 # _debug "Get cert failed. Let's try last response."
2878 # printf -- "%s" "$_rcert" | _dbase64 "multiline" | _base64 "multiline" >> "$CERT_PATH"
2879 #fi
4c2a3841 2880
2881 if ! printf -- "%s" "$_rcert" | _dbase64 "multiline" | _base64 "multiline" >>"$CERT_PATH"; then
72518d48 2882 _debug "Try cert link."
4c2a3841 2883 _get "$Le_LinkCert" | _base64 "multiline" >>"$CERT_PATH"
d404e92d 2884 fi
2885
4c2a3841 2886 echo "$END_CERT" >>"$CERT_PATH"
43822d37 2887 _info "$(__green "Cert success.")"
4c3b3608 2888 cat "$CERT_PATH"
5980ebc7 2889
4c2a3841 2890 _info "Your cert is in $(__green " $CERT_PATH ")"
2891
2892 if [ -f "$CERT_KEY_PATH" ]; then
2893 _info "Your cert key is in $(__green " $CERT_KEY_PATH ")"
5980ebc7 2894 fi
2895
caf1fc10 2896 cp "$CERT_PATH" "$CERT_FULLCHAIN_PATH"
281aa349 2897
4c2a3841 2898 if [ ! "$USER_PATH" ] || [ ! "$IN_CRON" ]; then
281aa349 2899 USER_PATH="$PATH"
2900 _saveaccountconf "USER_PATH" "$USER_PATH"
2901 fi
4c3b3608 2902 fi
4c3b3608 2903
4c2a3841 2904 if [ -z "$Le_LinkCert" ]; then
0c538f75 2905 response="$(echo "$response" | _dbase64 "multiline" | _normalizeJson)"
4c2a3841 2906 _err "Sign failed: $(echo "$response" | _egrep_o '"detail":"[^"]*"')"
b0070f03 2907 _on_issue_err
4c3b3608 2908 return 1
2909 fi
4c2a3841 2910
2911 _cleardomainconf "Le_Vlist"
2912
0c538f75 2913 Le_LinkIssuer=$(grep -i '^Link' "$HTTP_HEADER" | _head_n 1 | cut -d " " -f 2 | cut -d ';' -f 1 | tr -d '<>')
4c2a3841 2914 if ! _contains "$Le_LinkIssuer" ":"; then
fac1e367 2915 Le_LinkIssuer="$API$Le_LinkIssuer"
2916 fi
4c2a3841 2917
2918 _savedomainconf "Le_LinkIssuer" "$Le_LinkIssuer"
2919
2920 if [ "$Le_LinkIssuer" ]; then
2921 echo "$BEGIN_CERT" >"$CA_CERT_PATH"
2922 _get "$Le_LinkIssuer" | _base64 "multiline" >>"$CA_CERT_PATH"
2923 echo "$END_CERT" >>"$CA_CERT_PATH"
2924 _info "The intermediate CA cert is in $(__green " $CA_CERT_PATH ")"
2925 cat "$CA_CERT_PATH" >>"$CERT_FULLCHAIN_PATH"
2926 _info "And the full chain certs is there: $(__green " $CERT_FULLCHAIN_PATH ")"
4c3b3608 2927 fi
4c2a3841 2928
3aae1ae3 2929 Le_CertCreateTime=$(_time)
4c2a3841 2930 _savedomainconf "Le_CertCreateTime" "$Le_CertCreateTime"
2931
2932 Le_CertCreateTimeStr=$(date -u)
2933 _savedomainconf "Le_CertCreateTimeStr" "$Le_CertCreateTimeStr"
2934
2935 if [ -z "$Le_RenewalDays" ] || [ "$Le_RenewalDays" -lt "0" ] || [ "$Le_RenewalDays" -gt "$MAX_RENEW" ]; then
ca7202eb 2936 Le_RenewalDays="$MAX_RENEW"
054cb72e 2937 else
4c2a3841 2938 _savedomainconf "Le_RenewalDays" "$Le_RenewalDays"
13d7cae9 2939 fi
4c2a3841 2940
2941 if [ "$CA_BUNDLE" ]; then
78009539
PS
2942 _saveaccountconf CA_BUNDLE "$CA_BUNDLE"
2943 else
2944 _clearaccountconf "CA_BUNDLE"
2945 fi
2946
4c2a3841 2947 if [ "$HTTPS_INSECURE" ]; then
fac1e367 2948 _saveaccountconf HTTPS_INSECURE "$HTTPS_INSECURE"
2949 else
4c2a3841 2950 _clearaccountconf "HTTPS_INSECURE"
13d7cae9 2951 fi
00a50605 2952
4c2a3841 2953 if [ "$Le_Listen_V4" ]; then
2954 _savedomainconf "Le_Listen_V4" "$Le_Listen_V4"
50827188 2955 _cleardomainconf Le_Listen_V6
4c2a3841 2956 elif [ "$Le_Listen_V6" ]; then
2957 _savedomainconf "Le_Listen_V6" "$Le_Listen_V6"
50827188 2958 _cleardomainconf Le_Listen_V4
2959 fi
f6dcd989 2960
ca7202eb 2961 Le_NextRenewTime=$(_math "$Le_CertCreateTime" + "$Le_RenewalDays" \* 24 \* 60 \* 60)
4c2a3841 2962
ca7202eb 2963 Le_NextRenewTimeStr=$(_time2str "$Le_NextRenewTime")
4c2a3841 2964 _savedomainconf "Le_NextRenewTimeStr" "$Le_NextRenewTimeStr"
2965
ca7202eb 2966 Le_NextRenewTime=$(_math "$Le_NextRenewTime" - 86400)
4c2a3841 2967 _savedomainconf "Le_NextRenewTime" "$Le_NextRenewTime"
f6dcd989 2968
b0070f03 2969 _on_issue_success
4c3b3608 2970
4c2a3841 2971 if [ "$Le_RealCertPath$Le_RealKeyPath$Le_RealCACertPath$Le_ReloadCmd$Le_RealFullChainPath" ]; then
43822d37 2972 _installcert
01f54558 2973 fi
4c0d3f1b 2974
4c3b3608 2975}
2976
43822d37 2977#domain [isEcc]
4c3b3608 2978renew() {
2979 Le_Domain="$1"
4c2a3841 2980 if [ -z "$Le_Domain" ]; then
43822d37 2981 _usage "Usage: $PROJECT_ENTRY --renew -d domain.com [--ecc]"
4c3b3608 2982 return 1
2983 fi
2984
43822d37 2985 _isEcc="$2"
2986
e799ef29 2987 _initpath "$Le_Domain" "$_isEcc"
43822d37 2988
e2053b22 2989 _info "$(__green "Renew: '$Le_Domain'")"
4c2a3841 2990 if [ ! -f "$DOMAIN_CONF" ]; then
43822d37 2991 _info "'$Le_Domain' is not a issued domain, skip."
4c2a3841 2992 return 0
4c3b3608 2993 fi
4c2a3841 2994
2995 if [ "$Le_RenewalDays" ]; then
1e6b68f5 2996 _savedomainconf Le_RenewalDays "$Le_RenewalDays"
2997 fi
2998
8663fb7e 2999 . "$DOMAIN_CONF"
4c2a3841 3000
3001 if [ "$Le_API" ]; then
5c48e139 3002 API="$Le_API"
c4236e58 3003 #reload ca configs
3004 ACCOUNT_KEY_PATH=""
3005 ACCOUNT_JSON_PATH=""
3006 CA_CONF=""
3007 _debug3 "initpath again."
3008 _initpath "$Le_Domain" "$_isEcc"
5c48e139 3009 fi
4c2a3841 3010
3011 if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(_time)" -lt "$Le_NextRenewTime" ]; then
e2053b22 3012 _info "Skip, Next renewal time is: $(__green "$Le_NextRenewTimeStr")"
3013 _info "Add '$(__red '--force')' to force to renew."
e799ef29 3014 return "$RENEW_SKIP"
4c3b3608 3015 fi
4c2a3841 3016
4c3b3608 3017 IS_RENEW="1"
0463b5d6 3018 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 3019 res="$?"
4c2a3841 3020 if [ "$res" != "0" ]; then
e799ef29 3021 return "$res"
a61fe418 3022 fi
4c2a3841 3023
3024 if [ "$Le_DeployHook" ]; then
e799ef29 3025 deploy "$Le_Domain" "$Le_DeployHook" "$Le_Keylength"
3026 res="$?"
a61fe418 3027 fi
4c2a3841 3028
4c3b3608 3029 IS_RENEW=""
3030
e799ef29 3031 return "$res"
4c3b3608 3032}
3033
cc179731 3034#renewAll [stopRenewOnError]
4c3b3608 3035renewAll() {
3036 _initpath
cc179731 3037 _stopRenewOnError="$1"
3038 _debug "_stopRenewOnError" "$_stopRenewOnError"
3039 _ret="0"
43822d37 3040
e591d5cf 3041 for di in "${CERT_HOME}"/*.*/; do
3042 _debug di "$di"
44483dba 3043 if ! [ -d "$di" ]; then
3498a585 3044 _debug "Not directory, skip: $di"
3045 continue
3046 fi
e591d5cf 3047 d=$(basename "$di")
201aa244 3048 _debug d "$d"
43822d37 3049 (
201aa244 3050 if _endswith "$d" "$ECC_SUFFIX"; then
3051 _isEcc=$(echo "$d" | cut -d "$ECC_SEP" -f 2)
3052 d=$(echo "$d" | cut -d "$ECC_SEP" -f 1)
43822d37 3053 fi
3054 renew "$d" "$_isEcc"
4d2f38b0 3055 )
cc179731 3056 rc="$?"
3057 _debug "Return code: $rc"
4c2a3841 3058 if [ "$rc" != "0" ]; then
3059 if [ "$rc" = "$RENEW_SKIP" ]; then
cc179731 3060 _info "Skipped $d"
4c2a3841 3061 elif [ "$_stopRenewOnError" ]; then
cc179731 3062 _err "Error renew $d, stop now."
201aa244 3063 return "$rc"
cc179731 3064 else
3065 _ret="$rc"
3066 _err "Error renew $d, Go ahead to next one."
3067 fi
3068 fi
4c3b3608 3069 done
201aa244 3070 return "$_ret"
4c3b3608 3071}
3072
10afcaca 3073#csr webroot
4c2a3841 3074signcsr() {
10afcaca 3075 _csrfile="$1"
3076 _csrW="$2"
3077 if [ -z "$_csrfile" ] || [ -z "$_csrW" ]; then
3078 _usage "Usage: $PROJECT_ENTRY --signcsr --csr mycsr.csr -w /path/to/webroot/a.com/ "
3079 return 1
3080 fi
3081
3082 _initpath
3083
3084 _csrsubj=$(_readSubjectFromCSR "$_csrfile")
4c2a3841 3085 if [ "$?" != "0" ]; then
10afcaca 3086 _err "Can not read subject from csr: $_csrfile"
3087 return 1
3088 fi
ad752b31 3089 _debug _csrsubj "$_csrsubj"
10afcaca 3090
3091 _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile")
4c2a3841 3092 if [ "$?" != "0" ]; then
10afcaca 3093 _err "Can not read domain list from csr: $_csrfile"
3094 return 1
3095 fi
3096 _debug "_csrdomainlist" "$_csrdomainlist"
4c2a3841 3097
3098 if [ -z "$_csrsubj" ]; then
ad752b31 3099 _csrsubj="$(_getfield "$_csrdomainlist" 1)"
3100 _debug _csrsubj "$_csrsubj"
3101 _csrdomainlist="$(echo "$_csrdomainlist" | cut -d , -f 2-)"
3102 _debug "_csrdomainlist" "$_csrdomainlist"
3103 fi
4c2a3841 3104
3105 if [ -z "$_csrsubj" ]; then
ad752b31 3106 _err "Can not read subject from csr: $_csrfile"
3107 return 1
3108 fi
4c2a3841 3109
10afcaca 3110 _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile")
4c2a3841 3111 if [ "$?" != "0" ] || [ -z "$_csrkeylength" ]; then
10afcaca 3112 _err "Can not read key length from csr: $_csrfile"
3113 return 1
3114 fi
4c2a3841 3115
10afcaca 3116 _initpath "$_csrsubj" "$_csrkeylength"
3117 mkdir -p "$DOMAIN_PATH"
4c2a3841 3118
10afcaca 3119 _info "Copy csr to: $CSR_PATH"
3120 cp "$_csrfile" "$CSR_PATH"
4c2a3841 3121
10afcaca 3122 issue "$_csrW" "$_csrsubj" "$_csrdomainlist" "$_csrkeylength"
4c2a3841 3123
10afcaca 3124}
3125
3126showcsr() {
4c2a3841 3127 _csrfile="$1"
10afcaca 3128 _csrd="$2"
3129 if [ -z "$_csrfile" ] && [ -z "$_csrd" ]; then
3130 _usage "Usage: $PROJECT_ENTRY --showcsr --csr mycsr.csr"
3131 return 1
3132 fi
3133
3134 _initpath
4c2a3841 3135
10afcaca 3136 _csrsubj=$(_readSubjectFromCSR "$_csrfile")
4c2a3841 3137 if [ "$?" != "0" ] || [ -z "$_csrsubj" ]; then
10afcaca 3138 _err "Can not read subject from csr: $_csrfile"
3139 return 1
3140 fi
4c2a3841 3141
10afcaca 3142 _info "Subject=$_csrsubj"
3143
3144 _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile")
4c2a3841 3145 if [ "$?" != "0" ]; then
10afcaca 3146 _err "Can not read domain list from csr: $_csrfile"
3147 return 1
3148 fi
3149 _debug "_csrdomainlist" "$_csrdomainlist"
3150
3151 _info "SubjectAltNames=$_csrdomainlist"
3152
10afcaca 3153 _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile")
4c2a3841 3154 if [ "$?" != "0" ] || [ -z "$_csrkeylength" ]; then
10afcaca 3155 _err "Can not read key length from csr: $_csrfile"
3156 return 1
3157 fi
3158 _info "KeyLength=$_csrkeylength"
3159}
3160
6d7eda3e 3161list() {
22ea4004 3162 _raw="$1"
6d7eda3e 3163 _initpath
4c2a3841 3164
dcf4f8f6 3165 _sep="|"
4c2a3841 3166 if [ "$_raw" ]; then
d5ec5f80 3167 printf "%s\n" "Main_Domain${_sep}KeyLength${_sep}SAN_Domains${_sep}Created${_sep}Renew"
e591d5cf 3168 for di in "${CERT_HOME}"/*.*/; do
44483dba 3169 if ! [ -d "$di" ]; then
3498a585 3170 _debug "Not directory, skip: $di"
3171 continue
3172 fi
e591d5cf 3173 d=$(basename "$di")
201aa244 3174 _debug d "$d"
dcf4f8f6 3175 (
201aa244 3176 if _endswith "$d" "$ECC_SUFFIX"; then
3177 _isEcc=$(echo "$d" | cut -d "$ECC_SEP" -f 2)
3178 d=$(echo "$d" | cut -d "$ECC_SEP" -f 1)
43822d37 3179 fi
e591d5cf 3180 _initpath "$d" "$_isEcc"
4c2a3841 3181 if [ -f "$DOMAIN_CONF" ]; then
dcf4f8f6 3182 . "$DOMAIN_CONF"
d5ec5f80 3183 printf "%s\n" "$Le_Domain${_sep}\"$Le_Keylength\"${_sep}$Le_Alt${_sep}$Le_CertCreateTimeStr${_sep}$Le_NextRenewTimeStr"
dcf4f8f6 3184 fi
3185 )
3186 done
3187 else
4c2a3841 3188 if _exists column; then
22ea4004 3189 list "raw" | column -t -s "$_sep"
3190 else
43822d37 3191 list "raw" | tr "$_sep" '\t'
22ea4004 3192 fi
dcf4f8f6 3193 fi
6d7eda3e 3194
6d7eda3e 3195}
3196
a61fe418 3197deploy() {
3198 Le_Domain="$1"
3199 Le_DeployHook="$2"
3200 _isEcc="$3"
4c2a3841 3201 if [ -z "$Le_DeployHook" ]; then
a61fe418 3202 _usage "Usage: $PROJECT_ENTRY --deploy -d domain.com --deploy-hook cpanel [--ecc] "
3203 return 1
3204 fi
3205
e591d5cf 3206 _initpath "$Le_Domain" "$_isEcc"
4c2a3841 3207 if [ ! -d "$DOMAIN_PATH" ]; then
a61fe418 3208 _err "Domain is not valid:'$Le_Domain'"
3209 return 1
3210 fi
3211
e591d5cf 3212 _deployApi="$(_findHook "$Le_Domain" deploy "$Le_DeployHook")"
4c2a3841 3213 if [ -z "$_deployApi" ]; then
a61fe418 3214 _err "The deploy hook $Le_DeployHook is not found."
3215 return 1
3216 fi
3217 _debug _deployApi "$_deployApi"
4c2a3841 3218
a61fe418 3219 _savedomainconf Le_DeployHook "$Le_DeployHook"
4c2a3841 3220
a61fe418 3221 if ! (
e591d5cf 3222 if ! . "$_deployApi"; then
a61fe418 3223 _err "Load file $_deployApi error. Please check your api file and try again."
3224 return 1
3225 fi
4c2a3841 3226
a61fe418 3227 d_command="${Le_DeployHook}_deploy"
e591d5cf 3228 if ! _exists "$d_command"; then
a61fe418 3229 _err "It seems that your api file is not correct, it must have a function named: $d_command"
3230 return 1
3231 fi
4c2a3841 3232
e591d5cf 3233 if ! $d_command "$Le_Domain" "$CERT_KEY_PATH" "$CERT_PATH" "$CA_CERT_PATH" "$CERT_FULLCHAIN_PATH"; then
a61fe418 3234 _err "Error deploy for domain:$Le_Domain"
3235 _on_issue_err
3236 return 1
3237 fi
4c2a3841 3238 ); then
a61fe418 3239 _err "Deploy error."
3240 return 1
3241 else
3242 _info "$(__green Success)"
3243 fi
4c2a3841 3244
a61fe418 3245}
3246
4c3b3608 3247installcert() {
3248 Le_Domain="$1"
4c2a3841 3249 if [ -z "$Le_Domain" ]; then
43822d37 3250 _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 3251 return 1
3252 fi
3253
3254 Le_RealCertPath="$2"
3255 Le_RealKeyPath="$3"
3256 Le_RealCACertPath="$4"
3257 Le_ReloadCmd="$5"
a63b05a9 3258 Le_RealFullChainPath="$6"
43822d37 3259 _isEcc="$7"
3260
4bd31f49 3261 _initpath "$Le_Domain" "$_isEcc"
4c2a3841 3262 if [ ! -d "$DOMAIN_PATH" ]; then
43822d37 3263 _err "Domain is not valid:'$Le_Domain'"
3264 return 1
3265 fi
3266
3267 _installcert
3268}
4c3b3608 3269
43822d37 3270_installcert() {
4c2a3841 3271 _savedomainconf "Le_RealCertPath" "$Le_RealCertPath"
3272 _savedomainconf "Le_RealCACertPath" "$Le_RealCACertPath"
3273 _savedomainconf "Le_RealKeyPath" "$Le_RealKeyPath"
3274 _savedomainconf "Le_ReloadCmd" "$Le_ReloadCmd"
3275 _savedomainconf "Le_RealFullChainPath" "$Le_RealFullChainPath"
4c3b3608 3276
4c2a3841 3277 if [ "$Le_RealCertPath" = "$NO_VALUE" ]; then
4d2f38b0 3278 Le_RealCertPath=""
3279 fi
4c2a3841 3280 if [ "$Le_RealKeyPath" = "$NO_VALUE" ]; then
4d2f38b0 3281 Le_RealKeyPath=""
3282 fi
4c2a3841 3283 if [ "$Le_RealCACertPath" = "$NO_VALUE" ]; then
4d2f38b0 3284 Le_RealCACertPath=""
3285 fi
4c2a3841 3286 if [ "$Le_ReloadCmd" = "$NO_VALUE" ]; then
4d2f38b0 3287 Le_ReloadCmd=""
3288 fi
4c2a3841 3289 if [ "$Le_RealFullChainPath" = "$NO_VALUE" ]; then
4d2f38b0 3290 Le_RealFullChainPath=""
3291 fi
4c2a3841 3292
4c2a3841 3293 if [ "$Le_RealCertPath" ]; then
4bd31f49 3294
4d2f38b0 3295 _info "Installing cert to:$Le_RealCertPath"
4c2a3841 3296 if [ -f "$Le_RealCertPath" ] && [ ! "$IS_RENEW" ]; then
ff3bce32 3297 cp "$Le_RealCertPath" "$Le_RealCertPath".bak
4c3b3608 3298 fi
4c2a3841 3299 cat "$CERT_PATH" >"$Le_RealCertPath"
4c3b3608 3300 fi
4c2a3841 3301
3302 if [ "$Le_RealCACertPath" ]; then
4bd31f49 3303
4d2f38b0 3304 _info "Installing CA to:$Le_RealCACertPath"
4c2a3841 3305 if [ "$Le_RealCACertPath" = "$Le_RealCertPath" ]; then
3306 echo "" >>"$Le_RealCACertPath"
3307 cat "$CA_CERT_PATH" >>"$Le_RealCACertPath"
4c3b3608 3308 else
4c2a3841 3309 if [ -f "$Le_RealCACertPath" ] && [ ! "$IS_RENEW" ]; then
ff3bce32 3310 cp "$Le_RealCACertPath" "$Le_RealCACertPath".bak
78552b18 3311 fi
4c2a3841 3312 cat "$CA_CERT_PATH" >"$Le_RealCACertPath"
4c3b3608 3313 fi
3314 fi
3315
4c2a3841 3316 if [ "$Le_RealKeyPath" ]; then
4c3b3608 3317
4d2f38b0 3318 _info "Installing key to:$Le_RealKeyPath"
4c2a3841 3319 if [ -f "$Le_RealKeyPath" ] && [ ! "$IS_RENEW" ]; then
ff3bce32 3320 cp "$Le_RealKeyPath" "$Le_RealKeyPath".bak
4c3b3608 3321 fi
4c2a3841 3322 cat "$CERT_KEY_PATH" >"$Le_RealKeyPath"
4c3b3608 3323 fi
4c2a3841 3324
3325 if [ "$Le_RealFullChainPath" ]; then
4bd31f49 3326
4d2f38b0 3327 _info "Installing full chain to:$Le_RealFullChainPath"
4c2a3841 3328 if [ -f "$Le_RealFullChainPath" ] && [ ! "$IS_RENEW" ]; then
ff3bce32 3329 cp "$Le_RealFullChainPath" "$Le_RealFullChainPath".bak
a63b05a9 3330 fi
4c2a3841 3331 cat "$CERT_FULLCHAIN_PATH" >"$Le_RealFullChainPath"
3332 fi
4c3b3608 3333
4c2a3841 3334 if [ "$Le_ReloadCmd" ]; then
4c3b3608 3335
4c3b3608 3336 _info "Run Le_ReloadCmd: $Le_ReloadCmd"
4c2a3841 3337 if (cd "$DOMAIN_PATH" && eval "$Le_ReloadCmd"); then
43822d37 3338 _info "$(__green "Reload success")"
4d2f38b0 3339 else
3340 _err "Reload error for :$Le_Domain"
3341 fi
3342 fi
3343
4c3b3608 3344}
3345
3346installcronjob() {
3347 _initpath
4c2a3841 3348 if ! _exists "crontab"; then
77546ea5 3349 _err "crontab doesn't exist, so, we can not install cron jobs."
3350 _err "All your certs will not be renewed automatically."
a7b7355d 3351 _err "You must add your own cron job to call '$PROJECT_ENTRY --cron' everyday."
77546ea5 3352 return 1
3353 fi
3354
4c3b3608 3355 _info "Installing cron job"
4c2a3841 3356 if ! crontab -l | grep "$PROJECT_ENTRY --cron"; then
3357 if [ -f "$LE_WORKING_DIR/$PROJECT_ENTRY" ]; then
a7b7355d 3358 lesh="\"$LE_WORKING_DIR\"/$PROJECT_ENTRY"
4c3b3608 3359 else
a7b7355d 3360 _err "Can not install cronjob, $PROJECT_ENTRY not found."
4c3b3608 3361 return 1
3362 fi
e2c939fb 3363 if _exists uname && uname -a | grep SunOS >/dev/null; then
4c2a3841 3364 crontab -l | {
3365 cat
3366 echo "0 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null"
3367 } | crontab --
22ea4004 3368 else
4c2a3841 3369 crontab -l | {
3370 cat
3371 echo "0 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null"
3372 } | crontab -
22ea4004 3373 fi
4c3b3608 3374 fi
4c2a3841 3375 if [ "$?" != "0" ]; then
4c3b3608 3376 _err "Install cron job failed. You need to manually renew your certs."
3377 _err "Or you can add cronjob by yourself:"
a7b7355d 3378 _err "$lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null"
4c3b3608 3379 return 1
3380 fi
3381}
3382
3383uninstallcronjob() {
4c2a3841 3384 if ! _exists "crontab"; then
37db5b81 3385 return
3386 fi
4c3b3608 3387 _info "Removing cron job"
a7b7355d 3388 cr="$(crontab -l | grep "$PROJECT_ENTRY --cron")"
4c2a3841 3389 if [ "$cr" ]; then
3390 if _exists uname && uname -a | grep solaris >/dev/null; then
22ea4004 3391 crontab -l | sed "/$PROJECT_ENTRY --cron/d" | crontab --
3392 else
3393 crontab -l | sed "/$PROJECT_ENTRY --cron/d" | crontab -
3394 fi
a7b7355d 3395 LE_WORKING_DIR="$(echo "$cr" | cut -d ' ' -f 9 | tr -d '"')"
4c3b3608 3396 _info LE_WORKING_DIR "$LE_WORKING_DIR"
4c2a3841 3397 fi
4c3b3608 3398 _initpath
a7b7355d 3399
4c3b3608 3400}
3401
6cb415f5 3402revoke() {
3403 Le_Domain="$1"
4c2a3841 3404 if [ -z "$Le_Domain" ]; then
43822d37 3405 _usage "Usage: $PROJECT_ENTRY --revoke -d domain.com"
6cb415f5 3406 return 1
3407 fi
4c2a3841 3408
43822d37 3409 _isEcc="$2"
3410
c4a375b3 3411 _initpath "$Le_Domain" "$_isEcc"
4c2a3841 3412 if [ ! -f "$DOMAIN_CONF" ]; then
6cb415f5 3413 _err "$Le_Domain is not a issued domain, skip."
4c2a3841 3414 return 1
6cb415f5 3415 fi
4c2a3841 3416
3417 if [ ! -f "$CERT_PATH" ]; then
6cb415f5 3418 _err "Cert for $Le_Domain $CERT_PATH is not found, skip."
3419 return 1
3420 fi
6cb415f5 3421
4c2a3841 3422 cert="$(_getfile "${CERT_PATH}" "${BEGIN_CERT}" "${END_CERT}" | tr -d "\r\n" | _urlencode)"
3423
3424 if [ -z "$cert" ]; then
6cb415f5 3425 _err "Cert for $Le_Domain is empty found, skip."
3426 return 1
3427 fi
4c2a3841 3428
6cb415f5 3429 data="{\"resource\": \"revoke-cert\", \"certificate\": \"$cert\"}"
3430 uri="$API/acme/revoke-cert"
3431
4c2a3841 3432 if [ -f "$CERT_KEY_PATH" ]; then
1befee5a 3433 _info "Try domain key first."
c4a375b3 3434 if _send_signed_request "$uri" "$data" "" "$CERT_KEY_PATH"; then
4c2a3841 3435 if [ -z "$response" ]; then
1befee5a 3436 _info "Revoke success."
c4a375b3 3437 rm -f "$CERT_PATH"
1befee5a 3438 return 0
4c2a3841 3439 else
1befee5a 3440 _err "Revoke error by domain key."
3441 _err "$response"
3442 fi
6cb415f5 3443 fi
4c2a3841 3444 else
1befee5a 3445 _info "Domain key file doesn't exists."
6cb415f5 3446 fi
6cb415f5 3447
1befee5a 3448 _info "Try account key."
6cb415f5 3449
c4a375b3 3450 if _send_signed_request "$uri" "$data" "" "$ACCOUNT_KEY_PATH"; then
4c2a3841 3451 if [ -z "$response" ]; then
6cb415f5 3452 _info "Revoke success."
c4a375b3 3453 rm -f "$CERT_PATH"
6cb415f5 3454 return 0
4c2a3841 3455 else
6cb415f5 3456 _err "Revoke error."
c9c31c04 3457 _debug "$response"
6cb415f5 3458 fi
3459 fi
3460 return 1
3461}
4c3b3608 3462
0c00e870 3463#domain vtype
3464_deactivate() {
3465 _d_domain="$1"
3466 _d_type="$2"
3467 _initpath
4c2a3841 3468
0c00e870 3469 _d_i=0
3470 _d_max_retry=9
4c2a3841 3471 while [ "$_d_i" -lt "$_d_max_retry" ]; do
0407c4e0 3472 _info "Deactivate: $_d_domain"
0c00e870 3473 _d_i="$(_math $_d_i + 1)"
4c2a3841 3474
3475 if ! __get_domain_new_authz "$_d_domain"; then
f940b2a5 3476 _err "Can not get domain new authz token."
0c00e870 3477 return 1
3478 fi
4c2a3841 3479
c2c8f320 3480 authzUri="$(echo "$responseHeaders" | grep "^Location:" | _head_n 1 | cut -d ' ' -f 2 | tr -d "\r\n")"
3f4513b3 3481 _debug "authzUri" "$authzUri"
0c00e870 3482
4c2a3841 3483 if [ ! -z "$code" ] && [ ! "$code" = '201' ]; then
0c00e870 3484 _err "new-authz error: $response"
3485 return 1
3486 fi
4c2a3841 3487
947d14ff 3488 entry="$(printf "%s\n" "$response" | _egrep_o '{"type":"[^"]*","status":"valid","uri"[^}]*')"
0c00e870 3489 _debug entry "$entry"
4c2a3841 3490
3491 if [ -z "$entry" ]; then
fb2029e7 3492 _info "No more valid entry found."
0c00e870 3493 break
3494 fi
4c2a3841 3495
0c00e870 3496 _vtype="$(printf "%s\n" "$entry" | _egrep_o '"type": *"[^"]*"' | cut -d : -f 2 | tr -d '"')"
c4a375b3 3497 _debug _vtype "$_vtype"
0c00e870 3498 _info "Found $_vtype"
3499
4c2a3841 3500 uri="$(printf "%s\n" "$entry" | _egrep_o '"uri":"[^"]*' | cut -d : -f 2,3 | tr -d '"')"
c4a375b3 3501 _debug uri "$uri"
4c2a3841 3502
3503 if [ "$_d_type" ] && [ "$_d_type" != "$_vtype" ]; then
0c00e870 3504 _info "Skip $_vtype"
3505 continue
3506 fi
4c2a3841 3507
0c00e870 3508 _info "Deactivate: $_vtype"
4c2a3841 3509
3510 if ! _send_signed_request "$authzUri" "{\"resource\": \"authz\", \"status\":\"deactivated\"}"; then
0c00e870 3511 _err "Can not deactivate $_vtype."
3512 return 1
3513 fi
4c2a3841 3514
fb2029e7 3515 _info "Deactivate: $_vtype success."
4c2a3841 3516
0c00e870 3517 done
3518 _debug "$_d_i"
4c2a3841 3519 if [ "$_d_i" -lt "$_d_max_retry" ]; then
0c00e870 3520 _info "Deactivated success!"
3521 else
3522 _err "Deactivate failed."
3523 fi
3524
3525}
3526
3527deactivate() {
3f4513b3 3528 _d_domain_list="$1"
0c00e870 3529 _d_type="$2"
3530 _initpath
3f4513b3 3531 _debug _d_domain_list "$_d_domain_list"
4c2a3841 3532 if [ -z "$(echo $_d_domain_list | cut -d , -f 1)" ]; then
3f4513b3 3533 _usage "Usage: $PROJECT_ENTRY --deactivate -d domain.com [-d domain.com]"
0c00e870 3534 return 1
3535 fi
4c2a3841 3536 for _d_dm in $(echo "$_d_domain_list" | tr ',' ' '); do
3537 if [ -z "$_d_dm" ] || [ "$_d_dm" = "$NO_VALUE" ]; then
3f4513b3 3538 continue
3539 fi
c4a375b3 3540 if ! _deactivate "$_d_dm" "$_d_type"; then
86c017ec 3541 return 1
3542 fi
3f4513b3 3543 done
0c00e870 3544}
3545
4c3b3608 3546# Detect profile file if not specified as environment variable
3547_detect_profile() {
4c2a3841 3548 if [ -n "$PROFILE" -a -f "$PROFILE" ]; then
4c3b3608 3549 echo "$PROFILE"
3550 return
3551 fi
3552
4c3b3608 3553 DETECTED_PROFILE=''
4c3b3608 3554 SHELLTYPE="$(basename "/$SHELL")"
3555
4c2a3841 3556 if [ "$SHELLTYPE" = "bash" ]; then
3557 if [ -f "$HOME/.bashrc" ]; then
4c3b3608 3558 DETECTED_PROFILE="$HOME/.bashrc"
4c2a3841 3559 elif [ -f "$HOME/.bash_profile" ]; then
4c3b3608 3560 DETECTED_PROFILE="$HOME/.bash_profile"
3561 fi
4c2a3841 3562 elif [ "$SHELLTYPE" = "zsh" ]; then
4c3b3608 3563 DETECTED_PROFILE="$HOME/.zshrc"
3564 fi
3565
4c2a3841 3566 if [ -z "$DETECTED_PROFILE" ]; then
3567 if [ -f "$HOME/.profile" ]; then
4c3b3608 3568 DETECTED_PROFILE="$HOME/.profile"
4c2a3841 3569 elif [ -f "$HOME/.bashrc" ]; then
4c3b3608 3570 DETECTED_PROFILE="$HOME/.bashrc"
4c2a3841 3571 elif [ -f "$HOME/.bash_profile" ]; then
4c3b3608 3572 DETECTED_PROFILE="$HOME/.bash_profile"
4c2a3841 3573 elif [ -f "$HOME/.zshrc" ]; then
4c3b3608 3574 DETECTED_PROFILE="$HOME/.zshrc"
3575 fi
3576 fi
3577
4c2a3841 3578 if [ ! -z "$DETECTED_PROFILE" ]; then
4c3b3608 3579 echo "$DETECTED_PROFILE"
3580 fi
3581}
3582
3583_initconf() {
3584 _initpath
4c2a3841 3585 if [ ! -f "$ACCOUNT_CONF_PATH" ]; then
d53289d7 3586 echo "#ACCOUNT_CONF_PATH=xxxx
3587
caa2e45a 3588#ACCOUNT_EMAIL=aaa@example.com # the account email used to register account.
5fd3f21b 3589#ACCOUNT_KEY_PATH=\"/path/to/account.key\"
b2817897 3590#CERT_HOME=\"/path/to/cert/home\"
4c3b3608 3591
d404e92d 3592
d0871bda 3593#LOG_FILE=\"$DEFAULT_LOG_FILE\"
6b500036 3594#LOG_LEVEL=1
5ea6e9c9 3595
251d1c5c 3596#AUTO_UPGRADE=\"1\"
89002ed2 3597
569d6c55 3598#NO_TIMESTAMP=1
a746139c 3599#OPENSSL_BIN=openssl
166096dc 3600
8814a348 3601#USER_AGENT=\"$USER_AGENT\"
281aa349 3602
c4a375b3 3603#USER_PATH=
281aa349 3604
5b771039 3605
d5ec5f80 3606 " >"$ACCOUNT_CONF_PATH"
4c3b3608 3607 fi
3608}
3609
c8e9a31e 3610# nocron
c60883ef 3611_precheck() {
c8e9a31e 3612 _nocron="$1"
4c2a3841 3613
3614 if ! _exists "curl" && ! _exists "wget"; then
c60883ef 3615 _err "Please install curl or wget first, we need to access http resources."
4c3b3608 3616 return 1
3617 fi
4c2a3841 3618
3619 if [ -z "$_nocron" ]; then
3620 if ! _exists "crontab"; then
c8e9a31e 3621 _err "It is recommended to install crontab first. try to install 'cron, crontab, crontabs or vixie-cron'."
3622 _err "We need to set cron job to renew the certs automatically."
3623 _err "Otherwise, your certs will not be able to be renewed automatically."
4c2a3841 3624 if [ -z "$FORCE" ]; then
c8e9a31e 3625 _err "Please add '--force' and try install again to go without crontab."
3626 _err "./$PROJECT_ENTRY --install --force"
3627 return 1
3628 fi
77546ea5 3629 fi
4c3b3608 3630 fi
4c2a3841 3631
a746139c 3632 if ! _exists "$OPENSSL_BIN"; then
c60883ef 3633 _err "Please install openssl first."
3634 _err "We need openssl to generate keys."
4c3b3608 3635 return 1
3636 fi
4c2a3841 3637
3638 if ! _exists "nc"; then
c60883ef 3639 _err "It is recommended to install nc first, try to install 'nc' or 'netcat'."
3640 _err "We use nc for standalone server if you use standalone mode."
3641 _err "If you don't use standalone mode, just ignore this warning."
3642 fi
4c2a3841 3643
c60883ef 3644 return 0
3645}
3646
0a7c9364 3647_setShebang() {
3648 _file="$1"
3649 _shebang="$2"
4c2a3841 3650 if [ -z "$_shebang" ]; then
43822d37 3651 _usage "Usage: file shebang"
0a7c9364 3652 return 1
3653 fi
3654 cp "$_file" "$_file.tmp"
4c2a3841 3655 echo "$_shebang" >"$_file"
3656 sed -n 2,99999p "$_file.tmp" >>"$_file"
3657 rm -f "$_file.tmp"
0a7c9364 3658}
3659
94dc5f33 3660_installalias() {
3661 _initpath
3662
3663 _envfile="$LE_WORKING_DIR/$PROJECT_ENTRY.env"
4c2a3841 3664 if [ "$_upgrading" ] && [ "$_upgrading" = "1" ]; then
44edb2bd 3665 echo "$(cat "$_envfile")" | sed "s|^LE_WORKING_DIR.*$||" >"$_envfile"
3666 echo "$(cat "$_envfile")" | sed "s|^alias le.*$||" >"$_envfile"
3667 echo "$(cat "$_envfile")" | sed "s|^alias le.sh.*$||" >"$_envfile"
94dc5f33 3668 fi
3669
1786a5e5 3670 _setopt "$_envfile" "export LE_WORKING_DIR" "=" "\"$LE_WORKING_DIR\""
94dc5f33 3671 _setopt "$_envfile" "alias $PROJECT_ENTRY" "=" "\"$LE_WORKING_DIR/$PROJECT_ENTRY\""
3672
3673 _profile="$(_detect_profile)"
4c2a3841 3674 if [ "$_profile" ]; then
94dc5f33 3675 _debug "Found profile: $_profile"
aba5c634 3676 _info "Installing alias to '$_profile'"
94dc5f33 3677 _setopt "$_profile" ". \"$_envfile\""
3678 _info "OK, Close and reopen your terminal to start using $PROJECT_NAME"
3679 else
3680 _info "No profile is found, you will need to go into $LE_WORKING_DIR to use $PROJECT_NAME"
3681 fi
94dc5f33 3682
3683 #for csh
3684 _cshfile="$LE_WORKING_DIR/$PROJECT_ENTRY.csh"
94dc5f33 3685 _csh_profile="$HOME/.cshrc"
4c2a3841 3686 if [ -f "$_csh_profile" ]; then
aba5c634 3687 _info "Installing alias to '$_csh_profile'"
6626371d 3688 _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\""
3689 _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY\""
4c2a3841 3690 _setopt "$_csh_profile" "source \"$_cshfile\""
94dc5f33 3691 fi
4c2a3841 3692
acafa585 3693 #for tcsh
3694 _tcsh_profile="$HOME/.tcshrc"
4c2a3841 3695 if [ -f "$_tcsh_profile" ]; then
aba5c634 3696 _info "Installing alias to '$_tcsh_profile'"
acafa585 3697 _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\""
3698 _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY\""
4c2a3841 3699 _setopt "$_tcsh_profile" "source \"$_cshfile\""
acafa585 3700 fi
94dc5f33 3701
3702}
3703
c8e9a31e 3704# nocron
c60883ef 3705install() {
f3e4cea3 3706
4c2a3841 3707 if [ -z "$LE_WORKING_DIR" ]; then
f3e4cea3 3708 LE_WORKING_DIR="$DEFAULT_INSTALL_HOME"
3709 fi
4c2a3841 3710
c8e9a31e 3711 _nocron="$1"
4c2a3841 3712 if ! _initpath; then
c60883ef 3713 _err "Install failed."
4c3b3608 3714 return 1
3715 fi
4c2a3841 3716 if [ "$_nocron" ]; then
52677b0a 3717 _debug "Skip install cron job"
3718 fi
4c2a3841 3719
3720 if ! _precheck "$_nocron"; then
c60883ef 3721 _err "Pre-check failed, can not install."
4c3b3608 3722 return 1
3723 fi
4c2a3841 3724
6cc11ffb 3725 #convert from le
4c2a3841 3726 if [ -d "$HOME/.le" ]; then
3727 for envfile in "le.env" "le.sh.env"; do
3728 if [ -f "$HOME/.le/$envfile" ]; then
3729 if grep "le.sh" "$HOME/.le/$envfile" >/dev/null; then
3730 _upgrading="1"
3731 _info "You are upgrading from le.sh"
3732 _info "Renaming \"$HOME/.le\" to $LE_WORKING_DIR"
3733 mv "$HOME/.le" "$LE_WORKING_DIR"
3734 mv "$LE_WORKING_DIR/$envfile" "$LE_WORKING_DIR/$PROJECT_ENTRY.env"
3735 break
6cc11ffb 3736 fi
3737 fi
3738 done
3739 fi
3740
4c3b3608 3741 _info "Installing to $LE_WORKING_DIR"
635695ec 3742
4c2a3841 3743 if ! mkdir -p "$LE_WORKING_DIR"; then
90035252 3744 _err "Can not create working dir: $LE_WORKING_DIR"
4a0f23e2 3745 return 1
3746 fi
4c2a3841 3747
762978f8 3748 chmod 700 "$LE_WORKING_DIR"
3749
d5ec5f80 3750 cp "$PROJECT_ENTRY" "$LE_WORKING_DIR/" && chmod +x "$LE_WORKING_DIR/$PROJECT_ENTRY"
4c3b3608 3751
4c2a3841 3752 if [ "$?" != "0" ]; then
a7b7355d 3753 _err "Install failed, can not copy $PROJECT_ENTRY"
4c3b3608 3754 return 1
3755 fi
3756
a7b7355d 3757 _info "Installed to $LE_WORKING_DIR/$PROJECT_ENTRY"
4c3b3608 3758
94dc5f33 3759 _installalias
4c3b3608 3760
4c2a3841 3761 for subf in $_SUB_FOLDERS; do
3762 if [ -d "$subf" ]; then
d5ec5f80 3763 mkdir -p "$LE_WORKING_DIR/$subf"
3764 cp "$subf"/* "$LE_WORKING_DIR"/"$subf"/
a61fe418 3765 fi
3766 done
3767
4c2a3841 3768 if [ ! -f "$ACCOUNT_CONF_PATH" ]; then
4c3b3608 3769 _initconf
3770 fi
6cc11ffb 3771
4c2a3841 3772 if [ "$_DEFAULT_ACCOUNT_CONF_PATH" != "$ACCOUNT_CONF_PATH" ]; then
635695ec 3773 _setopt "$_DEFAULT_ACCOUNT_CONF_PATH" "ACCOUNT_CONF_PATH" "=" "\"$ACCOUNT_CONF_PATH\""
6cc11ffb 3774 fi
3775
4c2a3841 3776 if [ "$_DEFAULT_CERT_HOME" != "$CERT_HOME" ]; then
b2817897 3777 _saveaccountconf "CERT_HOME" "$CERT_HOME"
3778 fi
3779
4c2a3841 3780 if [ "$_DEFAULT_ACCOUNT_KEY_PATH" != "$ACCOUNT_KEY_PATH" ]; then
b2817897 3781 _saveaccountconf "ACCOUNT_KEY_PATH" "$ACCOUNT_KEY_PATH"
3782 fi
4c2a3841 3783
3784 if [ -z "$_nocron" ]; then
c8e9a31e 3785 installcronjob
3786 fi
0a7c9364 3787
4c2a3841 3788 if [ -z "$NO_DETECT_SH" ]; then
641989fd 3789 #Modify shebang
4c2a3841 3790 if _exists bash; then
329174b6 3791 _info "Good, bash is found, so change the shebang to use bash as preferred."
641989fd 3792 _shebang='#!/usr/bin/env bash'
3793 _setShebang "$LE_WORKING_DIR/$PROJECT_ENTRY" "$_shebang"
4c2a3841 3794 for subf in $_SUB_FOLDERS; do
3795 if [ -d "$LE_WORKING_DIR/$subf" ]; then
3796 for _apifile in "$LE_WORKING_DIR/$subf/"*.sh; do
a61fe418 3797 _setShebang "$_apifile" "$_shebang"
3798 done
3799 fi
3800 done
0a7c9364 3801 fi
3802 fi
3803
4c3b3608 3804 _info OK
3805}
3806
52677b0a 3807# nocron
4c3b3608 3808uninstall() {
52677b0a 3809 _nocron="$1"
4c2a3841 3810 if [ -z "$_nocron" ]; then
52677b0a 3811 uninstallcronjob
3812 fi
4c3b3608 3813 _initpath
3814
9aa3be7f 3815 _uninstallalias
4c2a3841 3816
d5ec5f80 3817 rm -f "$LE_WORKING_DIR/$PROJECT_ENTRY"
9aa3be7f 3818 _info "The keys and certs are in $LE_WORKING_DIR, you can remove them by yourself."
3819
3820}
3821
3822_uninstallalias() {
3823 _initpath
3824
4c3b3608 3825 _profile="$(_detect_profile)"
4c2a3841 3826 if [ "$_profile" ]; then
9aa3be7f 3827 _info "Uninstalling alias from: '$_profile'"
d5ec5f80 3828 text="$(cat "$_profile")"
4c2a3841 3829 echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.env\"$||" >"$_profile"
4c3b3608 3830 fi
3831
94dc5f33 3832 _csh_profile="$HOME/.cshrc"
4c2a3841 3833 if [ -f "$_csh_profile" ]; then
9aa3be7f 3834 _info "Uninstalling alias from: '$_csh_profile'"
d5ec5f80 3835 text="$(cat "$_csh_profile")"
4c2a3841 3836 echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" >"$_csh_profile"
94dc5f33 3837 fi
4c2a3841 3838
acafa585 3839 _tcsh_profile="$HOME/.tcshrc"
4c2a3841 3840 if [ -f "$_tcsh_profile" ]; then
9aa3be7f 3841 _info "Uninstalling alias from: '$_csh_profile'"
d5ec5f80 3842 text="$(cat "$_tcsh_profile")"
4c2a3841 3843 echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" >"$_tcsh_profile"
acafa585 3844 fi
4c3b3608 3845
3846}
3847
3848cron() {
281aa349 3849 IN_CRON=1
89002ed2 3850 _initpath
4c2a3841 3851 if [ "$AUTO_UPGRADE" = "1" ]; then
89002ed2 3852 export LE_WORKING_DIR
3853 (
4c2a3841 3854 if ! upgrade; then
3855 _err "Cron:Upgrade failed!"
3856 return 1
3857 fi
89002ed2 3858 )
d5ec5f80 3859 . "$LE_WORKING_DIR/$PROJECT_ENTRY" >/dev/null
1ab63043 3860
4c2a3841 3861 if [ -t 1 ]; then
1ab63043 3862 __INTERACTIVE="1"
3863 fi
4c2a3841 3864
89002ed2 3865 _info "Auto upgraded to: $VER"
3866 fi
4c3b3608 3867 renewAll
cc179731 3868 _ret="$?"
281aa349 3869 IN_CRON=""
0ba95a3d 3870 exit $_ret
4c3b3608 3871}
3872
3873version() {
a63b05a9 3874 echo "$PROJECT"
3875 echo "v$VER"
4c3b3608 3876}
3877
3878showhelp() {
d0871bda 3879 _initpath
4c3b3608 3880 version
a7b7355d 3881 echo "Usage: $PROJECT_ENTRY command ...[parameters]....
a63b05a9 3882Commands:
3883 --help, -h Show this help message.
3884 --version, -v Show version info.
a7b7355d 3885 --install Install $PROJECT_NAME to your system.
3886 --uninstall Uninstall $PROJECT_NAME, and uninstall the cron job.
10afcaca 3887 --upgrade Upgrade $PROJECT_NAME to the latest code from $PROJECT .
a63b05a9 3888 --issue Issue a cert.
10afcaca 3889 --signcsr Issue a cert from an existing csr.
a61fe418 3890 --deploy Deploy the cert to your server.
a63b05a9 3891 --installcert Install the issued cert to apache/nginx or any other server.
3892 --renew, -r Renew a cert.
10afcaca 3893 --renewAll Renew all the certs.
a63b05a9 3894 --revoke Revoke a cert.
10afcaca 3895 --list List all the certs.
3896 --showcsr Show the content of a csr.
a63b05a9 3897 --installcronjob Install the cron job to renew certs, you don't need to call this. The 'install' command can automatically install the cron job.
3898 --uninstallcronjob Uninstall the cron job. The 'uninstall' command can do this automatically.
3899 --cron Run cron job to renew all the certs.
3900 --toPkcs Export the certificate and key to a pfx file.
eb59817e 3901 --updateaccount Update account info.
3902 --registeraccount Register account key.
a63b05a9 3903 --createAccountKey, -cak Create an account private key, professional use.
3904 --createDomainKey, -cdk Create an domain private key, professional use.
3905 --createCSR, -ccsr Create CSR , professional use.
0c00e870 3906 --deactivate Deactivate the domain authz, professional use.
a63b05a9 3907
3908Parameters:
3909 --domain, -d domain.tld Specifies a domain, used to issue, renew or revoke etc.
3910 --force, -f Used to force to install or force to renew a cert immediately.
3911 --staging, --test Use staging server, just for test.
3912 --debug Output debug info.
3913
3914 --webroot, -w /path/to/webroot Specifies the web root folder for web root mode.
3915 --standalone Use standalone mode.
e22bcf7c 3916 --tls Use standalone tls mode.
a63b05a9 3917 --apache Use apache mode.
eccec5f6 3918 --dns [dns_cf|dns_dp|dns_cx|/path/to/api/file] Use dns mode or dns api.
4a4dacb5 3919 --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 3920
3921 --keylength, -k [2048] Specifies the domain key length: 2048, 3072, 4096, 8192 or ec-256, ec-384.
3922 --accountkeylength, -ak [2048] Specifies the account key length.
d0871bda 3923 --log [/path/to/logfile] Specifies the log file. The default is: \"$DEFAULT_LOG_FILE\" if you don't give a file path here.
a73c5b33 3924 --log-level 1|2 Specifies the log level, default is 1.
a63b05a9 3925
3926 These parameters are to install the cert to nginx/apache or anyother server after issue/renew a cert:
3927
3928 --certpath /path/to/real/cert/file After issue/renew, the cert will be copied to this path.
3929 --keypath /path/to/real/key/file After issue/renew, the key will be copied to this path.
3930 --capath /path/to/real/ca/file After issue/renew, the intermediate cert will be copied to this path.
3931 --fullchainpath /path/to/fullchain/file After issue/renew, the fullchain cert will be copied to this path.
3932
3933 --reloadcmd \"service nginx reload\" After issue/renew, it's used to reload the server.
3934
3935 --accountconf Specifies a customized account config file.
635695ec 3936 --home Specifies the home dir for $PROJECT_NAME .
39c8f79f 3937 --certhome Specifies the home dir to save all the certs, only valid for '--install' command.
635695ec 3938 --useragent Specifies the user agent string. it will be saved for future use too.
b5eb4b90 3939 --accountemail Specifies the account email for registering, Only valid for the '--install' command.
06625071 3940 --accountkey Specifies the account key path, Only valid for the '--install' command.
523c7682 3941 --days Specifies the days to renew the cert when using '--issue' command. The max value is $MAX_RENEW days.
39c8f79f 3942 --httpport Specifies the standalone listening port. Only valid if the server is behind a reverse proxy or load balancer.
e22bcf7c 3943 --tlsport Specifies the standalone tls listening port. Only valid if the server is behind a reverse proxy or load balancer.
6ae0f7f5 3944 --local-address Specifies the standalone/tls server listening address, in case you have multiple ip addresses.
dcf4f8f6 3945 --listraw Only used for '--list' command, list the certs in raw format.
c8e9a31e 3946 --stopRenewOnError, -se Only valid for '--renewall' command. Stop if one cert has error in renewal.
13d7cae9 3947 --insecure Do not check the server certificate, in some devices, the api server's certificate may not be trusted.
78009539 3948 --ca-bundle Specifices the path to the CA certificate bundle to verify api server's certificate.
bc96082f 3949 --nocron Only valid for '--install' command, which means: do not install the default cron job. In this case, the certs will not be renewed automatically.
43822d37 3950 --ecc Specifies to use the ECC cert. Valid for '--installcert', '--renew', '--revoke', '--toPkcs' and '--createCSR'
10afcaca 3951 --csr Specifies the input csr.
b0070f03 3952 --pre-hook Command to be run before obtaining any certificates.
3953 --post-hook Command to be run after attempting to obtain/renew certificates. No matter the obain/renew is success or failed.
3954 --renew-hook Command to be run once for each successfully renewed certificate.
a61fe418 3955 --deploy-hook The hook file to deploy cert
0c9546cc 3956 --ocsp-must-staple, --ocsp Generate ocsp must Staple extension.
6bf281f9 3957 --auto-upgrade [0|1] Valid for '--upgrade' command, indicating whether to upgrade automatically in future.
6ae0f7f5 3958 --listen-v4 Force standalone/tls server to listen at ipv4.
3959 --listen-v6 Force standalone/tls server to listen at ipv6.
a746139c 3960 --openssl-bin Specifies a custom openssl bin location.
4c3b3608 3961 "
3962}
3963
52677b0a 3964# nocron
4a0f23e2 3965_installOnline() {
3966 _info "Installing from online archive."
52677b0a 3967 _nocron="$1"
4c2a3841 3968 if [ ! "$BRANCH" ]; then
4a0f23e2 3969 BRANCH="master"
3970 fi
a8df88ab 3971
4a0f23e2 3972 target="$PROJECT/archive/$BRANCH.tar.gz"
3973 _info "Downloading $target"
3974 localname="$BRANCH.tar.gz"
4c2a3841 3975 if ! _get "$target" >$localname; then
df9547ae 3976 _err "Download error."
4a0f23e2 3977 return 1
3978 fi
0bbe6eef 3979 (
4c2a3841 3980 _info "Extracting $localname"
3a3b0dd5 3981 if ! (tar xzf $localname || gtar xzf $localname); then
3982 _err "Extraction error."
3983 exit 1
3984 fi
4c2a3841 3985
3986 cd "$PROJECT_NAME-$BRANCH"
3987 chmod +x $PROJECT_ENTRY
3988 if ./$PROJECT_ENTRY install "$_nocron"; then
3989 _info "Install success!"
3990 fi
3991
3992 cd ..
3993
3994 rm -rf "$PROJECT_NAME-$BRANCH"
3995 rm -f "$localname"
0bbe6eef 3996 )
4a0f23e2 3997}
3998
52677b0a 3999upgrade() {
4000 if (
267f283a 4001 _initpath
4002 export LE_WORKING_DIR
d0b748a4 4003 cd "$LE_WORKING_DIR"
52677b0a 4004 _installOnline "nocron"
4c2a3841 4005 ); then
52677b0a 4006 _info "Upgrade success!"
096d8992 4007 exit 0
52677b0a 4008 else
4009 _err "Upgrade failed!"
096d8992 4010 exit 1
52677b0a 4011 fi
4012}
a63b05a9 4013
5ea6e9c9 4014_processAccountConf() {
4c2a3841 4015 if [ "$_useragent" ]; then
5ea6e9c9 4016 _saveaccountconf "USER_AGENT" "$_useragent"
4c2a3841 4017 elif [ "$USER_AGENT" ] && [ "$USER_AGENT" != "$DEFAULT_USER_AGENT" ]; then
d0871bda 4018 _saveaccountconf "USER_AGENT" "$USER_AGENT"
5ea6e9c9 4019 fi
4c2a3841 4020
4021 if [ "$_accountemail" ]; then
5ea6e9c9 4022 _saveaccountconf "ACCOUNT_EMAIL" "$_accountemail"
4c2a3841 4023 elif [ "$ACCOUNT_EMAIL" ] && [ "$ACCOUNT_EMAIL" != "$DEFAULT_ACCOUNT_EMAIL" ]; then
d0871bda 4024 _saveaccountconf "ACCOUNT_EMAIL" "$ACCOUNT_EMAIL"
5ea6e9c9 4025 fi
4c2a3841 4026
a746139c 4027 if [ "$_openssl_bin" ]; then
4028 _saveaccountconf "OPENSSL_BIN" "$_openssl_bin"
4029 elif [ "$OPENSSL_BIN" ] && [ "$OPENSSL_BIN" != "$DEFAULT_OPENSSL_BIN" ]; then
4030 _saveaccountconf "OPENSSL_BIN" "$OPENSSL_BIN"
4031 fi
4032
4c2a3841 4033 if [ "$_auto_upgrade" ]; then
6bf281f9 4034 _saveaccountconf "AUTO_UPGRADE" "$_auto_upgrade"
4c2a3841 4035 elif [ "$AUTO_UPGRADE" ]; then
6bf281f9 4036 _saveaccountconf "AUTO_UPGRADE" "$AUTO_UPGRADE"
4037 fi
4c2a3841 4038
5ea6e9c9 4039}
4040
a63b05a9 4041_process() {
4042 _CMD=""
4043 _domain=""
3f4513b3 4044 _altdomains="$NO_VALUE"
a63b05a9 4045 _webroot=""
bdbf323f 4046 _keylength=""
4047 _accountkeylength=""
4048 _certpath=""
4049 _keypath=""
4050 _capath=""
4051 _fullchainpath=""
4d2f38b0 4052 _reloadcmd=""
a63b05a9 4053 _password=""
635695ec 4054 _accountconf=""
4055 _useragent=""
b5eb4b90 4056 _accountemail=""
4057 _accountkey=""
b2817897 4058 _certhome=""
39c8f79f 4059 _httpport=""
e22bcf7c 4060 _tlsport=""
0e38c60d 4061 _dnssleep=""
dcf4f8f6 4062 _listraw=""
cc179731 4063 _stopRenewOnError=""
e3698edd 4064 #_insecure=""
78009539 4065 _ca_bundle=""
c8e9a31e 4066 _nocron=""
43822d37 4067 _ecc=""
10afcaca 4068 _csr=""
b0070f03 4069 _pre_hook=""
4070 _post_hook=""
4071 _renew_hook=""
a61fe418 4072 _deploy_hook=""
5ea6e9c9 4073 _logfile=""
d0871bda 4074 _log=""
0463b5d6 4075 _local_address=""
a73c5b33 4076 _log_level=""
6bf281f9 4077 _auto_upgrade=""
6ae0f7f5 4078 _listen_v4=""
4079 _listen_v6=""
a746139c 4080 _openssl_bin=""
4c2a3841 4081 while [ ${#} -gt 0 ]; do
a63b05a9 4082 case "${1}" in
4c2a3841 4083
4084 --help | -h)
a63b05a9 4085 showhelp
4086 return
4087 ;;
4c2a3841 4088 --version | -v)
a63b05a9 4089 version
4090 return
4091 ;;
4c2a3841 4092 --install)
a63b05a9 4093 _CMD="install"
4094 ;;
4c2a3841 4095 --uninstall)
a63b05a9 4096 _CMD="uninstall"
4097 ;;
4c2a3841 4098 --upgrade)
52677b0a 4099 _CMD="upgrade"
4100 ;;
4c2a3841 4101 --issue)
a63b05a9 4102 _CMD="issue"
4103 ;;
4c2a3841 4104 --deploy)
a61fe418 4105 _CMD="deploy"
4106 ;;
4c2a3841 4107 --signcsr)
10afcaca 4108 _CMD="signcsr"
4109 ;;
4c2a3841 4110 --showcsr)
10afcaca 4111 _CMD="showcsr"
4112 ;;
4c2a3841 4113 --installcert | -i)
a63b05a9 4114 _CMD="installcert"
4115 ;;
4c2a3841 4116 --renew | -r)
a63b05a9 4117 _CMD="renew"
4118 ;;
4c2a3841 4119 --renewAll | --renewall)
a63b05a9 4120 _CMD="renewAll"
4121 ;;
4c2a3841 4122 --revoke)
a63b05a9 4123 _CMD="revoke"
4124 ;;
4c2a3841 4125 --list)
6d7eda3e 4126 _CMD="list"
4127 ;;
4c2a3841 4128 --installcronjob)
a63b05a9 4129 _CMD="installcronjob"
4130 ;;
4c2a3841 4131 --uninstallcronjob)
a63b05a9 4132 _CMD="uninstallcronjob"
4133 ;;
4c2a3841 4134 --cron)
a63b05a9 4135 _CMD="cron"
4136 ;;
4c2a3841 4137 --toPkcs)
a63b05a9 4138 _CMD="toPkcs"
4c2a3841 4139 ;;
4140 --createAccountKey | --createaccountkey | -cak)
a63b05a9 4141 _CMD="createAccountKey"
4142 ;;
4c2a3841 4143 --createDomainKey | --createdomainkey | -cdk)
a63b05a9 4144 _CMD="createDomainKey"
4145 ;;
4c2a3841 4146 --createCSR | --createcsr | -ccr)
a63b05a9 4147 _CMD="createCSR"
4148 ;;
4c2a3841 4149 --deactivate)
0c00e870 4150 _CMD="deactivate"
4151 ;;
4c2a3841 4152 --updateaccount)
eb59817e 4153 _CMD="updateaccount"
4154 ;;
4c2a3841 4155 --registeraccount)
eb59817e 4156 _CMD="registeraccount"
4157 ;;
4c2a3841 4158 --domain | -d)
a63b05a9 4159 _dvalue="$2"
4c2a3841 4160
4161 if [ "$_dvalue" ]; then
4162 if _startswith "$_dvalue" "-"; then
ee1737a5 4163 _err "'$_dvalue' is not a valid domain for parameter '$1'"
4164 return 1
4165 fi
4c2a3841 4166 if _is_idn "$_dvalue" && ! _exists idn; then
9774b01b 4167 _err "It seems that $_dvalue is an IDN( Internationalized Domain Names), please install 'idn' command first."
4168 return 1
4169 fi
4c2a3841 4170
4171 if [ -z "$_domain" ]; then
ee1737a5 4172 _domain="$_dvalue"
a63b05a9 4173 else
4c2a3841 4174 if [ "$_altdomains" = "$NO_VALUE" ]; then
ee1737a5 4175 _altdomains="$_dvalue"
4176 else
4177 _altdomains="$_altdomains,$_dvalue"
4178 fi
a63b05a9 4179 fi
4180 fi
4c2a3841 4181
a63b05a9 4182 shift
4183 ;;
4184
4c2a3841 4185 --force | -f)
a63b05a9 4186 FORCE="1"
4187 ;;
4c2a3841 4188 --staging | --test)
a63b05a9 4189 STAGE="1"
4190 ;;
4c2a3841 4191 --debug)
4192 if [ -z "$2" ] || _startswith "$2" "-"; then
a63b05a9 4193 DEBUG="1"
4194 else
4195 DEBUG="$2"
4196 shift
4c2a3841 4197 fi
a63b05a9 4198 ;;
4c2a3841 4199 --webroot | -w)
a63b05a9 4200 wvalue="$2"
4c2a3841 4201 if [ -z "$_webroot" ]; then
a63b05a9 4202 _webroot="$wvalue"
4203 else
4204 _webroot="$_webroot,$wvalue"
4205 fi
4206 shift
4c2a3841 4207 ;;
4208 --standalone)
3f4513b3 4209 wvalue="$NO_VALUE"
4c2a3841 4210 if [ -z "$_webroot" ]; then
a63b05a9 4211 _webroot="$wvalue"
4212 else
4213 _webroot="$_webroot,$wvalue"
4214 fi
4215 ;;
4c2a3841 4216 --local-address)
0463b5d6 4217 lvalue="$2"
4218 _local_address="$_local_address$lvalue,"
4219 shift
4220 ;;
4c2a3841 4221 --apache)
a63b05a9 4222 wvalue="apache"
4c2a3841 4223 if [ -z "$_webroot" ]; then
a63b05a9 4224 _webroot="$wvalue"
4225 else
4226 _webroot="$_webroot,$wvalue"
4227 fi
4228 ;;
4c2a3841 4229 --tls)
e22bcf7c 4230 wvalue="$W_TLS"
4c2a3841 4231 if [ -z "$_webroot" ]; then
e22bcf7c 4232 _webroot="$wvalue"
4233 else
4234 _webroot="$_webroot,$wvalue"
4235 fi
4236 ;;
4c2a3841 4237 --dns)
a63b05a9 4238 wvalue="dns"
4c2a3841 4239 if ! _startswith "$2" "-"; then
a63b05a9 4240 wvalue="$2"
4241 shift
4242 fi
4c2a3841 4243 if [ -z "$_webroot" ]; then
a63b05a9 4244 _webroot="$wvalue"
4245 else
4246 _webroot="$_webroot,$wvalue"
4247 fi
4248 ;;
4c2a3841 4249 --dnssleep)
0e38c60d 4250 _dnssleep="$2"
4251 Le_DNSSleep="$_dnssleep"
4252 shift
4253 ;;
4c2a3841 4254
4255 --keylength | -k)
a63b05a9 4256 _keylength="$2"
a63b05a9 4257 shift
4258 ;;
4c2a3841 4259 --accountkeylength | -ak)
2ce87fe2 4260 _accountkeylength="$2"
a63b05a9 4261 shift
4262 ;;
4263
4c2a3841 4264 --certpath)
a63b05a9 4265 _certpath="$2"
4266 shift
4267 ;;
4c2a3841 4268 --keypath)
a63b05a9 4269 _keypath="$2"
4270 shift
4271 ;;
4c2a3841 4272 --capath)
a63b05a9 4273 _capath="$2"
4274 shift
4275 ;;
4c2a3841 4276 --fullchainpath)
a63b05a9 4277 _fullchainpath="$2"
4278 shift
4279 ;;
4c2a3841 4280 --reloadcmd | --reloadCmd)
a63b05a9 4281 _reloadcmd="$2"
4282 shift
4283 ;;
4c2a3841 4284 --password)
a63b05a9 4285 _password="$2"
4286 shift
4287 ;;
4c2a3841 4288 --accountconf)
635695ec 4289 _accountconf="$2"
4290 ACCOUNT_CONF_PATH="$_accountconf"
a7b7355d 4291 shift
a63b05a9 4292 ;;
4c2a3841 4293 --home)
a63b05a9 4294 LE_WORKING_DIR="$2"
a7b7355d 4295 shift
a63b05a9 4296 ;;
4c2a3841 4297 --certhome)
b2817897 4298 _certhome="$2"
4299 CERT_HOME="$_certhome"
4300 shift
4c2a3841 4301 ;;
4302 --useragent)
635695ec 4303 _useragent="$2"
4304 USER_AGENT="$_useragent"
4305 shift
4306 ;;
4c2a3841 4307 --accountemail)
b5eb4b90 4308 _accountemail="$2"
4309 ACCOUNT_EMAIL="$_accountemail"
4310 shift
4311 ;;
4c2a3841 4312 --accountkey)
b5eb4b90 4313 _accountkey="$2"
4314 ACCOUNT_KEY_PATH="$_accountkey"
4315 shift
4316 ;;
4c2a3841 4317 --days)
06625071 4318 _days="$2"
4319 Le_RenewalDays="$_days"
4320 shift
4321 ;;
4c2a3841 4322 --httpport)
39c8f79f 4323 _httpport="$2"
4324 Le_HTTPPort="$_httpport"
4325 shift
4326 ;;
4c2a3841 4327 --tlsport)
e22bcf7c 4328 _tlsport="$2"
4329 Le_TLSPort="$_tlsport"
4330 shift
4331 ;;
4c2a3841 4332
4333 --listraw)
dcf4f8f6 4334 _listraw="raw"
4c2a3841 4335 ;;
4336 --stopRenewOnError | --stoprenewonerror | -se)
cc179731 4337 _stopRenewOnError="1"
4338 ;;
4c2a3841 4339 --insecure)
e3698edd 4340 #_insecure="1"
fac1e367 4341 HTTPS_INSECURE="1"
13d7cae9 4342 ;;
4c2a3841 4343 --ca-bundle)
d5ec5f80 4344 _ca_bundle="$(readlink -f "$2")"
78009539
PS
4345 CA_BUNDLE="$_ca_bundle"
4346 shift
4347 ;;
4c2a3841 4348 --nocron)
c8e9a31e 4349 _nocron="1"
4350 ;;
4c2a3841 4351 --ecc)
43822d37 4352 _ecc="isEcc"
4353 ;;
4c2a3841 4354 --csr)
10afcaca 4355 _csr="$2"
4356 shift
4357 ;;
4c2a3841 4358 --pre-hook)
b0070f03 4359 _pre_hook="$2"
4360 shift
4361 ;;
4c2a3841 4362 --post-hook)
b0070f03 4363 _post_hook="$2"
4364 shift
4365 ;;
4c2a3841 4366 --renew-hook)
b0070f03 4367 _renew_hook="$2"
4368 shift
4369 ;;
4c2a3841 4370 --deploy-hook)
a61fe418 4371 _deploy_hook="$2"
4372 shift
4373 ;;
4c2a3841 4374 --ocsp-must-staple | --ocsp)
96db9362 4375 Le_OCSP_Staple="1"
0c9546cc 4376 ;;
4c2a3841 4377 --log | --logfile)
d0871bda 4378 _log="1"
5ea6e9c9 4379 _logfile="$2"
4c2a3841 4380 if _startswith "$_logfile" '-'; then
d0871bda 4381 _logfile=""
4382 else
4383 shift
4384 fi
5ea6e9c9 4385 LOG_FILE="$_logfile"
4c2a3841 4386 if [ -z "$LOG_LEVEL" ]; then
a73c5b33 4387 LOG_LEVEL="$DEFAULT_LOG_LEVEL"
4388 fi
4389 ;;
4c2a3841 4390 --log-level)
30bfc2ce 4391 _log_level="$2"
a73c5b33 4392 LOG_LEVEL="$_log_level"
4393 shift
5ea6e9c9 4394 ;;
4c2a3841 4395 --auto-upgrade)
6bf281f9 4396 _auto_upgrade="$2"
4c2a3841 4397 if [ -z "$_auto_upgrade" ] || _startswith "$_auto_upgrade" '-'; then
6bf281f9 4398 _auto_upgrade="1"
4399 else
4400 shift
4401 fi
4402 AUTO_UPGRADE="$_auto_upgrade"
4403 ;;
4c2a3841 4404 --listen-v4)
6ae0f7f5 4405 _listen_v4="1"
4406 Le_Listen_V4="$_listen_v4"
4407 ;;
4c2a3841 4408 --listen-v6)
6ae0f7f5 4409 _listen_v6="1"
4410 Le_Listen_V6="$_listen_v6"
4411 ;;
a746139c 4412 --openssl-bin)
4413 _openssl_bin="$2"
4414 OPENSSL_BIN="$_openssl_bin"
4415 ;;
4c2a3841 4416 *)
a63b05a9 4417 _err "Unknown parameter : $1"
4418 return 1
4419 ;;
4420 esac
4421
4422 shift 1
4423 done
4424
4c2a3841 4425 if [ "${_CMD}" != "install" ]; then
5ea6e9c9 4426 __initHome
661f0583 4427 if [ "$_log" ]; then
4c2a3841 4428 if [ -z "$_logfile" ]; then
661f0583 4429 _logfile="$DEFAULT_LOG_FILE"
4430 fi
d0871bda 4431 fi
4c2a3841 4432 if [ "$_logfile" ]; then
5ea6e9c9 4433 _saveaccountconf "LOG_FILE" "$_logfile"
661f0583 4434 LOG_FILE="$_logfile"
5ea6e9c9 4435 fi
a73c5b33 4436
4c2a3841 4437 if [ "$_log_level" ]; then
a73c5b33 4438 _saveaccountconf "LOG_LEVEL" "$_log_level"
4439 LOG_LEVEL="$_log_level"
4440 fi
4c2a3841 4441
5ea6e9c9 4442 _processAccountConf
4443 fi
4c2a3841 4444
9d548d81 4445 _debug2 LE_WORKING_DIR "$LE_WORKING_DIR"
4c2a3841 4446
4447 if [ "$DEBUG" ]; then
dcf9cb58 4448 version
4449 fi
a63b05a9 4450
4451 case "${_CMD}" in
c8e9a31e 4452 install) install "$_nocron" ;;
bc96082f 4453 uninstall) uninstall "$_nocron" ;;
52677b0a 4454 upgrade) upgrade ;;
a63b05a9 4455 issue)
4c2a3841 4456 issue "$_webroot" "$_domain" "$_altdomains" "$_keylength" "$_certpath" "$_keypath" "$_capath" "$_reloadcmd" "$_fullchainpath" "$_pre_hook" "$_post_hook" "$_renew_hook" "$_local_address"
a63b05a9 4457 ;;
a61fe418 4458 deploy)
4459 deploy "$_domain" "$_deploy_hook" "$_ecc"
4460 ;;
10afcaca 4461 signcsr)
4462 signcsr "$_csr" "$_webroot"
4463 ;;
4464 showcsr)
4465 showcsr "$_csr" "$_domain"
4466 ;;
a63b05a9 4467 installcert)
43822d37 4468 installcert "$_domain" "$_certpath" "$_keypath" "$_capath" "$_reloadcmd" "$_fullchainpath" "$_ecc"
a63b05a9 4469 ;;
4c2a3841 4470 renew)
43822d37 4471 renew "$_domain" "$_ecc"
a63b05a9 4472 ;;
4c2a3841 4473 renewAll)
cc179731 4474 renewAll "$_stopRenewOnError"
a63b05a9 4475 ;;
4c2a3841 4476 revoke)
43822d37 4477 revoke "$_domain" "$_ecc"
a63b05a9 4478 ;;
4c2a3841 4479 deactivate)
3f4513b3 4480 deactivate "$_domain,$_altdomains"
eb59817e 4481 ;;
4c2a3841 4482 registeraccount)
57e58ce7 4483 registeraccount "$_accountkeylength"
eb59817e 4484 ;;
4c2a3841 4485 updateaccount)
eb59817e 4486 updateaccount
4487 ;;
4c2a3841 4488 list)
dcf4f8f6 4489 list "$_listraw"
6d7eda3e 4490 ;;
a63b05a9 4491 installcronjob) installcronjob ;;
4492 uninstallcronjob) uninstallcronjob ;;
4493 cron) cron ;;
4c2a3841 4494 toPkcs)
43822d37 4495 toPkcs "$_domain" "$_password" "$_ecc"
a63b05a9 4496 ;;
4c2a3841 4497 createAccountKey)
5fbc47eb 4498 createAccountKey "$_accountkeylength"
a63b05a9 4499 ;;
4c2a3841 4500 createDomainKey)
a63b05a9 4501 createDomainKey "$_domain" "$_keylength"
4502 ;;
4c2a3841 4503 createCSR)
43822d37 4504 createCSR "$_domain" "$_altdomains" "$_ecc"
a63b05a9 4505 ;;
4506
4507 *)
4508 _err "Invalid command: $_CMD"
4c2a3841 4509 showhelp
a63b05a9 4510 return 1
4c2a3841 4511 ;;
a63b05a9 4512 esac
d3595686 4513 _ret="$?"
4c2a3841 4514 if [ "$_ret" != "0" ]; then
d3595686 4515 return $_ret
4516 fi
4c2a3841 4517
4518 if [ "${_CMD}" = "install" ]; then
4519 if [ "$_log" ]; then
4520 if [ -z "$LOG_FILE" ]; then
d0871bda 4521 LOG_FILE="$DEFAULT_LOG_FILE"
4522 fi
4523 _saveaccountconf "LOG_FILE" "$LOG_FILE"
5ea6e9c9 4524 fi
4c2a3841 4525
4526 if [ "$_log_level" ]; then
a73c5b33 4527 _saveaccountconf "LOG_LEVEL" "$_log_level"
4528 fi
5ea6e9c9 4529 _processAccountConf
b5eb4b90 4530 fi
635695ec 4531
a63b05a9 4532}
4533
4c2a3841 4534if [ "$INSTALLONLINE" ]; then
d1f97fc8 4535 INSTALLONLINE=""
4a0f23e2 4536 _installOnline $BRANCH
4537 exit
4538fi
4c3b3608 4539
319e0ae3 4540main() {
4541 [ -z "$1" ] && showhelp && return
4c2a3841 4542 if _startswith "$1" '-'; then _process "$@"; else "$@"; fi
319e0ae3 4543}
e69a7c38 4544
aa7b82de 4545main "$@"