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