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