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