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