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