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