]> git.proxmox.com Git - mirror_acme.sh.git/blame - acme.sh
add retry for temp authz request error
[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
3afa4b21 2285 _Max_new_authz_retry_times=5
2286 _authz_i=0
2287 while [ "$_authz_i" -lt "$_Max_new_authz_retry_times" ] ; do
2288 _info "Try new-authz for the $_authz_i time."
2289 if ! _send_signed_request "$API/acme/new-authz" "{\"resource\": \"new-authz\", \"identifier\": {\"type\": \"dns\", \"value\": \"$(_idn "$d")\"}}" ; then
2290 _err "Can not get domain token."
2291 _clearup
2292 _on_issue_err
2293 return 1
2294 fi
2295 if ! _contains "$response" "An error occurred while processing your request" ; then
2296 _info "The new-authz request is ok."
2297 break
2298 fi
2299 _authz_i="$(_math "$_authz_i" + 1)"
2300 _info "Sleep $_authz_i to retry."
2301 _sleep "$_authz_i"
2302 done;
2303
2304 if [ "$_authz_i" = "$_Max_new_authz_retry_times" ] ; then
2305 _debug "new-authz retry reach the max $_Max_new_authz_retry_times times."
c4d8fd83 2306 fi
2307
8663fb7e 2308 if [ ! -z "$code" ] && [ ! "$code" = '201' ] ; then
4c3b3608 2309 _err "new-authz error: $response"
2310 _clearup
b0070f03 2311 _on_issue_err
4c3b3608 2312 return 1
2313 fi
2314
fdcb6b72 2315 entry="$(printf "%s\n" "$response" | _egrep_o '[^\{]*"type":"'$vtype'"[^\}]*')"
4c3b3608 2316 _debug entry "$entry"
19539575 2317 if [ -z "$entry" ] ; then
2318 _err "Error, can not get domain token $d"
2319 _clearup
b0070f03 2320 _on_issue_err
19539575 2321 return 1
2322 fi
22ea4004 2323 token="$(printf "%s\n" "$entry" | _egrep_o '"token":"[^"]*' | cut -d : -f 2 | tr -d '"')"
4c3b3608 2324 _debug token $token
2325
22ea4004 2326 uri="$(printf "%s\n" "$entry" | _egrep_o '"uri":"[^"]*'| cut -d : -f 2,3 | tr -d '"' )"
4c3b3608 2327 _debug uri $uri
2328
2329 keyauthorization="$token.$thumbprint"
2330 _debug keyauthorization "$keyauthorization"
2331
d35bf517 2332
2333 if printf "$response" | grep '"status":"valid"' >/dev/null 2>&1 ; then
2334 _info "$d is already verified, skip."
2335 keyauthorization=$STATE_VERIFIED
2336 _debug keyauthorization "$keyauthorization"
ec603bee 2337 fi
2338
d35bf517 2339
a63b05a9 2340 dvlist="$d$sep$keyauthorization$sep$uri$sep$vtype$sep$_currentRoot"
4c3b3608 2341 _debug dvlist "$dvlist"
2342
2343 vlist="$vlist$dvlist,"
2344
2345 done
2346
2347 #add entry
2348 dnsadded=""
2349 ventries=$(echo "$vlist" | tr ',' ' ' )
2350 for ventry in $ventries
2351 do
2352 d=$(echo $ventry | cut -d $sep -f 1)
2353 keyauthorization=$(echo $ventry | cut -d $sep -f 2)
a63b05a9 2354 vtype=$(echo $ventry | cut -d $sep -f 4)
2355 _currentRoot=$(echo $ventry | cut -d $sep -f 5)
ec603bee 2356
bd5e57d8 2357 if [ "$keyauthorization" = "$STATE_VERIFIED" ] ; then
ec603bee 2358 _info "$d is already verified, skip $vtype."
2359 continue
2360 fi
2361
8663fb7e 2362 if [ "$vtype" = "$VTYPE_DNS" ] ; then
4c3b3608 2363 dnsadded='0'
2364 txtdomain="_acme-challenge.$d"
2365 _debug txtdomain "$txtdomain"
22ea4004 2366 txt="$(printf "%s" "$keyauthorization" | _digest "sha256" | _urlencode)"
4c3b3608 2367 _debug txt "$txt"
a61fe418 2368
2369 d_api="$(_findHook $d dnsapi $_currentRoot)"
2370
4c3b3608 2371 _debug d_api "$d_api"
2372
8663fb7e 2373 if [ "$d_api" ] ; then
4c3b3608 2374 _info "Found domain api file: $d_api"
2375 else
2376 _err "Add the following TXT record:"
cbcd7e0f 2377 _err "Domain: '$(__green $txtdomain)'"
2378 _err "TXT value: '$(__green $txt)'"
4c3b3608 2379 _err "Please be aware that you prepend _acme-challenge. before your domain"
2380 _err "so the resulting subdomain will be: $txtdomain"
2381 continue
2382 fi
4c3b3608 2383
73b8b120 2384 (
8663fb7e 2385 if ! . $d_api ; then
73b8b120 2386 _err "Load file $d_api error. Please check your api file and try again."
2387 return 1
2388 fi
2389
158f22f7 2390 addcommand="${_currentRoot}_add"
d53289d7 2391 if ! _exists $addcommand ; then
73b8b120 2392 _err "It seems that your api file is not correct, it must have a function named: $addcommand"
2393 return 1
2394 fi
2395
2396 if ! $addcommand $txtdomain $txt ; then
2397 _err "Error add txt for domain:$txtdomain"
2398 return 1
2399 fi
2400 )
4c3b3608 2401
8663fb7e 2402 if [ "$?" != "0" ] ; then
5ef501c5 2403 _clearup
b0070f03 2404 _on_issue_err
4c3b3608 2405 return 1
2406 fi
2407 dnsadded='1'
2408 fi
2409 done
2410
8663fb7e 2411 if [ "$dnsadded" = '0' ] ; then
4d2f38b0 2412 _savedomainconf "Le_Vlist" "$vlist"
4c3b3608 2413 _debug "Dns record not added yet, so, save to $DOMAIN_CONF and exit."
2414 _err "Please add the TXT records to the domains, and retry again."
5ef501c5 2415 _clearup
b0070f03 2416 _on_issue_err
4c3b3608 2417 return 1
2418 fi
2419
2420 fi
2421
8663fb7e 2422 if [ "$dnsadded" = '1' ] ; then
0e38c60d 2423 if [ -z "$Le_DNSSleep" ] ; then
4a4dacb5 2424 Le_DNSSleep=$DEFAULT_DNS_SLEEP
0e38c60d 2425 else
2426 _savedomainconf "Le_DNSSleep" "$Le_DNSSleep"
2427 fi
2428
5fbc47eb 2429 _info "Sleep $(__green $Le_DNSSleep) seconds for the txt records to take effect"
fdcb6b72 2430 _sleep $Le_DNSSleep
4c3b3608 2431 fi
2432
2433 _debug "ok, let's start to verify"
a63b05a9 2434
0463b5d6 2435 _ncIndex=1
4c3b3608 2436 ventries=$(echo "$vlist" | tr ',' ' ' )
2437 for ventry in $ventries
2438 do
2439 d=$(echo $ventry | cut -d $sep -f 1)
2440 keyauthorization=$(echo $ventry | cut -d $sep -f 2)
2441 uri=$(echo $ventry | cut -d $sep -f 3)
a63b05a9 2442 vtype=$(echo $ventry | cut -d $sep -f 4)
2443 _currentRoot=$(echo $ventry | cut -d $sep -f 5)
ec603bee 2444
bd5e57d8 2445 if [ "$keyauthorization" = "$STATE_VERIFIED" ] ; then
ec603bee 2446 _info "$d is already verified, skip $vtype."
2447 continue
2448 fi
2449
4c3b3608 2450 _info "Verifying:$d"
2451 _debug "d" "$d"
2452 _debug "keyauthorization" "$keyauthorization"
2453 _debug "uri" "$uri"
2454 removelevel=""
e22bcf7c 2455 token="$(printf "%s" "$keyauthorization" | cut -d '.' -f 1)"
a63b05a9 2456
2457 _debug "_currentRoot" "$_currentRoot"
2458
2459
8663fb7e 2460 if [ "$vtype" = "$VTYPE_HTTP" ] ; then
3f4513b3 2461 if [ "$_currentRoot" = "$NO_VALUE" ] ; then
4c3b3608 2462 _info "Standalone mode server"
0463b5d6 2463 _ncaddr="$(_getfield "$Le_LocalAddress" "$_ncIndex" )"
2464 _ncIndex="$(_math $_ncIndex + 1)"
2465 _startserver "$keyauthorization" "$_ncaddr" &
8663fb7e 2466 if [ "$?" != "0" ] ; then
5ef501c5 2467 _clearup
b0070f03 2468 _on_issue_err
6fc1447f 2469 return 1
2470 fi
4c3b3608 2471 serverproc="$!"
2472 sleep 2
2473 _debug serverproc $serverproc
6fc1447f 2474
4c3b3608 2475 else
8663fb7e 2476 if [ "$_currentRoot" = "apache" ] ; then
6f930641 2477 wellknown_path="$ACME_DIR"
2478 else
a63b05a9 2479 wellknown_path="$_currentRoot/.well-known/acme-challenge"
8663fb7e 2480 if [ ! -d "$_currentRoot/.well-known" ] ; then
6f930641 2481 removelevel='1'
8663fb7e 2482 elif [ ! -d "$_currentRoot/.well-known/acme-challenge" ] ; then
6f930641 2483 removelevel='2'
2484 else
2485 removelevel='3'
2486 fi
4c3b3608 2487 fi
6f930641 2488
4c3b3608 2489 _debug wellknown_path "$wellknown_path"
6f930641 2490
4c3b3608 2491 _debug "writing token:$token to $wellknown_path/$token"
2492
2493 mkdir -p "$wellknown_path"
7939b419 2494 printf "%s" "$keyauthorization" > "$wellknown_path/$token"
8663fb7e 2495 if [ ! "$usingApache" ] ; then
32fdc196
TB
2496 if webroot_owner=$(_stat $_currentRoot) ; then
2497 _debug "Changing owner/group of .well-known to $webroot_owner"
2498 chown -R $webroot_owner "$_currentRoot/.well-known"
2499 else
2500 _debug "not chaning owner/group of webroot";
2501 fi
df886ffa 2502 fi
4c3b3608 2503
2504 fi
e22bcf7c 2505
2506 elif [ "$vtype" = "$VTYPE_TLS" ] ; then
2507 #create A
2508 #_hash_A="$(printf "%s" $token | _digest "sha256" "hex" )"
2509 #_debug2 _hash_A "$_hash_A"
2510 #_x="$(echo $_hash_A | cut -c 1-32)"
2511 #_debug2 _x "$_x"
2512 #_y="$(echo $_hash_A | cut -c 33-64)"
2513 #_debug2 _y "$_y"
2514 #_SAN_A="$_x.$_y.token.acme.invalid"
2515 #_debug2 _SAN_A "$_SAN_A"
2516
2517 #create B
2518 _hash_B="$(printf "%s" $keyauthorization | _digest "sha256" "hex" )"
2519 _debug2 _hash_B "$_hash_B"
2520 _x="$(echo $_hash_B | cut -c 1-32)"
2521 _debug2 _x "$_x"
2522 _y="$(echo $_hash_B | cut -c 33-64)"
2523 _debug2 _y "$_y"
2524
2525 #_SAN_B="$_x.$_y.ka.acme.invalid"
2526
2527 _SAN_B="$_x.$_y.acme.invalid"
2528 _debug2 _SAN_B "$_SAN_B"
2529
0463b5d6 2530 _ncaddr="$(_getfield "$Le_LocalAddress" "$_ncIndex" )"
2531 _ncIndex="$(_math $_ncIndex + 1)"
2532 if ! _starttlsserver "$_SAN_B" "$_SAN_A" "$Le_TLSPort" "$keyauthorization" "$_ncaddr"; then
e22bcf7c 2533 _err "Start tls server error."
2534 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
2535 _clearup
b0070f03 2536 _on_issue_err
e22bcf7c 2537 return 1
2538 fi
4c3b3608 2539 fi
2540
c4d8fd83 2541 if ! _send_signed_request $uri "{\"resource\": \"challenge\", \"keyAuthorization\": \"$keyauthorization\"}" ; then
2542 _err "$d:Can not get challenge: $response"
2543 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
2544 _clearup
b0070f03 2545 _on_issue_err
c4d8fd83 2546 return 1
2547 fi
4c3b3608 2548
8663fb7e 2549 if [ ! -z "$code" ] && [ ! "$code" = '202' ] ; then
c60883ef 2550 _err "$d:Challenge error: $response"
a63b05a9 2551 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4c3b3608 2552 _clearup
b0070f03 2553 _on_issue_err
4c3b3608 2554 return 1
2555 fi
2556
6fc1447f 2557 waittimes=0
8663fb7e 2558 if [ -z "$MAX_RETRY_TIMES" ] ; then
6fc1447f 2559 MAX_RETRY_TIMES=30
2560 fi
2561
2ee5d873 2562 while true ; do
00a50605 2563 waittimes=$(_math $waittimes + 1)
8663fb7e 2564 if [ "$waittimes" -ge "$MAX_RETRY_TIMES" ] ; then
6fc1447f 2565 _err "$d:Timeout"
2566 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
2567 _clearup
b0070f03 2568 _on_issue_err
6fc1447f 2569 return 1
2570 fi
2571
4c3b3608 2572 _debug "sleep 5 secs to verify"
2573 sleep 5
2574 _debug "checking"
9aaf36cd 2575 response="$(_get $uri)"
8663fb7e 2576 if [ "$?" != "0" ] ; then
c60883ef 2577 _err "$d:Verify error:$response"
a63b05a9 2578 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4c3b3608 2579 _clearup
b0070f03 2580 _on_issue_err
4c3b3608 2581 return 1
2582 fi
9aaf36cd 2583 _debug2 original "$response"
2584
2585 response="$(echo "$response" | _normalizeJson )"
7012b91f 2586 _debug2 response "$response"
4c3b3608 2587
22ea4004 2588 status=$(echo "$response" | _egrep_o '"status":"[^"]*' | cut -d : -f 2 | tr -d '"')
8663fb7e 2589 if [ "$status" = "valid" ] ; then
4c3b3608 2590 _info "Success"
2591 _stopserver $serverproc
2592 serverproc=""
a63b05a9 2593 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4c3b3608 2594 break;
2595 fi
2596
8663fb7e 2597 if [ "$status" = "invalid" ] ; then
b15cfc2c 2598 error="$(echo "$response" | tr -d "\r\n" | _egrep_o '"error":\{[^\}]*')"
b7ec6789 2599 _debug2 error "$error"
b15cfc2c 2600 errordetail="$(echo "$error" | _egrep_o '"detail": *"[^"]*' | cut -d '"' -f 4)"
b7ec6789 2601 _debug2 errordetail "$errordetail"
2602 if [ "$errordetail" ] ; then
2603 _err "$d:Verify error:$errordetail"
2604 else
2605 _err "$d:Verify error:$error"
2606 fi
dcf9cb58 2607 if [ "$DEBUG" ] ; then
2608 if [ "$vtype" = "$VTYPE_HTTP" ] ; then
276b51d9 2609 _debug "Debug: get token url."
81f27e90 2610 _get "http://$d/.well-known/acme-challenge/$token" "" 1
dcf9cb58 2611 fi
2612 fi
a63b05a9 2613 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4c3b3608 2614 _clearup
b0070f03 2615 _on_issue_err
4c3b3608 2616 return 1;
2617 fi
2618
8663fb7e 2619 if [ "$status" = "pending" ] ; then
4c3b3608 2620 _info "Pending"
2621 else
2622 _err "$d:Verify error:$response"
a63b05a9 2623 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4c3b3608 2624 _clearup
b0070f03 2625 _on_issue_err
4c3b3608 2626 return 1
2627 fi
2628
2629 done
2630
2631 done
2632
2633 _clearup
2634 _info "Verify finished, start to sign."
fa8311dc 2635 der="$(_getfile "${CSR_PATH}" "${BEGIN_CSR}" "${END_CSR}" | tr -d "\r\n" | _urlencode)"
c4d8fd83 2636
2637 if ! _send_signed_request "$API/acme/new-cert" "{\"resource\": \"new-cert\", \"csr\": \"$der\"}" "needbase64" ; then
2638 _err "Sign failed."
b0070f03 2639 _on_issue_err
c4d8fd83 2640 return 1
2641 fi
4c3b3608 2642
d404e92d 2643 _rcert="$response"
c2c8f320 2644 Le_LinkCert="$(grep -i '^Location.*$' $HTTP_HEADER | _head_n 1 | tr -d "\r\n" | cut -d " " -f 2)"
4d2f38b0 2645 _savedomainconf "Le_LinkCert" "$Le_LinkCert"
4c3b3608 2646
8663fb7e 2647 if [ "$Le_LinkCert" ] ; then
88fab7d6 2648 echo "$BEGIN_CERT" > "$CERT_PATH"
d404e92d 2649
2650 if ! _get "$Le_LinkCert" | _base64 "multiline" >> "$CERT_PATH" ; then
2651 _debug "Get cert failed. Let's try last response."
2652 printf -- "%s" "$_rcert" | _dbase64 "multiline" | _base64 "multiline" >> "$CERT_PATH"
2653 fi
2654
88fab7d6 2655 echo "$END_CERT" >> "$CERT_PATH"
43822d37 2656 _info "$(__green "Cert success.")"
4c3b3608 2657 cat "$CERT_PATH"
2658
66f08eb2 2659 _info "Your cert is in $( __green " $CERT_PATH ")"
5980ebc7 2660
2661 if [ -f "$CERT_KEY_PATH" ] ; then
2662 _info "Your cert key is in $( __green " $CERT_KEY_PATH ")"
2663 fi
2664
caf1fc10 2665 cp "$CERT_PATH" "$CERT_FULLCHAIN_PATH"
281aa349 2666
8663fb7e 2667 if [ ! "$USER_PATH" ] || [ ! "$IN_CRON" ] ; then
281aa349 2668 USER_PATH="$PATH"
2669 _saveaccountconf "USER_PATH" "$USER_PATH"
2670 fi
4c3b3608 2671 fi
2672
2673
8663fb7e 2674 if [ -z "$Le_LinkCert" ] ; then
eae29099 2675 response="$(echo $response | _dbase64 "multiline" | _normalizeJson )"
22ea4004 2676 _err "Sign failed: $(echo "$response" | _egrep_o '"detail":"[^"]*"')"
b0070f03 2677 _on_issue_err
4c3b3608 2678 return 1
2679 fi
2680
4d2f38b0 2681 _cleardomainconf "Le_Vlist"
4c3b3608 2682
c2c8f320 2683 Le_LinkIssuer=$(grep -i '^Link' $HTTP_HEADER | _head_n 1 | cut -d " " -f 2| cut -d ';' -f 1 | tr -d '<>' )
fac1e367 2684 if ! _contains "$Le_LinkIssuer" ":" ; then
2685 Le_LinkIssuer="$API$Le_LinkIssuer"
2686 fi
2687
4d2f38b0 2688 _savedomainconf "Le_LinkIssuer" "$Le_LinkIssuer"
4c3b3608 2689
8663fb7e 2690 if [ "$Le_LinkIssuer" ] ; then
88fab7d6 2691 echo "$BEGIN_CERT" > "$CA_CERT_PATH"
c60883ef 2692 _get "$Le_LinkIssuer" | _base64 "multiline" >> "$CA_CERT_PATH"
88fab7d6 2693 echo "$END_CERT" >> "$CA_CERT_PATH"
66f08eb2 2694 _info "The intermediate CA cert is in $( __green " $CA_CERT_PATH ")"
caf1fc10 2695 cat "$CA_CERT_PATH" >> "$CERT_FULLCHAIN_PATH"
66f08eb2 2696 _info "And the full chain certs is there: $( __green " $CERT_FULLCHAIN_PATH ")"
4c3b3608 2697 fi
2698
3aae1ae3 2699 Le_CertCreateTime=$(_time)
4d2f38b0 2700 _savedomainconf "Le_CertCreateTime" "$Le_CertCreateTime"
4c3b3608 2701
2702 Le_CertCreateTimeStr=$(date -u )
4d2f38b0 2703 _savedomainconf "Le_CertCreateTimeStr" "$Le_CertCreateTimeStr"
4c3b3608 2704
523c7682 2705 if [ -z "$Le_RenewalDays" ] || [ "$Le_RenewalDays" -lt "0" ] || [ "$Le_RenewalDays" -gt "$MAX_RENEW" ] ; then
2706 Le_RenewalDays=$MAX_RENEW
054cb72e 2707 else
2708 _savedomainconf "Le_RenewalDays" "$Le_RenewalDays"
13d7cae9 2709 fi
2710
78009539
PS
2711 if [ "$CA_BUNDLE" ] ; then
2712 _saveaccountconf CA_BUNDLE "$CA_BUNDLE"
2713 else
2714 _clearaccountconf "CA_BUNDLE"
2715 fi
2716
fac1e367 2717 if [ "$HTTPS_INSECURE" ] ; then
2718 _saveaccountconf HTTPS_INSECURE "$HTTPS_INSECURE"
2719 else
2720 _clearaccountconf "HTTPS_INSECURE"
13d7cae9 2721 fi
00a50605 2722
50827188 2723 if [ "$Le_Listen_V4" ] ; then
2724 _savedomainconf "Le_Listen_V4" "$Le_Listen_V4"
2725 _cleardomainconf Le_Listen_V6
2726 elif [ "$Le_Listen_V6" ] ; then
2727 _savedomainconf "Le_Listen_V6" "$Le_Listen_V6"
2728 _cleardomainconf Le_Listen_V4
2729 fi
2730
00a50605 2731 Le_NextRenewTime=$(_math $Le_CertCreateTime + $Le_RenewalDays \* 24 \* 60 \* 60)
028e1747 2732
4c3b3608 2733
2734 Le_NextRenewTimeStr=$( _time2str $Le_NextRenewTime )
4d2f38b0 2735 _savedomainconf "Le_NextRenewTimeStr" "$Le_NextRenewTimeStr"
028e1747 2736
2737 Le_NextRenewTime=$(_math $Le_NextRenewTime - 86400)
2738 _savedomainconf "Le_NextRenewTime" "$Le_NextRenewTime"
f6dcd989 2739
028e1747 2740
b0070f03 2741 _on_issue_success
4c3b3608 2742
4c0d3f1b 2743 if [ "$Le_RealCertPath$Le_RealKeyPath$Le_RealCACertPath$Le_ReloadCmd$Le_RealFullChainPath" ] ; then
43822d37 2744 _installcert
01f54558 2745 fi
4c0d3f1b 2746
4c3b3608 2747}
2748
43822d37 2749#domain [isEcc]
4c3b3608 2750renew() {
2751 Le_Domain="$1"
8663fb7e 2752 if [ -z "$Le_Domain" ] ; then
43822d37 2753 _usage "Usage: $PROJECT_ENTRY --renew -d domain.com [--ecc]"
4c3b3608 2754 return 1
2755 fi
2756
43822d37 2757 _isEcc="$2"
2758
2759 _initpath $Le_Domain "$_isEcc"
2760
e2053b22 2761 _info "$(__green "Renew: '$Le_Domain'")"
8663fb7e 2762 if [ ! -f "$DOMAIN_CONF" ] ; then
43822d37 2763 _info "'$Le_Domain' is not a issued domain, skip."
4c3b3608 2764 return 0;
2765 fi
2766
1e6b68f5 2767 if [ "$Le_RenewalDays" ] ; then
2768 _savedomainconf Le_RenewalDays "$Le_RenewalDays"
2769 fi
2770
8663fb7e 2771 . "$DOMAIN_CONF"
5c48e139 2772
2773 if [ "$Le_API" ] ; then
2774 API="$Le_API"
2775 fi
2776
3aae1ae3 2777 if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(_time)" -lt "$Le_NextRenewTime" ] ; then
e2053b22 2778 _info "Skip, Next renewal time is: $(__green "$Le_NextRenewTimeStr")"
2779 _info "Add '$(__red '--force')' to force to renew."
cc179731 2780 return $RENEW_SKIP
4c3b3608 2781 fi
2782
2783 IS_RENEW="1"
0463b5d6 2784 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 2785 res=$?
a61fe418 2786 if [ "$res" != "0" ] ; then
2787 return $res
2788 fi
2789
2790 if [ "$Le_DeployHook" ] ; then
2791 deploy $Le_Domain "$Le_DeployHook" "$Le_Keylength"
2792 res=$?
2793 fi
2794
4c3b3608 2795 IS_RENEW=""
2796
2797 return $res
2798}
2799
cc179731 2800#renewAll [stopRenewOnError]
4c3b3608 2801renewAll() {
2802 _initpath
cc179731 2803 _stopRenewOnError="$1"
2804 _debug "_stopRenewOnError" "$_stopRenewOnError"
2805 _ret="0"
43822d37 2806
b2817897 2807 for d in $(ls -F ${CERT_HOME}/ | grep [^.].*[.].*/$ ) ; do
4c3b3608 2808 d=$(echo $d | cut -d '/' -f 1)
43822d37 2809 (
2810 if _endswith $d "$ECC_SUFFIX" ; then
2811 _isEcc=$(echo $d | cut -d "$ECC_SEP" -f 2)
2812 d=$(echo $d | cut -d "$ECC_SEP" -f 1)
2813 fi
2814 renew "$d" "$_isEcc"
4d2f38b0 2815 )
cc179731 2816 rc="$?"
2817 _debug "Return code: $rc"
2818 if [ "$rc" != "0" ] ; then
2819 if [ "$rc" = "$RENEW_SKIP" ] ; then
2820 _info "Skipped $d"
2821 elif [ "$_stopRenewOnError" ] ; then
2822 _err "Error renew $d, stop now."
2823 return $rc
2824 else
2825 _ret="$rc"
2826 _err "Error renew $d, Go ahead to next one."
2827 fi
2828 fi
4c3b3608 2829 done
cc179731 2830 return $_ret
4c3b3608 2831}
2832
dcf4f8f6 2833
10afcaca 2834#csr webroot
2835signcsr(){
2836 _csrfile="$1"
2837 _csrW="$2"
2838 if [ -z "$_csrfile" ] || [ -z "$_csrW" ]; then
2839 _usage "Usage: $PROJECT_ENTRY --signcsr --csr mycsr.csr -w /path/to/webroot/a.com/ "
2840 return 1
2841 fi
2842
2843 _initpath
2844
2845 _csrsubj=$(_readSubjectFromCSR "$_csrfile")
ad752b31 2846 if [ "$?" != "0" ] ; then
10afcaca 2847 _err "Can not read subject from csr: $_csrfile"
2848 return 1
2849 fi
ad752b31 2850 _debug _csrsubj "$_csrsubj"
10afcaca 2851
2852 _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile")
2853 if [ "$?" != "0" ] ; then
2854 _err "Can not read domain list from csr: $_csrfile"
2855 return 1
2856 fi
2857 _debug "_csrdomainlist" "$_csrdomainlist"
2858
ad752b31 2859
2860 if [ -z "$_csrsubj" ] ; then
2861 _csrsubj="$(_getfield "$_csrdomainlist" 1)"
2862 _debug _csrsubj "$_csrsubj"
2863 _csrdomainlist="$(echo "$_csrdomainlist" | cut -d , -f 2-)"
2864 _debug "_csrdomainlist" "$_csrdomainlist"
2865 fi
2866
2867 if [ -z "$_csrsubj" ] ; then
2868 _err "Can not read subject from csr: $_csrfile"
2869 return 1
2870 fi
2871
10afcaca 2872 _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile")
2873 if [ "$?" != "0" ] || [ -z "$_csrkeylength" ] ; then
2874 _err "Can not read key length from csr: $_csrfile"
2875 return 1
2876 fi
2877
2878 _initpath "$_csrsubj" "$_csrkeylength"
2879 mkdir -p "$DOMAIN_PATH"
2880
2881 _info "Copy csr to: $CSR_PATH"
2882 cp "$_csrfile" "$CSR_PATH"
2883
2884 issue "$_csrW" "$_csrsubj" "$_csrdomainlist" "$_csrkeylength"
2885
2886}
2887
2888showcsr() {
2889 _csrfile="$1"
2890 _csrd="$2"
2891 if [ -z "$_csrfile" ] && [ -z "$_csrd" ]; then
2892 _usage "Usage: $PROJECT_ENTRY --showcsr --csr mycsr.csr"
2893 return 1
2894 fi
2895
2896 _initpath
2897
2898 _csrsubj=$(_readSubjectFromCSR "$_csrfile")
2899 if [ "$?" != "0" ] || [ -z "$_csrsubj" ] ; then
2900 _err "Can not read subject from csr: $_csrfile"
2901 return 1
2902 fi
2903
2904 _info "Subject=$_csrsubj"
2905
2906 _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile")
2907 if [ "$?" != "0" ] ; then
2908 _err "Can not read domain list from csr: $_csrfile"
2909 return 1
2910 fi
2911 _debug "_csrdomainlist" "$_csrdomainlist"
2912
2913 _info "SubjectAltNames=$_csrdomainlist"
2914
2915
2916 _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile")
2917 if [ "$?" != "0" ] || [ -z "$_csrkeylength" ] ; then
2918 _err "Can not read key length from csr: $_csrfile"
2919 return 1
2920 fi
2921 _info "KeyLength=$_csrkeylength"
2922}
2923
6d7eda3e 2924list() {
22ea4004 2925 _raw="$1"
6d7eda3e 2926 _initpath
dcf4f8f6 2927
2928 _sep="|"
2929 if [ "$_raw" ] ; then
43822d37 2930 printf "Main_Domain${_sep}KeyLength${_sep}SAN_Domains${_sep}Created${_sep}Renew\n"
dcf4f8f6 2931 for d in $(ls -F ${CERT_HOME}/ | grep [^.].*[.].*/$ ) ; do
2932 d=$(echo $d | cut -d '/' -f 1)
2933 (
43822d37 2934 if _endswith $d "$ECC_SUFFIX" ; then
2935 _isEcc=$(echo $d | cut -d "$ECC_SEP" -f 2)
2936 d=$(echo $d | cut -d "$ECC_SEP" -f 1)
2937 fi
2938 _initpath $d "$_isEcc"
dcf4f8f6 2939 if [ -f "$DOMAIN_CONF" ] ; then
2940 . "$DOMAIN_CONF"
43822d37 2941 printf "$Le_Domain${_sep}\"$Le_Keylength\"${_sep}$Le_Alt${_sep}$Le_CertCreateTimeStr${_sep}$Le_NextRenewTimeStr\n"
dcf4f8f6 2942 fi
2943 )
2944 done
2945 else
22ea4004 2946 if _exists column ; then
2947 list "raw" | column -t -s "$_sep"
2948 else
43822d37 2949 list "raw" | tr "$_sep" '\t'
22ea4004 2950 fi
dcf4f8f6 2951 fi
6d7eda3e 2952
2953
2954}
2955
a61fe418 2956deploy() {
2957 Le_Domain="$1"
2958 Le_DeployHook="$2"
2959 _isEcc="$3"
2960 if [ -z "$Le_DeployHook" ] ; then
2961 _usage "Usage: $PROJECT_ENTRY --deploy -d domain.com --deploy-hook cpanel [--ecc] "
2962 return 1
2963 fi
2964
2965 _initpath $Le_Domain "$_isEcc"
2966 if [ ! -d "$DOMAIN_PATH" ] ; then
2967 _err "Domain is not valid:'$Le_Domain'"
2968 return 1
2969 fi
2970
2971 _deployApi="$(_findHook $Le_Domain deploy $Le_DeployHook)"
2972 if [ -z "$_deployApi" ] ; then
2973 _err "The deploy hook $Le_DeployHook is not found."
2974 return 1
2975 fi
2976 _debug _deployApi "$_deployApi"
2977
2978 _savedomainconf Le_DeployHook "$Le_DeployHook"
2979
2980 if ! (
2981 if ! . $_deployApi ; then
2982 _err "Load file $_deployApi error. Please check your api file and try again."
2983 return 1
2984 fi
2985
2986 d_command="${Le_DeployHook}_deploy"
2987 if ! _exists $d_command ; then
2988 _err "It seems that your api file is not correct, it must have a function named: $d_command"
2989 return 1
2990 fi
2991
2992 if ! $d_command $Le_Domain "$CERT_KEY_PATH" "$CERT_PATH" "$CA_CERT_PATH" "$CERT_FULLCHAIN_PATH" ; then
2993 _err "Error deploy for domain:$Le_Domain"
2994 _on_issue_err
2995 return 1
2996 fi
2997 ) ; then
2998 _err "Deploy error."
2999 return 1
3000 else
3001 _info "$(__green Success)"
3002 fi
3003
3004}
3005
4c3b3608 3006installcert() {
3007 Le_Domain="$1"
8663fb7e 3008 if [ -z "$Le_Domain" ] ; then
43822d37 3009 _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 3010 return 1
3011 fi
3012
3013 Le_RealCertPath="$2"
3014 Le_RealKeyPath="$3"
3015 Le_RealCACertPath="$4"
3016 Le_ReloadCmd="$5"
a63b05a9 3017 Le_RealFullChainPath="$6"
43822d37 3018 _isEcc="$7"
3019
3020 _initpath $Le_Domain "$_isEcc"
3021 if [ ! -d "$DOMAIN_PATH" ] ; then
3022 _err "Domain is not valid:'$Le_Domain'"
3023 return 1
3024 fi
3025
3026 _installcert
3027}
4c3b3608 3028
43822d37 3029
3030_installcert() {
4c3b3608 3031
4d2f38b0 3032 _savedomainconf "Le_RealCertPath" "$Le_RealCertPath"
3033 _savedomainconf "Le_RealCACertPath" "$Le_RealCACertPath"
3034 _savedomainconf "Le_RealKeyPath" "$Le_RealKeyPath"
3035 _savedomainconf "Le_ReloadCmd" "$Le_ReloadCmd"
3036 _savedomainconf "Le_RealFullChainPath" "$Le_RealFullChainPath"
4c3b3608 3037
3f4513b3 3038 if [ "$Le_RealCertPath" = "$NO_VALUE" ] ; then
4d2f38b0 3039 Le_RealCertPath=""
3040 fi
3f4513b3 3041 if [ "$Le_RealKeyPath" = "$NO_VALUE" ] ; then
4d2f38b0 3042 Le_RealKeyPath=""
3043 fi
3f4513b3 3044 if [ "$Le_RealCACertPath" = "$NO_VALUE" ] ; then
4d2f38b0 3045 Le_RealCACertPath=""
3046 fi
3f4513b3 3047 if [ "$Le_ReloadCmd" = "$NO_VALUE" ] ; then
4d2f38b0 3048 Le_ReloadCmd=""
3049 fi
3f4513b3 3050 if [ "$Le_RealFullChainPath" = "$NO_VALUE" ] ; then
4d2f38b0 3051 Le_RealFullChainPath=""
3052 fi
3053
3054 _installed="0"
8663fb7e 3055 if [ "$Le_RealCertPath" ] ; then
4d2f38b0 3056 _installed=1
3057 _info "Installing cert to:$Le_RealCertPath"
43822d37 3058 if [ -f "$Le_RealCertPath" ] && [ ! "$IS_RENEW" ] ; then
ff3bce32 3059 cp "$Le_RealCertPath" "$Le_RealCertPath".bak
4c3b3608 3060 fi
3061 cat "$CERT_PATH" > "$Le_RealCertPath"
3062 fi
3063
8663fb7e 3064 if [ "$Le_RealCACertPath" ] ; then
4d2f38b0 3065 _installed=1
3066 _info "Installing CA to:$Le_RealCACertPath"
8663fb7e 3067 if [ "$Le_RealCACertPath" = "$Le_RealCertPath" ] ; then
4c3b3608 3068 echo "" >> "$Le_RealCACertPath"
3069 cat "$CA_CERT_PATH" >> "$Le_RealCACertPath"
3070 else
43822d37 3071 if [ -f "$Le_RealCACertPath" ] && [ ! "$IS_RENEW" ] ; then
ff3bce32 3072 cp "$Le_RealCACertPath" "$Le_RealCACertPath".bak
78552b18 3073 fi
4c3b3608 3074 cat "$CA_CERT_PATH" > "$Le_RealCACertPath"
3075 fi
3076 fi
3077
3078
8663fb7e 3079 if [ "$Le_RealKeyPath" ] ; then
4d2f38b0 3080 _installed=1
3081 _info "Installing key to:$Le_RealKeyPath"
43822d37 3082 if [ -f "$Le_RealKeyPath" ] && [ ! "$IS_RENEW" ] ; then
ff3bce32 3083 cp "$Le_RealKeyPath" "$Le_RealKeyPath".bak
4c3b3608 3084 fi
3085 cat "$CERT_KEY_PATH" > "$Le_RealKeyPath"
3086 fi
a63b05a9 3087
8663fb7e 3088 if [ "$Le_RealFullChainPath" ] ; then
4d2f38b0 3089 _installed=1
3090 _info "Installing full chain to:$Le_RealFullChainPath"
43822d37 3091 if [ -f "$Le_RealFullChainPath" ] && [ ! "$IS_RENEW" ] ; then
ff3bce32 3092 cp "$Le_RealFullChainPath" "$Le_RealFullChainPath".bak
a63b05a9 3093 fi
3094 cat "$CERT_FULLCHAIN_PATH" > "$Le_RealFullChainPath"
3095 fi
4c3b3608 3096
8663fb7e 3097 if [ "$Le_ReloadCmd" ] ; then
4d2f38b0 3098 _installed=1
4c3b3608 3099 _info "Run Le_ReloadCmd: $Le_ReloadCmd"
4d2f38b0 3100 if (cd "$DOMAIN_PATH" && eval "$Le_ReloadCmd") ; then
43822d37 3101 _info "$(__green "Reload success")"
4d2f38b0 3102 else
3103 _err "Reload error for :$Le_Domain"
3104 fi
3105 fi
3106
4c3b3608 3107
3108}
3109
3110installcronjob() {
3111 _initpath
77546ea5 3112 if ! _exists "crontab" ; then
3113 _err "crontab doesn't exist, so, we can not install cron jobs."
3114 _err "All your certs will not be renewed automatically."
a7b7355d 3115 _err "You must add your own cron job to call '$PROJECT_ENTRY --cron' everyday."
77546ea5 3116 return 1
3117 fi
3118
4c3b3608 3119 _info "Installing cron job"
a7b7355d 3120 if ! crontab -l | grep "$PROJECT_ENTRY --cron" ; then
8663fb7e 3121 if [ -f "$LE_WORKING_DIR/$PROJECT_ENTRY" ] ; then
a7b7355d 3122 lesh="\"$LE_WORKING_DIR\"/$PROJECT_ENTRY"
4c3b3608 3123 else
a7b7355d 3124 _err "Can not install cronjob, $PROJECT_ENTRY not found."
4c3b3608 3125 return 1
3126 fi
22ea4004 3127 if _exists uname && uname -a | grep solaris >/dev/null ; then
3128 crontab -l | { cat; echo "0 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null"; } | crontab --
3129 else
3130 crontab -l | { cat; echo "0 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null"; } | crontab -
3131 fi
4c3b3608 3132 fi
8663fb7e 3133 if [ "$?" != "0" ] ; then
4c3b3608 3134 _err "Install cron job failed. You need to manually renew your certs."
3135 _err "Or you can add cronjob by yourself:"
a7b7355d 3136 _err "$lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null"
4c3b3608 3137 return 1
3138 fi
3139}
3140
3141uninstallcronjob() {
37db5b81 3142 if ! _exists "crontab" ; then
3143 return
3144 fi
4c3b3608 3145 _info "Removing cron job"
a7b7355d 3146 cr="$(crontab -l | grep "$PROJECT_ENTRY --cron")"
8663fb7e 3147 if [ "$cr" ] ; then
22ea4004 3148 if _exists uname && uname -a | grep solaris >/dev/null ; then
3149 crontab -l | sed "/$PROJECT_ENTRY --cron/d" | crontab --
3150 else
3151 crontab -l | sed "/$PROJECT_ENTRY --cron/d" | crontab -
3152 fi
a7b7355d 3153 LE_WORKING_DIR="$(echo "$cr" | cut -d ' ' -f 9 | tr -d '"')"
4c3b3608 3154 _info LE_WORKING_DIR "$LE_WORKING_DIR"
3155 fi
3156 _initpath
a7b7355d 3157
4c3b3608 3158}
3159
6cb415f5 3160revoke() {
3161 Le_Domain="$1"
8663fb7e 3162 if [ -z "$Le_Domain" ] ; then
43822d37 3163 _usage "Usage: $PROJECT_ENTRY --revoke -d domain.com"
6cb415f5 3164 return 1
3165 fi
3166
43822d37 3167 _isEcc="$2"
3168
3169 _initpath $Le_Domain "$_isEcc"
8663fb7e 3170 if [ ! -f "$DOMAIN_CONF" ] ; then
6cb415f5 3171 _err "$Le_Domain is not a issued domain, skip."
3172 return 1;
3173 fi
3174
8663fb7e 3175 if [ ! -f "$CERT_PATH" ] ; then
6cb415f5 3176 _err "Cert for $Le_Domain $CERT_PATH is not found, skip."
3177 return 1
3178 fi
3179
3180 cert="$(_getfile "${CERT_PATH}" "${BEGIN_CERT}" "${END_CERT}"| tr -d "\r\n" | _urlencode)"
3181
8663fb7e 3182 if [ -z "$cert" ] ; then
6cb415f5 3183 _err "Cert for $Le_Domain is empty found, skip."
3184 return 1
3185 fi
3186
3187 data="{\"resource\": \"revoke-cert\", \"certificate\": \"$cert\"}"
3188 uri="$API/acme/revoke-cert"
3189
3190 _info "Try domain key first."
3191 if _send_signed_request $uri "$data" "" "$CERT_KEY_PATH"; then
8663fb7e 3192 if [ -z "$response" ] ; then
6cb415f5 3193 _info "Revoke success."
3194 rm -f $CERT_PATH
3195 return 0
3196 else
3197 _err "Revoke error by domain key."
c9c31c04 3198 _err "$response"
6cb415f5 3199 fi
3200 fi
3201
3202 _info "Then try account key."
3203
3204 if _send_signed_request $uri "$data" "" "$ACCOUNT_KEY_PATH" ; then
8663fb7e 3205 if [ -z "$response" ] ; then
6cb415f5 3206 _info "Revoke success."
3207 rm -f $CERT_PATH
3208 return 0
3209 else
3210 _err "Revoke error."
c9c31c04 3211 _debug "$response"
6cb415f5 3212 fi
3213 fi
3214 return 1
3215}
4c3b3608 3216
0c00e870 3217
3218#domain vtype
3219_deactivate() {
3220 _d_domain="$1"
3221 _d_type="$2"
3222 _initpath
3223
3224 _d_i=0
3225 _d_max_retry=9
3226 while [ "$_d_i" -lt "$_d_max_retry" ] ;
3227 do
0407c4e0 3228 _info "Deactivate: $_d_domain"
0c00e870 3229 _d_i="$(_math $_d_i + 1)"
9774b01b 3230 if ! _send_signed_request "$API/acme/new-authz" "{\"resource\": \"new-authz\", \"identifier\": {\"type\": \"dns\", \"value\": \"$(_idn "$_d_domain")\"}}" ; then
0c00e870 3231 _err "Can not get domain token."
3232 return 1
3233 fi
3234
c2c8f320 3235 authzUri="$(echo "$responseHeaders" | grep "^Location:" | _head_n 1 | cut -d ' ' -f 2 | tr -d "\r\n")"
3f4513b3 3236 _debug "authzUri" "$authzUri"
0c00e870 3237
3238 if [ ! -z "$code" ] && [ ! "$code" = '201' ] ; then
3239 _err "new-authz error: $response"
3240 return 1
3241 fi
3242
fdcb6b72 3243 entry="$(printf "%s\n" "$response" | _egrep_o '[^\{]*"status":"valid","uri"[^\}]*')"
0c00e870 3244 _debug entry "$entry"
3245
3246 if [ -z "$entry" ] ; then
fb2029e7 3247 _info "No more valid entry found."
0c00e870 3248 break
3249 fi
3250
3251 _vtype="$(printf "%s\n" "$entry" | _egrep_o '"type": *"[^"]*"' | cut -d : -f 2 | tr -d '"')"
3252 _debug _vtype $_vtype
3253 _info "Found $_vtype"
3254
3255
3256 uri="$(printf "%s\n" "$entry" | _egrep_o '"uri":"[^"]*'| cut -d : -f 2,3 | tr -d '"' )"
3257 _debug uri $uri
3258
3259 if [ "$_d_type" ] && [ "$_d_type" != "$_vtype" ] ; then
3260 _info "Skip $_vtype"
3261 continue
3262 fi
3263
3264 _info "Deactivate: $_vtype"
3265
3266 if ! _send_signed_request "$authzUri" "{\"resource\": \"authz\", \"status\":\"deactivated\"}" ; then
3267 _err "Can not deactivate $_vtype."
3268 return 1
3269 fi
3270
fb2029e7 3271 _info "Deactivate: $_vtype success."
3272
0c00e870 3273 done
3274 _debug "$_d_i"
3275 if [ "$_d_i" -lt "$_d_max_retry" ] ; then
3276 _info "Deactivated success!"
3277 else
3278 _err "Deactivate failed."
3279 fi
3280
3281}
3282
3283deactivate() {
3f4513b3 3284 _d_domain_list="$1"
0c00e870 3285 _d_type="$2"
3286 _initpath
3f4513b3 3287 _debug _d_domain_list "$_d_domain_list"
3288 if [ -z "$(echo $_d_domain_list | cut -d , -f 1 )" ] ; then
3289 _usage "Usage: $PROJECT_ENTRY --deactivate -d domain.com [-d domain.com]"
0c00e870 3290 return 1
3291 fi
3f4513b3 3292 for _d_dm in $(echo "$_d_domain_list" | tr ',' ' ' ) ;
3293 do
3294 if [ -z "$_d_dm" ] || [ "$_d_dm" = "$NO_VALUE" ] ; then
3295 continue
3296 fi
86c017ec 3297 if ! _deactivate "$_d_dm" $_d_type ; then
3298 return 1
3299 fi
3f4513b3 3300 done
0c00e870 3301}
3302
4c3b3608 3303# Detect profile file if not specified as environment variable
3304_detect_profile() {
a63b05a9 3305 if [ -n "$PROFILE" -a -f "$PROFILE" ] ; then
4c3b3608 3306 echo "$PROFILE"
3307 return
3308 fi
3309
4c3b3608 3310 DETECTED_PROFILE=''
4c3b3608 3311 SHELLTYPE="$(basename "/$SHELL")"
3312
8663fb7e 3313 if [ "$SHELLTYPE" = "bash" ] ; then
3314 if [ -f "$HOME/.bashrc" ] ; then
4c3b3608 3315 DETECTED_PROFILE="$HOME/.bashrc"
8663fb7e 3316 elif [ -f "$HOME/.bash_profile" ] ; then
4c3b3608 3317 DETECTED_PROFILE="$HOME/.bash_profile"
3318 fi
8663fb7e 3319 elif [ "$SHELLTYPE" = "zsh" ] ; then
4c3b3608 3320 DETECTED_PROFILE="$HOME/.zshrc"
3321 fi
3322
8663fb7e 3323 if [ -z "$DETECTED_PROFILE" ] ; then
3324 if [ -f "$HOME/.profile" ] ; then
4c3b3608 3325 DETECTED_PROFILE="$HOME/.profile"
8663fb7e 3326 elif [ -f "$HOME/.bashrc" ] ; then
4c3b3608 3327 DETECTED_PROFILE="$HOME/.bashrc"
8663fb7e 3328 elif [ -f "$HOME/.bash_profile" ] ; then
4c3b3608 3329 DETECTED_PROFILE="$HOME/.bash_profile"
8663fb7e 3330 elif [ -f "$HOME/.zshrc" ] ; then
4c3b3608 3331 DETECTED_PROFILE="$HOME/.zshrc"
3332 fi
3333 fi
3334
8663fb7e 3335 if [ ! -z "$DETECTED_PROFILE" ] ; then
4c3b3608 3336 echo "$DETECTED_PROFILE"
3337 fi
3338}
3339
3340_initconf() {
3341 _initpath
8663fb7e 3342 if [ ! -f "$ACCOUNT_CONF_PATH" ] ; then
d53289d7 3343 echo "#ACCOUNT_CONF_PATH=xxxx
3344
3345#Account configurations:
4c3b3608 3346#Here are the supported macros, uncomment them to make them take effect.
d53289d7 3347
caa2e45a 3348#ACCOUNT_EMAIL=aaa@example.com # the account email used to register account.
5fd3f21b 3349#ACCOUNT_KEY_PATH=\"/path/to/account.key\"
b2817897 3350#CERT_HOME=\"/path/to/cert/home\"
4c3b3608 3351
d404e92d 3352
3353
d0871bda 3354#LOG_FILE=\"$DEFAULT_LOG_FILE\"
6b500036 3355#LOG_LEVEL=1
5ea6e9c9 3356
251d1c5c 3357#AUTO_UPGRADE=\"1\"
89002ed2 3358
4c3b3608 3359#STAGE=1 # Use the staging api
3360#FORCE=1 # Force to issue cert
3361#DEBUG=1 # Debug mode
3362
166096dc 3363
8814a348 3364#USER_AGENT=\"$USER_AGENT\"
281aa349 3365
3366#USER_PATH=""
3367
4c3b3608 3368#dns api
3369#######################
3370#Cloudflare:
3371#api key
3d49985a 3372#CF_Key=\"sdfsdfsdfljlbjkljlkjsdfoiwje\"
4c3b3608 3373#account email
3d49985a 3374#CF_Email=\"xxxx@sss.com\"
4c3b3608 3375
3376#######################
3377#Dnspod.cn:
3378#api key id
3d49985a 3379#DP_Id=\"1234\"
4c3b3608 3380#api key
3d49985a 3381#DP_Key=\"sADDsdasdgdsf\"
4c3b3608 3382
3383#######################
3384#Cloudxns.com:
3d49985a 3385#CX_Key=\"1234\"
4c3b3608 3386#
3d49985a 3387#CX_Secret=\"sADDsdasdgdsf\"
30de13b4 3388
3389#######################
3390#Godaddy.com:
3391#GD_Key=\"sdfdsgdgdfdasfds\"
3392#
3393#GD_Secret=\"sADDsdasdfsdfdssdgdsf\"
4c3b3608 3394
d6f0c2b5
MZ
3395#######################
3396#PowerDNS:
3397#PDNS_Url=\"http://ns.example.com:8081\"
3398#PDNS_ServerId=\"localhost\"
3399#PDNS_Token=\"0123456789ABCDEF\"
3400#PDNS_Ttl=60
3401
4c3b3608 3402 " > $ACCOUNT_CONF_PATH
3403 fi
3404}
3405
c8e9a31e 3406# nocron
c60883ef 3407_precheck() {
c8e9a31e 3408 _nocron="$1"
3409
c60883ef 3410 if ! _exists "curl" && ! _exists "wget"; then
3411 _err "Please install curl or wget first, we need to access http resources."
4c3b3608 3412 return 1
3413 fi
3414
c8e9a31e 3415 if [ -z "$_nocron" ] ; then
3416 if ! _exists "crontab" ; then
3417 _err "It is recommended to install crontab first. try to install 'cron, crontab, crontabs or vixie-cron'."
3418 _err "We need to set cron job to renew the certs automatically."
3419 _err "Otherwise, your certs will not be able to be renewed automatically."
3420 if [ -z "$FORCE" ] ; then
3421 _err "Please add '--force' and try install again to go without crontab."
3422 _err "./$PROJECT_ENTRY --install --force"
3423 return 1
3424 fi
77546ea5 3425 fi
4c3b3608 3426 fi
3427
c60883ef 3428 if ! _exists "openssl" ; then
3429 _err "Please install openssl first."
3430 _err "We need openssl to generate keys."
4c3b3608 3431 return 1
3432 fi
3433
c60883ef 3434 if ! _exists "nc" ; then
3435 _err "It is recommended to install nc first, try to install 'nc' or 'netcat'."
3436 _err "We use nc for standalone server if you use standalone mode."
3437 _err "If you don't use standalone mode, just ignore this warning."
3438 fi
3439
3440 return 0
3441}
3442
0a7c9364 3443_setShebang() {
3444 _file="$1"
3445 _shebang="$2"
3446 if [ -z "$_shebang" ] ; then
43822d37 3447 _usage "Usage: file shebang"
0a7c9364 3448 return 1
3449 fi
3450 cp "$_file" "$_file.tmp"
3451 echo "$_shebang" > "$_file"
3452 sed -n 2,99999p "$_file.tmp" >> "$_file"
3453 rm -f "$_file.tmp"
3454}
3455
94dc5f33 3456_installalias() {
3457 _initpath
3458
3459 _envfile="$LE_WORKING_DIR/$PROJECT_ENTRY.env"
3460 if [ "$_upgrading" ] && [ "$_upgrading" = "1" ] ; then
3461 echo "$(cat $_envfile)" | sed "s|^LE_WORKING_DIR.*$||" > "$_envfile"
3462 echo "$(cat $_envfile)" | sed "s|^alias le.*$||" > "$_envfile"
3463 echo "$(cat $_envfile)" | sed "s|^alias le.sh.*$||" > "$_envfile"
3464 fi
3465
1786a5e5 3466 _setopt "$_envfile" "export LE_WORKING_DIR" "=" "\"$LE_WORKING_DIR\""
94dc5f33 3467 _setopt "$_envfile" "alias $PROJECT_ENTRY" "=" "\"$LE_WORKING_DIR/$PROJECT_ENTRY\""
3468
3469 _profile="$(_detect_profile)"
3470 if [ "$_profile" ] ; then
3471 _debug "Found profile: $_profile"
aba5c634 3472 _info "Installing alias to '$_profile'"
94dc5f33 3473 _setopt "$_profile" ". \"$_envfile\""
3474 _info "OK, Close and reopen your terminal to start using $PROJECT_NAME"
3475 else
3476 _info "No profile is found, you will need to go into $LE_WORKING_DIR to use $PROJECT_NAME"
3477 fi
3478
3479
3480 #for csh
3481 _cshfile="$LE_WORKING_DIR/$PROJECT_ENTRY.csh"
94dc5f33 3482 _csh_profile="$HOME/.cshrc"
3483 if [ -f "$_csh_profile" ] ; then
aba5c634 3484 _info "Installing alias to '$_csh_profile'"
6626371d 3485 _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\""
3486 _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY\""
94dc5f33 3487 _setopt "$_csh_profile" "source \"$_cshfile\""
3488 fi
acafa585 3489
3490 #for tcsh
3491 _tcsh_profile="$HOME/.tcshrc"
3492 if [ -f "$_tcsh_profile" ] ; then
aba5c634 3493 _info "Installing alias to '$_tcsh_profile'"
acafa585 3494 _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\""
3495 _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY\""
3496 _setopt "$_tcsh_profile" "source \"$_cshfile\""
3497 fi
94dc5f33 3498
3499}
3500
c8e9a31e 3501# nocron
c60883ef 3502install() {
f3e4cea3 3503
3504 if [ -z "$LE_WORKING_DIR" ] ; then
3505 LE_WORKING_DIR="$DEFAULT_INSTALL_HOME"
3506 fi
3507
c8e9a31e 3508 _nocron="$1"
c60883ef 3509 if ! _initpath ; then
3510 _err "Install failed."
4c3b3608 3511 return 1
3512 fi
52677b0a 3513 if [ "$_nocron" ] ; then
3514 _debug "Skip install cron job"
3515 fi
3516
c8e9a31e 3517 if ! _precheck "$_nocron" ; then
c60883ef 3518 _err "Pre-check failed, can not install."
4c3b3608 3519 return 1
3520 fi
c60883ef 3521
6cc11ffb 3522 #convert from le
8663fb7e 3523 if [ -d "$HOME/.le" ] ; then
6cc11ffb 3524 for envfile in "le.env" "le.sh.env"
3525 do
8663fb7e 3526 if [ -f "$HOME/.le/$envfile" ] ; then
6cc11ffb 3527 if grep "le.sh" "$HOME/.le/$envfile" >/dev/null ; then
3528 _upgrading="1"
3529 _info "You are upgrading from le.sh"
3530 _info "Renaming \"$HOME/.le\" to $LE_WORKING_DIR"
3531 mv "$HOME/.le" "$LE_WORKING_DIR"
3532 mv "$LE_WORKING_DIR/$envfile" "$LE_WORKING_DIR/$PROJECT_ENTRY.env"
3533 break;
3534 fi
3535 fi
3536 done
3537 fi
3538
4c3b3608 3539 _info "Installing to $LE_WORKING_DIR"
635695ec 3540
4a0f23e2 3541 if ! mkdir -p "$LE_WORKING_DIR" ; then
90035252 3542 _err "Can not create working dir: $LE_WORKING_DIR"
4a0f23e2 3543 return 1
3544 fi
3545
762978f8 3546 chmod 700 "$LE_WORKING_DIR"
3547
a7b7355d 3548 cp $PROJECT_ENTRY "$LE_WORKING_DIR/" && chmod +x "$LE_WORKING_DIR/$PROJECT_ENTRY"
4c3b3608 3549
8663fb7e 3550 if [ "$?" != "0" ] ; then
a7b7355d 3551 _err "Install failed, can not copy $PROJECT_ENTRY"
4c3b3608 3552 return 1
3553 fi
3554
a7b7355d 3555 _info "Installed to $LE_WORKING_DIR/$PROJECT_ENTRY"
4c3b3608 3556
94dc5f33 3557 _installalias
4c3b3608 3558
a61fe418 3559 for subf in $_SUB_FOLDERS ; do
3560 if [ -d "$subf" ] ; then
3561 mkdir -p $LE_WORKING_DIR/$subf
3562 cp $subf/* $LE_WORKING_DIR/$subf/
3563 fi
3564 done
3565
d53289d7 3566
8663fb7e 3567 if [ ! -f "$ACCOUNT_CONF_PATH" ] ; then
4c3b3608 3568 _initconf
3569 fi
6cc11ffb 3570
8663fb7e 3571 if [ "$_DEFAULT_ACCOUNT_CONF_PATH" != "$ACCOUNT_CONF_PATH" ] ; then
635695ec 3572 _setopt "$_DEFAULT_ACCOUNT_CONF_PATH" "ACCOUNT_CONF_PATH" "=" "\"$ACCOUNT_CONF_PATH\""
6cc11ffb 3573 fi
3574
8663fb7e 3575 if [ "$_DEFAULT_CERT_HOME" != "$CERT_HOME" ] ; then
b2817897 3576 _saveaccountconf "CERT_HOME" "$CERT_HOME"
3577 fi
3578
8663fb7e 3579 if [ "$_DEFAULT_ACCOUNT_KEY_PATH" != "$ACCOUNT_KEY_PATH" ] ; then
b2817897 3580 _saveaccountconf "ACCOUNT_KEY_PATH" "$ACCOUNT_KEY_PATH"
3581 fi
3582
c8e9a31e 3583 if [ -z "$_nocron" ] ; then
3584 installcronjob
3585 fi
0a7c9364 3586
641989fd 3587 if [ -z "$NO_DETECT_SH" ] ; then
3588 #Modify shebang
3589 if _exists bash ; then
66990cf8 3590 _info "Good, bash is found, so change the shebang to use bash as prefered."
641989fd 3591 _shebang='#!/usr/bin/env bash'
3592 _setShebang "$LE_WORKING_DIR/$PROJECT_ENTRY" "$_shebang"
a61fe418 3593 for subf in $_SUB_FOLDERS ; do
3594 if [ -d "$LE_WORKING_DIR/$subf" ] ; then
3595 for _apifile in "$LE_WORKING_DIR/$subf/"*.sh ; do
3596 _setShebang "$_apifile" "$_shebang"
3597 done
3598 fi
3599 done
0a7c9364 3600 fi
3601 fi
3602
4c3b3608 3603 _info OK
3604}
3605
52677b0a 3606# nocron
4c3b3608 3607uninstall() {
52677b0a 3608 _nocron="$1"
3609 if [ -z "$_nocron" ] ; then
3610 uninstallcronjob
3611 fi
4c3b3608 3612 _initpath
3613
9aa3be7f 3614 _uninstallalias
3615
3616 rm -f $LE_WORKING_DIR/$PROJECT_ENTRY
3617 _info "The keys and certs are in $LE_WORKING_DIR, you can remove them by yourself."
3618
3619}
3620
3621_uninstallalias() {
3622 _initpath
3623
4c3b3608 3624 _profile="$(_detect_profile)"
8663fb7e 3625 if [ "$_profile" ] ; then
9aa3be7f 3626 _info "Uninstalling alias from: '$_profile'"
7203a1c1 3627 text="$(cat $_profile)"
94dc5f33 3628 echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.env\"$||" > "$_profile"
4c3b3608 3629 fi
3630
94dc5f33 3631 _csh_profile="$HOME/.cshrc"
3632 if [ -f "$_csh_profile" ] ; then
9aa3be7f 3633 _info "Uninstalling alias from: '$_csh_profile'"
94dc5f33 3634 text="$(cat $_csh_profile)"
3635 echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" > "$_csh_profile"
3636 fi
3637
acafa585 3638 _tcsh_profile="$HOME/.tcshrc"
3639 if [ -f "$_tcsh_profile" ] ; then
9aa3be7f 3640 _info "Uninstalling alias from: '$_csh_profile'"
acafa585 3641 text="$(cat $_tcsh_profile)"
3642 echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" > "$_tcsh_profile"
3643 fi
4c3b3608 3644
3645}
3646
3647cron() {
281aa349 3648 IN_CRON=1
89002ed2 3649 _initpath
6bf281f9 3650 if [ "$AUTO_UPGRADE" = "1" ] ; then
89002ed2 3651 export LE_WORKING_DIR
3652 (
89002ed2 3653 if ! upgrade ; then
3654 _err "Cron:Upgrade failed!"
3655 return 1
3656 fi
3657 )
3658 . $LE_WORKING_DIR/$PROJECT_ENTRY >/dev/null
1ab63043 3659
3660 if [ -t 1 ] ; then
3661 __INTERACTIVE="1"
3662 fi
3663
89002ed2 3664 _info "Auto upgraded to: $VER"
3665 fi
4c3b3608 3666 renewAll
cc179731 3667 _ret="$?"
281aa349 3668 IN_CRON=""
0ba95a3d 3669 exit $_ret
4c3b3608 3670}
3671
3672version() {
a63b05a9 3673 echo "$PROJECT"
3674 echo "v$VER"
4c3b3608 3675}
3676
3677showhelp() {
d0871bda 3678 _initpath
4c3b3608 3679 version
a7b7355d 3680 echo "Usage: $PROJECT_ENTRY command ...[parameters]....
a63b05a9 3681Commands:
3682 --help, -h Show this help message.
3683 --version, -v Show version info.
a7b7355d 3684 --install Install $PROJECT_NAME to your system.
3685 --uninstall Uninstall $PROJECT_NAME, and uninstall the cron job.
10afcaca 3686 --upgrade Upgrade $PROJECT_NAME to the latest code from $PROJECT .
a63b05a9 3687 --issue Issue a cert.
10afcaca 3688 --signcsr Issue a cert from an existing csr.
a61fe418 3689 --deploy Deploy the cert to your server.
a63b05a9 3690 --installcert Install the issued cert to apache/nginx or any other server.
3691 --renew, -r Renew a cert.
10afcaca 3692 --renewAll Renew all the certs.
a63b05a9 3693 --revoke Revoke a cert.
10afcaca 3694 --list List all the certs.
3695 --showcsr Show the content of a csr.
a63b05a9 3696 --installcronjob Install the cron job to renew certs, you don't need to call this. The 'install' command can automatically install the cron job.
3697 --uninstallcronjob Uninstall the cron job. The 'uninstall' command can do this automatically.
3698 --cron Run cron job to renew all the certs.
3699 --toPkcs Export the certificate and key to a pfx file.
eb59817e 3700 --updateaccount Update account info.
3701 --registeraccount Register account key.
a63b05a9 3702 --createAccountKey, -cak Create an account private key, professional use.
3703 --createDomainKey, -cdk Create an domain private key, professional use.
3704 --createCSR, -ccsr Create CSR , professional use.
0c00e870 3705 --deactivate Deactivate the domain authz, professional use.
a63b05a9 3706
3707Parameters:
3708 --domain, -d domain.tld Specifies a domain, used to issue, renew or revoke etc.
3709 --force, -f Used to force to install or force to renew a cert immediately.
3710 --staging, --test Use staging server, just for test.
3711 --debug Output debug info.
3712
3713 --webroot, -w /path/to/webroot Specifies the web root folder for web root mode.
3714 --standalone Use standalone mode.
e22bcf7c 3715 --tls Use standalone tls mode.
a63b05a9 3716 --apache Use apache mode.
eccec5f6 3717 --dns [dns_cf|dns_dp|dns_cx|/path/to/api/file] Use dns mode or dns api.
4a4dacb5 3718 --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 3719
3720 --keylength, -k [2048] Specifies the domain key length: 2048, 3072, 4096, 8192 or ec-256, ec-384.
3721 --accountkeylength, -ak [2048] Specifies the account key length.
d0871bda 3722 --log [/path/to/logfile] Specifies the log file. The default is: \"$DEFAULT_LOG_FILE\" if you don't give a file path here.
a73c5b33 3723 --log-level 1|2 Specifies the log level, default is 1.
a63b05a9 3724
3725 These parameters are to install the cert to nginx/apache or anyother server after issue/renew a cert:
3726
3727 --certpath /path/to/real/cert/file After issue/renew, the cert will be copied to this path.
3728 --keypath /path/to/real/key/file After issue/renew, the key will be copied to this path.
3729 --capath /path/to/real/ca/file After issue/renew, the intermediate cert will be copied to this path.
3730 --fullchainpath /path/to/fullchain/file After issue/renew, the fullchain cert will be copied to this path.
3731
3732 --reloadcmd \"service nginx reload\" After issue/renew, it's used to reload the server.
3733
3734 --accountconf Specifies a customized account config file.
635695ec 3735 --home Specifies the home dir for $PROJECT_NAME .
39c8f79f 3736 --certhome Specifies the home dir to save all the certs, only valid for '--install' command.
635695ec 3737 --useragent Specifies the user agent string. it will be saved for future use too.
b5eb4b90 3738 --accountemail Specifies the account email for registering, Only valid for the '--install' command.
06625071 3739 --accountkey Specifies the account key path, Only valid for the '--install' command.
523c7682 3740 --days Specifies the days to renew the cert when using '--issue' command. The max value is $MAX_RENEW days.
39c8f79f 3741 --httpport Specifies the standalone listening port. Only valid if the server is behind a reverse proxy or load balancer.
e22bcf7c 3742 --tlsport Specifies the standalone tls listening port. Only valid if the server is behind a reverse proxy or load balancer.
6ae0f7f5 3743 --local-address Specifies the standalone/tls server listening address, in case you have multiple ip addresses.
dcf4f8f6 3744 --listraw Only used for '--list' command, list the certs in raw format.
c8e9a31e 3745 --stopRenewOnError, -se Only valid for '--renewall' command. Stop if one cert has error in renewal.
13d7cae9 3746 --insecure Do not check the server certificate, in some devices, the api server's certificate may not be trusted.
78009539 3747 --ca-bundle Specifices the path to the CA certificate bundle to verify api server's certificate.
bc96082f 3748 --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 3749 --ecc Specifies to use the ECC cert. Valid for '--installcert', '--renew', '--revoke', '--toPkcs' and '--createCSR'
10afcaca 3750 --csr Specifies the input csr.
b0070f03 3751 --pre-hook Command to be run before obtaining any certificates.
3752 --post-hook Command to be run after attempting to obtain/renew certificates. No matter the obain/renew is success or failed.
3753 --renew-hook Command to be run once for each successfully renewed certificate.
a61fe418 3754 --deploy-hook The hook file to deploy cert
0c9546cc 3755 --ocsp-must-staple, --ocsp Generate ocsp must Staple extension.
6bf281f9 3756 --auto-upgrade [0|1] Valid for '--upgrade' command, indicating whether to upgrade automatically in future.
6ae0f7f5 3757 --listen-v4 Force standalone/tls server to listen at ipv4.
3758 --listen-v6 Force standalone/tls server to listen at ipv6.
4c3b3608 3759 "
3760}
3761
52677b0a 3762# nocron
4a0f23e2 3763_installOnline() {
3764 _info "Installing from online archive."
52677b0a 3765 _nocron="$1"
8663fb7e 3766 if [ ! "$BRANCH" ] ; then
4a0f23e2 3767 BRANCH="master"
3768 fi
a8df88ab 3769
4a0f23e2 3770 target="$PROJECT/archive/$BRANCH.tar.gz"
3771 _info "Downloading $target"
3772 localname="$BRANCH.tar.gz"
3773 if ! _get "$target" > $localname ; then
df9547ae 3774 _err "Download error."
4a0f23e2 3775 return 1
3776 fi
0bbe6eef 3777 (
4a0f23e2 3778 _info "Extracting $localname"
3779 tar xzf $localname
0bbe6eef 3780
6cc11ffb 3781 cd "$PROJECT_NAME-$BRANCH"
a7b7355d 3782 chmod +x $PROJECT_ENTRY
52677b0a 3783 if ./$PROJECT_ENTRY install "$_nocron" ; then
4a0f23e2 3784 _info "Install success!"
3785 fi
3786
3787 cd ..
0bbe6eef 3788
6cc11ffb 3789 rm -rf "$PROJECT_NAME-$BRANCH"
4a0f23e2 3790 rm -f "$localname"
0bbe6eef 3791 )
4a0f23e2 3792}
3793
52677b0a 3794upgrade() {
3795 if (
267f283a 3796 _initpath
3797 export LE_WORKING_DIR
d0b748a4 3798 cd "$LE_WORKING_DIR"
52677b0a 3799 _installOnline "nocron"
3800 ) ; then
3801 _info "Upgrade success!"
096d8992 3802 exit 0
52677b0a 3803 else
3804 _err "Upgrade failed!"
096d8992 3805 exit 1
52677b0a 3806 fi
3807}
a63b05a9 3808
5ea6e9c9 3809_processAccountConf() {
3810 if [ "$_useragent" ] ; then
3811 _saveaccountconf "USER_AGENT" "$_useragent"
fbd2038f 3812 elif [ "$USER_AGENT" ] && [ "$USER_AGENT" != "$DEFAULT_USER_AGENT" ] ; then
d0871bda 3813 _saveaccountconf "USER_AGENT" "$USER_AGENT"
5ea6e9c9 3814 fi
3815
3816 if [ "$_accountemail" ] ; then
3817 _saveaccountconf "ACCOUNT_EMAIL" "$_accountemail"
fbd2038f 3818 elif [ "$ACCOUNT_EMAIL" ] && [ "$ACCOUNT_EMAIL" != "$DEFAULT_ACCOUNT_EMAIL" ] ; then
d0871bda 3819 _saveaccountconf "ACCOUNT_EMAIL" "$ACCOUNT_EMAIL"
5ea6e9c9 3820 fi
3821
6bf281f9 3822 if [ "$_auto_upgrade" ] ; then
3823 _saveaccountconf "AUTO_UPGRADE" "$_auto_upgrade"
3824 elif [ "$AUTO_UPGRADE" ] ; then
3825 _saveaccountconf "AUTO_UPGRADE" "$AUTO_UPGRADE"
3826 fi
3827
5ea6e9c9 3828}
3829
a63b05a9 3830_process() {
3831 _CMD=""
3832 _domain=""
3f4513b3 3833 _altdomains="$NO_VALUE"
a63b05a9 3834 _webroot=""
bdbf323f 3835 _keylength=""
3836 _accountkeylength=""
3837 _certpath=""
3838 _keypath=""
3839 _capath=""
3840 _fullchainpath=""
4d2f38b0 3841 _reloadcmd=""
a63b05a9 3842 _password=""
635695ec 3843 _accountconf=""
3844 _useragent=""
b5eb4b90 3845 _accountemail=""
3846 _accountkey=""
b2817897 3847 _certhome=""
39c8f79f 3848 _httpport=""
e22bcf7c 3849 _tlsport=""
0e38c60d 3850 _dnssleep=""
dcf4f8f6 3851 _listraw=""
cc179731 3852 _stopRenewOnError=""
13d7cae9 3853 _insecure=""
78009539 3854 _ca_bundle=""
c8e9a31e 3855 _nocron=""
43822d37 3856 _ecc=""
10afcaca 3857 _csr=""
b0070f03 3858 _pre_hook=""
3859 _post_hook=""
3860 _renew_hook=""
a61fe418 3861 _deploy_hook=""
5ea6e9c9 3862 _logfile=""
d0871bda 3863 _log=""
0463b5d6 3864 _local_address=""
a73c5b33 3865 _log_level=""
6bf281f9 3866 _auto_upgrade=""
6ae0f7f5 3867 _listen_v4=""
3868 _listen_v6=""
8663fb7e 3869 while [ ${#} -gt 0 ] ; do
a63b05a9 3870 case "${1}" in
3871
3872 --help|-h)
3873 showhelp
3874 return
3875 ;;
3876 --version|-v)
3877 version
3878 return
3879 ;;
3880 --install)
3881 _CMD="install"
3882 ;;
3883 --uninstall)
3884 _CMD="uninstall"
3885 ;;
52677b0a 3886 --upgrade)
3887 _CMD="upgrade"
3888 ;;
a63b05a9 3889 --issue)
3890 _CMD="issue"
3891 ;;
a61fe418 3892 --deploy)
3893 _CMD="deploy"
3894 ;;
10afcaca 3895 --signcsr)
3896 _CMD="signcsr"
3897 ;;
3898 --showcsr)
3899 _CMD="showcsr"
3900 ;;
a63b05a9 3901 --installcert|-i)
3902 _CMD="installcert"
3903 ;;
3904 --renew|-r)
3905 _CMD="renew"
3906 ;;
4d2f38b0 3907 --renewAll|--renewall)
a63b05a9 3908 _CMD="renewAll"
3909 ;;
3910 --revoke)
3911 _CMD="revoke"
3912 ;;
6d7eda3e 3913 --list)
3914 _CMD="list"
3915 ;;
a63b05a9 3916 --installcronjob)
3917 _CMD="installcronjob"
3918 ;;
3919 --uninstallcronjob)
3920 _CMD="uninstallcronjob"
3921 ;;
3922 --cron)
3923 _CMD="cron"
3924 ;;
3925 --toPkcs)
3926 _CMD="toPkcs"
3927 ;;
3928 --createAccountKey|--createaccountkey|-cak)
3929 _CMD="createAccountKey"
3930 ;;
3931 --createDomainKey|--createdomainkey|-cdk)
3932 _CMD="createDomainKey"
3933 ;;
3934 --createCSR|--createcsr|-ccr)
3935 _CMD="createCSR"
3936 ;;
0c00e870 3937 --deactivate)
3938 _CMD="deactivate"
3939 ;;
eb59817e 3940 --updateaccount)
3941 _CMD="updateaccount"
3942 ;;
3943 --registeraccount)
3944 _CMD="registeraccount"
3945 ;;
a63b05a9 3946 --domain|-d)
3947 _dvalue="$2"
3948
ee1737a5 3949 if [ "$_dvalue" ] ; then
3950 if _startswith "$_dvalue" "-" ; then
3951 _err "'$_dvalue' is not a valid domain for parameter '$1'"
3952 return 1
3953 fi
9774b01b 3954 if _is_idn "$_dvalue" && ! _exists idn ; then
3955 _err "It seems that $_dvalue is an IDN( Internationalized Domain Names), please install 'idn' command first."
3956 return 1
3957 fi
ee1737a5 3958
3959 if [ -z "$_domain" ] ; then
3960 _domain="$_dvalue"
a63b05a9 3961 else
3f4513b3 3962 if [ "$_altdomains" = "$NO_VALUE" ] ; then
ee1737a5 3963 _altdomains="$_dvalue"
3964 else
3965 _altdomains="$_altdomains,$_dvalue"
3966 fi
a63b05a9 3967 fi
3968 fi
ee1737a5 3969
a63b05a9 3970 shift
3971 ;;
3972
3973 --force|-f)
3974 FORCE="1"
3975 ;;
3976 --staging|--test)
3977 STAGE="1"
3978 ;;
3979 --debug)
8663fb7e 3980 if [ -z "$2" ] || _startswith "$2" "-" ; then
a63b05a9 3981 DEBUG="1"
3982 else
3983 DEBUG="$2"
3984 shift
6fc1447f 3985 fi
a63b05a9 3986 ;;
a63b05a9 3987 --webroot|-w)
3988 wvalue="$2"
8663fb7e 3989 if [ -z "$_webroot" ] ; then
a63b05a9 3990 _webroot="$wvalue"
3991 else
3992 _webroot="$_webroot,$wvalue"
3993 fi
3994 shift
3995 ;;
3996 --standalone)
3f4513b3 3997 wvalue="$NO_VALUE"
8663fb7e 3998 if [ -z "$_webroot" ] ; then
a63b05a9 3999 _webroot="$wvalue"
4000 else
4001 _webroot="$_webroot,$wvalue"
4002 fi
4003 ;;
0463b5d6 4004 --local-address)
4005 lvalue="$2"
4006 _local_address="$_local_address$lvalue,"
4007 shift
4008 ;;
a63b05a9 4009 --apache)
4010 wvalue="apache"
8663fb7e 4011 if [ -z "$_webroot" ] ; then
a63b05a9 4012 _webroot="$wvalue"
4013 else
4014 _webroot="$_webroot,$wvalue"
4015 fi
4016 ;;
e22bcf7c 4017 --tls)
4018 wvalue="$W_TLS"
4019 if [ -z "$_webroot" ] ; then
4020 _webroot="$wvalue"
4021 else
4022 _webroot="$_webroot,$wvalue"
4023 fi
4024 ;;
a63b05a9 4025 --dns)
4026 wvalue="dns"
dceb3aca 4027 if ! _startswith "$2" "-" ; then
a63b05a9 4028 wvalue="$2"
4029 shift
4030 fi
8663fb7e 4031 if [ -z "$_webroot" ] ; then
a63b05a9 4032 _webroot="$wvalue"
4033 else
4034 _webroot="$_webroot,$wvalue"
4035 fi
4036 ;;
0e38c60d 4037 --dnssleep)
4038 _dnssleep="$2"
4039 Le_DNSSleep="$_dnssleep"
4040 shift
4041 ;;
4042
a63b05a9 4043 --keylength|-k)
4044 _keylength="$2"
3f4513b3 4045 if [ "$_accountkeylength" = "$NO_VALUE" ] ; then
2ce87fe2 4046 _accountkeylength="$2"
4047 fi
a63b05a9 4048 shift
4049 ;;
4050 --accountkeylength|-ak)
2ce87fe2 4051 _accountkeylength="$2"
a63b05a9 4052 shift
4053 ;;
4054
4055 --certpath)
4056 _certpath="$2"
4057 shift
4058 ;;
4059 --keypath)
4060 _keypath="$2"
4061 shift
4062 ;;
4063 --capath)
4064 _capath="$2"
4065 shift
4066 ;;
4067 --fullchainpath)
4068 _fullchainpath="$2"
4069 shift
4070 ;;
635695ec 4071 --reloadcmd|--reloadCmd)
a63b05a9 4072 _reloadcmd="$2"
4073 shift
4074 ;;
4075 --password)
4076 _password="$2"
4077 shift
4078 ;;
4079 --accountconf)
635695ec 4080 _accountconf="$2"
4081 ACCOUNT_CONF_PATH="$_accountconf"
a7b7355d 4082 shift
a63b05a9 4083 ;;
a7b7355d 4084 --home)
a63b05a9 4085 LE_WORKING_DIR="$2"
a7b7355d 4086 shift
a63b05a9 4087 ;;
b2817897 4088 --certhome)
4089 _certhome="$2"
4090 CERT_HOME="$_certhome"
4091 shift
4092 ;;
635695ec 4093 --useragent)
4094 _useragent="$2"
4095 USER_AGENT="$_useragent"
4096 shift
4097 ;;
b5eb4b90 4098 --accountemail )
4099 _accountemail="$2"
4100 ACCOUNT_EMAIL="$_accountemail"
4101 shift
4102 ;;
4103 --accountkey )
4104 _accountkey="$2"
4105 ACCOUNT_KEY_PATH="$_accountkey"
4106 shift
4107 ;;
06625071 4108 --days )
4109 _days="$2"
4110 Le_RenewalDays="$_days"
4111 shift
4112 ;;
39c8f79f 4113 --httpport )
4114 _httpport="$2"
4115 Le_HTTPPort="$_httpport"
4116 shift
4117 ;;
e22bcf7c 4118 --tlsport )
4119 _tlsport="$2"
4120 Le_TLSPort="$_tlsport"
4121 shift
4122 ;;
4123
dcf4f8f6 4124 --listraw )
4125 _listraw="raw"
4126 ;;
cc179731 4127 --stopRenewOnError|--stoprenewonerror|-se )
4128 _stopRenewOnError="1"
4129 ;;
13d7cae9 4130 --insecure)
4131 _insecure="1"
fac1e367 4132 HTTPS_INSECURE="1"
13d7cae9 4133 ;;
78009539 4134 --ca-bundle)
775bd1ab 4135 _ca_bundle="$(readlink -f $2)"
78009539
PS
4136 CA_BUNDLE="$_ca_bundle"
4137 shift
4138 ;;
c8e9a31e 4139 --nocron)
4140 _nocron="1"
4141 ;;
43822d37 4142 --ecc)
4143 _ecc="isEcc"
4144 ;;
10afcaca 4145 --csr)
4146 _csr="$2"
4147 shift
4148 ;;
b0070f03 4149 --pre-hook)
4150 _pre_hook="$2"
4151 shift
4152 ;;
4153 --post-hook)
4154 _post_hook="$2"
4155 shift
4156 ;;
4157 --renew-hook)
4158 _renew_hook="$2"
4159 shift
4160 ;;
a61fe418 4161 --deploy-hook)
4162 _deploy_hook="$2"
4163 shift
4164 ;;
0c9546cc 4165 --ocsp-must-staple|--ocsp)
4166 Le_OCSP_Stable="1"
4167 ;;
d0871bda 4168 --log|--logfile)
4169 _log="1"
5ea6e9c9 4170 _logfile="$2"
6bf281f9 4171 if _startswith "$_logfile" '-' ; then
d0871bda 4172 _logfile=""
4173 else
4174 shift
4175 fi
5ea6e9c9 4176 LOG_FILE="$_logfile"
a73c5b33 4177 if [ -z "$LOG_LEVEL" ] ; then
4178 LOG_LEVEL="$DEFAULT_LOG_LEVEL"
4179 fi
4180 ;;
4181 --log-level)
30bfc2ce 4182 _log_level="$2"
a73c5b33 4183 LOG_LEVEL="$_log_level"
4184 shift
5ea6e9c9 4185 ;;
6bf281f9 4186 --auto-upgrade)
4187 _auto_upgrade="$2"
4188 if [ -z "$_auto_upgrade" ] || _startswith "$_auto_upgrade" '-' ; then
4189 _auto_upgrade="1"
4190 else
4191 shift
4192 fi
4193 AUTO_UPGRADE="$_auto_upgrade"
4194 ;;
6ae0f7f5 4195 --listen-v4)
4196 _listen_v4="1"
4197 Le_Listen_V4="$_listen_v4"
4198 ;;
4199 --listen-v6)
4200 _listen_v6="1"
4201 Le_Listen_V6="$_listen_v6"
4202 ;;
6bf281f9 4203
a63b05a9 4204 *)
4205 _err "Unknown parameter : $1"
4206 return 1
4207 ;;
4208 esac
4209
4210 shift 1
4211 done
4212
5ea6e9c9 4213 if [ "${_CMD}" != "install" ] ; then
4214 __initHome
661f0583 4215 if [ "$_log" ]; then
4216 if [ -z "$_logfile" ] ; then
4217 _logfile="$DEFAULT_LOG_FILE"
4218 fi
d0871bda 4219 fi
5ea6e9c9 4220 if [ "$_logfile" ] ; then
4221 _saveaccountconf "LOG_FILE" "$_logfile"
661f0583 4222 LOG_FILE="$_logfile"
5ea6e9c9 4223 fi
a73c5b33 4224
4225 if [ "$_log_level" ] ; then
4226 _saveaccountconf "LOG_LEVEL" "$_log_level"
4227 LOG_LEVEL="$_log_level"
4228 fi
4229
5ea6e9c9 4230 _processAccountConf
4231 fi
4232
dcf9cb58 4233 if [ "$DEBUG" ] ; then
4234 version
4235 fi
a63b05a9 4236
4237 case "${_CMD}" in
c8e9a31e 4238 install) install "$_nocron" ;;
bc96082f 4239 uninstall) uninstall "$_nocron" ;;
52677b0a 4240 upgrade) upgrade ;;
a63b05a9 4241 issue)
0463b5d6 4242 issue "$_webroot" "$_domain" "$_altdomains" "$_keylength" "$_certpath" "$_keypath" "$_capath" "$_reloadcmd" "$_fullchainpath" "$_pre_hook" "$_post_hook" "$_renew_hook" "$_local_address"
a63b05a9 4243 ;;
a61fe418 4244 deploy)
4245 deploy "$_domain" "$_deploy_hook" "$_ecc"
4246 ;;
10afcaca 4247 signcsr)
4248 signcsr "$_csr" "$_webroot"
4249 ;;
4250 showcsr)
4251 showcsr "$_csr" "$_domain"
4252 ;;
a63b05a9 4253 installcert)
43822d37 4254 installcert "$_domain" "$_certpath" "$_keypath" "$_capath" "$_reloadcmd" "$_fullchainpath" "$_ecc"
a63b05a9 4255 ;;
4256 renew)
43822d37 4257 renew "$_domain" "$_ecc"
a63b05a9 4258 ;;
4259 renewAll)
cc179731 4260 renewAll "$_stopRenewOnError"
a63b05a9 4261 ;;
4262 revoke)
43822d37 4263 revoke "$_domain" "$_ecc"
a63b05a9 4264 ;;
0c00e870 4265 deactivate)
3f4513b3 4266 deactivate "$_domain,$_altdomains"
eb59817e 4267 ;;
4268 registeraccount)
4269 registeraccount
4270 ;;
4271 updateaccount)
4272 updateaccount
4273 ;;
6d7eda3e 4274 list)
dcf4f8f6 4275 list "$_listraw"
6d7eda3e 4276 ;;
a63b05a9 4277 installcronjob) installcronjob ;;
4278 uninstallcronjob) uninstallcronjob ;;
4279 cron) cron ;;
4280 toPkcs)
43822d37 4281 toPkcs "$_domain" "$_password" "$_ecc"
a63b05a9 4282 ;;
4283 createAccountKey)
5fbc47eb 4284 createAccountKey "$_accountkeylength"
a63b05a9 4285 ;;
4286 createDomainKey)
4287 createDomainKey "$_domain" "$_keylength"
4288 ;;
4289 createCSR)
43822d37 4290 createCSR "$_domain" "$_altdomains" "$_ecc"
a63b05a9 4291 ;;
4292
4293 *)
4294 _err "Invalid command: $_CMD"
4295 showhelp;
4296 return 1
4297 ;;
4298 esac
d3595686 4299 _ret="$?"
4300 if [ "$_ret" != "0" ] ; then
4301 return $_ret
4302 fi
a63b05a9 4303
5ea6e9c9 4304 if [ "${_CMD}" = "install" ] ; then
d0871bda 4305 if [ "$_log" ] ; then
4306 if [ -z "$LOG_FILE" ] ; then
4307 LOG_FILE="$DEFAULT_LOG_FILE"
4308 fi
4309 _saveaccountconf "LOG_FILE" "$LOG_FILE"
5ea6e9c9 4310 fi
a73c5b33 4311
4312 if [ "$_log_level" ] ; then
4313 _saveaccountconf "LOG_LEVEL" "$_log_level"
4314 fi
5ea6e9c9 4315 _processAccountConf
b5eb4b90 4316 fi
635695ec 4317
a63b05a9 4318}
4319
4320
8663fb7e 4321if [ "$INSTALLONLINE" ] ; then
d1f97fc8 4322 INSTALLONLINE=""
4a0f23e2 4323 _installOnline $BRANCH
4324 exit
4325fi
4c3b3608 4326
a63b05a9 4327
276b51d9 4328
4329
a63b05a9 4330
319e0ae3 4331main() {
4332 [ -z "$1" ] && showhelp && return
4333 if _startswith "$1" '-' ; then _process "$@"; else "$@";fi
4334}
e69a7c38 4335
aa7b82de 4336
4337main "$@"
4338
4339
4340