]> git.proxmox.com Git - mirror_acme.sh.git/blame - acme.sh
2.4.1 fix bug.
[mirror_acme.sh.git] / acme.sh
CommitLineData
0a7c9364 1#!/usr/bin/env sh
bfdf1f48 2
fac1e367 3VER=2.4.1
a7b7355d 4
6cc11ffb 5PROJECT_NAME="acme.sh"
a7b7355d 6
6cc11ffb 7PROJECT_ENTRY="acme.sh"
8
9PROJECT="https://github.com/Neilpang/$PROJECT_NAME"
4c3b3608 10
11DEFAULT_CA="https://acme-v01.api.letsencrypt.org"
c93ec933 12DEFAULT_AGREEMENT="https://letsencrypt.org/documents/LE-SA-v1.1.1-August-1-2016.pdf"
4c3b3608 13
08928b48 14DEFAULT_USER_AGENT="$PROJECT_ENTRY client v$VER : $PROJECT"
bbbdcb09 15
4c3b3608 16STAGE_CA="https://acme-staging.api.letsencrypt.org"
17
18VTYPE_HTTP="http-01"
19VTYPE_DNS="dns-01"
e22bcf7c 20VTYPE_TLS="tls-sni-01"
21VTYPE_TLS2="tls-sni-02"
22
523c7682 23MAX_RENEW=80
24
4a4dacb5 25DEFAULT_DNS_SLEEP=120
26
e22bcf7c 27W_TLS="tls"
4c3b3608 28
ec603bee 29STATE_VERIFIED="verified_ok"
30
88fab7d6 31BEGIN_CSR="-----BEGIN CERTIFICATE REQUEST-----"
32END_CSR="-----END CERTIFICATE REQUEST-----"
33
34BEGIN_CERT="-----BEGIN CERTIFICATE-----"
35END_CERT="-----END CERTIFICATE-----"
36
cc179731 37RENEW_SKIP=2
38
43822d37 39ECC_SEP="_"
40ECC_SUFFIX="${ECC_SEP}ecc"
41
8663fb7e 42if [ -z "$AGREEMENT" ] ; then
4c3b3608 43 AGREEMENT="$DEFAULT_AGREEMENT"
44fi
45
4c3b3608 46
00a50605 47
43822d37 48__green() {
49 printf '\033[1;31;32m'
50 printf -- "$1"
51 printf '\033[0m'
52}
53
54__red() {
55 printf '\033[1;31;40m'
56 printf -- "$1"
57 printf '\033[0m'
58}
00a50605 59
4c3b3608 60_info() {
8663fb7e 61 if [ -z "$2" ] ; then
43822d37 62 printf -- "[$(date)] $1"
63 else
64 printf -- "[$(date)] $1='$2'"
65 fi
66 printf "\n"
67}
68
69
43822d37 70_err_e() {
71 if [ -z "$2" ] ; then
72 __red "$1" >&2
4c3b3608 73 else
43822d37 74 __red "$1='$2'" >&2
4c3b3608 75 fi
fac1e367 76 printf "\n" >&2
4c3b3608 77}
78
79_err() {
43822d37 80 printf -- "[$(date)] " >&2
fac1e367 81 _err_e "$@"
4c3b3608 82 return 1
83}
84
43822d37 85_usage() {
86 version
87 _err_e "$@"
43822d37 88}
89
c60883ef 90_debug() {
8663fb7e 91 if [ -z "$DEBUG" ] ; then
c60883ef 92 return
93 fi
43822d37 94
95 if [ -z "$2" ] ; then
96 printf -- "[$(date)] $1" >&2
97 else
98 printf -- "[$(date)] $1='$2'" >&2
99 fi
100
31a5487c 101 printf "\n" >&2
c60883ef 102 return 0
103}
104
a63b05a9 105_debug2() {
8663fb7e 106 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ] ; then
a63b05a9 107 _debug "$@"
108 fi
109 return
110}
111
22ea4004 112_debug3() {
113 if [ "$DEBUG" ] && [ "$DEBUG" -ge "3" ] ; then
114 _debug "$@"
115 fi
116 return
117}
118
dceb3aca 119_startswith(){
120 _str="$1"
121 _sub="$2"
19539575 122 echo "$_str" | grep "^$_sub" >/dev/null 2>&1
dceb3aca 123}
124
43822d37 125_endswith(){
126 _str="$1"
127 _sub="$2"
128 echo "$_str" | grep -- "$_sub\$" >/dev/null 2>&1
129}
130
dceb3aca 131_contains(){
132 _str="$1"
133 _sub="$2"
43822d37 134 echo "$_str" | grep -- "$_sub" >/dev/null 2>&1
dceb3aca 135}
136
c53da1ef 137_hasfield() {
138 _str="$1"
139 _field="$2"
140 _sep="$3"
141 if [ -z "$_field" ] ; then
43822d37 142 _usage "Usage: str field [sep]"
c53da1ef 143 return 1
144 fi
145
146 if [ -z "$_sep" ] ; then
147 _sep=","
148 fi
149
150 for f in $(echo "$_str" | tr ',' ' ') ; do
151 if [ "$f" = "$_field" ] ; then
152 _debug "'$_str' contains '$_field'"
153 return 0 #contains ok
154 fi
155 done
156 _debug "'$_str' does not contain '$_field'"
157 return 1 #not contains
158}
159
dceb3aca 160_exists(){
c60883ef 161 cmd="$1"
8663fb7e 162 if [ -z "$cmd" ] ; then
43822d37 163 _usage "Usage: _exists cmd"
c60883ef 164 return 1
165 fi
eac18b1c 166 if type command >/dev/null 2>&1 ; then
19539575 167 command -v "$cmd" >/dev/null 2>&1
eac18b1c 168 else
19539575 169 type "$cmd" >/dev/null 2>&1
eac18b1c 170 fi
c60883ef 171 ret="$?"
a63b05a9 172 _debug2 "$cmd exists=$ret"
c60883ef 173 return $ret
174}
175
00a50605 176#a + b
177_math(){
178 expr "$@"
179}
180
181_h_char_2_dec() {
182 _ch=$1
183 case "${_ch}" in
184 a|A)
19539575 185 printf "10"
00a50605 186 ;;
187 b|B)
19539575 188 printf "11"
00a50605 189 ;;
190 c|C)
19539575 191 printf "12"
00a50605 192 ;;
193 d|D)
19539575 194 printf "13"
00a50605 195 ;;
196 e|E)
19539575 197 printf "14"
00a50605 198 ;;
199 f|F)
19539575 200 printf "15"
00a50605 201 ;;
202 *)
19539575 203 printf "%s" "$_ch"
00a50605 204 ;;
19539575 205 esac
00a50605 206
207}
208
fac1e367 209
210_URGLY_PRINTF=""
211if [ "$(printf '\x41')" != 'A' ] ; then
212 _URGLY_PRINTF=1
213fi
214
4c3b3608 215_h2b() {
216 hex=$(cat)
217 i=1
218 j=2
00a50605 219 if _exists let ; then
220 uselet="1"
221 fi
7270f277 222 _debug2 uselet "$uselet"
223 _debug2 _URGLY_PRINTF "$_URGLY_PRINTF"
19539575 224 while true ; do
00a50605 225 if [ -z "$_URGLY_PRINTF" ] ; then
19539575 226 h="$(printf $hex | cut -c $i-$j)"
00a50605 227 if [ -z "$h" ] ; then
228 break;
229 fi
230 printf "\x$h"
231 else
19539575 232 ic="$(printf $hex | cut -c $i)"
233 jc="$(printf $hex | cut -c $j)"
00a50605 234 if [ -z "$ic$jc" ] ; then
235 break;
236 fi
19539575 237 ic="$(_h_char_2_dec "$ic")"
238 jc="$(_h_char_2_dec "$jc")"
00a50605 239 printf '\'"$(printf %o "$(_math $ic \* 16 + $jc)")"
4c3b3608 240 fi
00a50605 241 if [ "$uselet" ] ; then
f4312b44 242 let "i+=2" >/dev/null
243 let "j+=2" >/dev/null
00a50605 244 else
245 i="$(_math $i + 2)"
246 j="$(_math $j + 2)"
247 fi
4c3b3608 248 done
249}
250
c60883ef 251#options file
252_sed_i() {
253 options="$1"
254 filename="$2"
8663fb7e 255 if [ -z "$filename" ] ; then
43822d37 256 _usage "Usage:_sed_i options filename"
c60883ef 257 return 1
258 fi
14f3dbb7 259 _debug2 options "$options"
260 if sed -h 2>&1 | grep "\-i\[SUFFIX]" >/dev/null 2>&1; then
c60883ef 261 _debug "Using sed -i"
14f3dbb7 262 sed -i "$options" "$filename"
c60883ef 263 else
264 _debug "No -i support in sed"
19539575 265 text="$(cat "$filename")"
c60883ef 266 echo "$text" | sed "$options" > "$filename"
267 fi
268}
269
22ea4004 270_egrep_o() {
271 if _contains "$(egrep -o 2>&1)" "egrep: illegal option -- o" ; then
272 sed -n 's/.*\('"$1"'\).*/\1/p'
273 else
274 egrep -o "$1"
275 fi
276}
277
88fab7d6 278#Usage: file startline endline
279_getfile() {
280 filename="$1"
281 startline="$2"
282 endline="$3"
8663fb7e 283 if [ -z "$endline" ] ; then
43822d37 284 _usage "Usage: file startline endline"
88fab7d6 285 return 1
286 fi
287
19539575 288 i="$(grep -n -- "$startline" "$filename" | cut -d : -f 1)"
8663fb7e 289 if [ -z "$i" ] ; then
88fab7d6 290 _err "Can not find start line: $startline"
291 return 1
292 fi
19539575 293 i="$(_math "$i" + 1)"
294 _debug i "$i"
88fab7d6 295
19539575 296 j="$(grep -n -- "$endline" "$filename" | cut -d : -f 1)"
8663fb7e 297 if [ -z "$j" ] ; then
88fab7d6 298 _err "Can not find end line: $endline"
299 return 1
300 fi
19539575 301 j="$(_math "$j" - 1)"
302 _debug j "$j"
88fab7d6 303
19539575 304 sed -n "$i,${j}p" "$filename"
88fab7d6 305
306}
307
308#Usage: multiline
4c3b3608 309_base64() {
8663fb7e 310 if [ "$1" ] ; then
88fab7d6 311 openssl base64 -e
312 else
313 openssl base64 -e | tr -d '\r\n'
314 fi
315}
316
317#Usage: multiline
318_dbase64() {
8663fb7e 319 if [ "$1" ] ; then
88fab7d6 320 openssl base64 -d -A
321 else
322 openssl base64 -d
323 fi
324}
325
e22bcf7c 326#Usage: hashalg [outputhex]
88fab7d6 327#Output Base64-encoded digest
328_digest() {
329 alg="$1"
8663fb7e 330 if [ -z "$alg" ] ; then
43822d37 331 _usage "Usage: _digest hashalg"
88fab7d6 332 return 1
333 fi
334
e22bcf7c 335 outputhex="$2"
336
8663fb7e 337 if [ "$alg" = "sha256" ] ; then
e22bcf7c 338 if [ "$outputhex" ] ; then
339 echo $(openssl dgst -sha256 -hex | cut -d = -f 2)
340 else
341 openssl dgst -sha256 -binary | _base64
342 fi
88fab7d6 343 else
344 _err "$alg is not supported yet"
345 return 1
346 fi
347
348}
349
350#Usage: keyfile hashalg
351#Output: Base64-encoded signature value
352_sign() {
353 keyfile="$1"
354 alg="$2"
8663fb7e 355 if [ -z "$alg" ] ; then
43822d37 356 _usage "Usage: _sign keyfile hashalg"
88fab7d6 357 return 1
358 fi
359
8663fb7e 360 if [ "$alg" = "sha256" ] ; then
88fab7d6 361 openssl dgst -sha256 -sign "$keyfile" | _base64
362 else
363 _err "$alg is not supported yet"
364 return 1
fac1e367 365 fi
88fab7d6 366
4c3b3608 367}
368
43822d37 369#keylength
370_isEccKey() {
371 _length="$1"
372
373 if [ -z "$_length" ] ;then
374 return 1
375 fi
376
377 [ "$_length" != "1024" ] \
378 && [ "$_length" != "2048" ] \
379 && [ "$_length" != "3172" ] \
380 && [ "$_length" != "4096" ] \
381 && [ "$_length" != "8192" ]
382}
383
e22bcf7c 384# _createkey 2048|ec-256 file
385_createkey() {
386 length="$1"
387 f="$2"
43822d37 388 eccname="$length"
e22bcf7c 389 if _startswith "$length" "ec-" ; then
e22bcf7c 390 length=$(printf $length | cut -d '-' -f 2-100)
e22bcf7c 391
e22bcf7c 392 if [ "$length" = "256" ] ; then
393 eccname="prime256v1"
394 fi
395 if [ "$length" = "384" ] ; then
396 eccname="secp384r1"
397 fi
398 if [ "$length" = "521" ] ; then
399 eccname="secp521r1"
400 fi
43822d37 401
e22bcf7c 402 fi
403
43822d37 404 if [ -z "$length" ] ; then
405 length=2048
406 fi
407
408 _info "Use length $length"
409
410 if _isEccKey "$length" ; then
411 _info "Using ec name: $eccname"
e22bcf7c 412 openssl ecparam -name $eccname -genkey 2>/dev/null > "$f"
413 else
43822d37 414 _info "Using RSA: $length"
e22bcf7c 415 openssl genrsa $length 2>/dev/null > "$f"
416 fi
43822d37 417
418 if [ "$?" != "0" ] ; then
419 _err "Create key error."
420 return 1
421 fi
e22bcf7c 422}
423
424#_createcsr cn san_list keyfile csrfile conf
425_createcsr() {
426 _debug _createcsr
427 domain="$1"
428 domainlist="$2"
429 key="$3"
430 csr="$4"
431 csrconf="$5"
432 _debug2 domain "$domain"
433 _debug2 domainlist "$domainlist"
434 if [ -z "$domainlist" ] || [ "$domainlist" = "no" ]; then
435 #single domain
436 _info "Single domain" "$domain"
437 printf "[ req_distinguished_name ]\n[ req ]\ndistinguished_name = req_distinguished_name\n" > "$csrconf"
438 openssl req -new -sha256 -key "$key" -subj "/CN=$domain" -config "$csrconf" -out "$csr"
439 else
440 if _contains "$domainlist" "," ; then
441 alt="DNS:$(echo $domainlist | sed "s/,/,DNS:/g")"
442 else
443 alt="DNS:$domainlist"
444 fi
445 #multi
446 _info "Multi domain" "$alt"
43822d37 447 printf -- "[ req_distinguished_name ]\n[ req ]\ndistinguished_name = req_distinguished_name\nreq_extensions = v3_req\n[ v3_req ]\nkeyUsage = nonRepudiation, digitalSignature, keyEncipherment\nsubjectAltName=$alt" > "$csrconf"
e22bcf7c 448 openssl req -new -sha256 -key "$key" -subj "/CN=$domain" -config "$csrconf" -out "$csr"
449 fi
450}
451
452#_signcsr key csr conf cert
453_signcsr() {
454 key="$1"
455 csr="$2"
456 conf="$3"
457 cert="$4"
5aa146a5 458 _debug "_signcsr"
e22bcf7c 459
5aa146a5 460 _msg="$(openssl x509 -req -days 365 -in "$csr" -signkey "$key" -extensions v3_req -extfile "$conf" -out "$cert" 2>&1)"
461 _ret="$?"
462 _debug "$_msg"
463 return $_ret
e22bcf7c 464}
465
34c27e09 466_ss() {
467 _port="$1"
edf08da6 468
469 if _exists "ss" ; then
470 _debug "Using: ss"
19539575 471 ss -ntpl | grep ":$_port "
edf08da6 472 return 0
473 fi
474
475 if _exists "netstat" ; then
251fc37c 476 _debug "Using: netstat"
ccb96535 477 if netstat -h 2>&1 | grep "\-p proto" >/dev/null ; then
478 #for windows version netstat tool
19539575 479 netstat -anb -p tcp | grep "LISTENING" | grep ":$_port "
ccb96535 480 else
14165e5a 481 if netstat -help 2>&1 | grep "\-p protocol" >/dev/null ; then
19539575 482 netstat -an -p tcp | grep LISTEN | grep ":$_port "
22ea4004 483 elif netstat -help 2>&1 | grep -- '-P protocol' >/dev/null ; then
484 #for solaris
e3c66532 485 netstat -an -P tcp | grep "\.$_port " | grep "LISTEN"
edf08da6 486 else
19539575 487 netstat -ntpl | grep ":$_port "
edf08da6 488 fi
ccb96535 489 fi
34c27e09 490 return 0
491 fi
edf08da6 492
34c27e09 493 return 1
494}
495
43822d37 496#domain [password] [isEcc]
ac2d5123 497toPkcs() {
498 domain="$1"
499 pfxPassword="$2"
8663fb7e 500 if [ -z "$domain" ] ; then
43822d37 501 _usage "Usage: $PROJECT_ENTRY --toPkcs -d domain [--password pfx-password]"
ac2d5123 502 return 1
503 fi
504
43822d37 505 _isEcc="$3"
ac2d5123 506
43822d37 507 _initpath "$domain" "$_isEcc"
508
8663fb7e 509 if [ "$pfxPassword" ] ; then
ac2d5123 510 openssl pkcs12 -export -out "$CERT_PFX_PATH" -inkey "$CERT_KEY_PATH" -in "$CERT_PATH" -certfile "$CA_CERT_PATH" -password "pass:$pfxPassword"
511 else
512 openssl pkcs12 -export -out "$CERT_PFX_PATH" -inkey "$CERT_KEY_PATH" -in "$CERT_PATH" -certfile "$CA_CERT_PATH"
513 fi
514
8663fb7e 515 if [ "$?" = "0" ] ; then
ac2d5123 516 _info "Success, Pfx is exported to: $CERT_PFX_PATH"
517 fi
518
519}
520
4c3b3608 521#domain [2048]
522createAccountKey() {
523 _info "Creating account key"
8663fb7e 524 if [ -z "$1" ] ; then
43822d37 525 _usage "Usage: $PROJECT_ENTRY --createAccountKey -d domain.com [--accountkeylength 2048]"
4c3b3608 526 return
527 fi
528
529 account=$1
530 length=$2
b5eb4b90 531 _debug account "$account"
532 _debug length "$length"
dceb3aca 533 if _startswith "$length" "ec-" ; then
4c3b3608 534 length=2048
535 fi
536
8663fb7e 537 if [ -z "$2" ] || [ "$2" = "no" ] ; then
4c3b3608 538 _info "Use default length 2048"
539 length=2048
540 fi
541 _initpath
542
8663fb7e 543 if [ -f "$ACCOUNT_KEY_PATH" ] ; then
4c3b3608 544 _info "Account key exists, skip"
545 return
546 else
547 #generate account key
31a5487c 548 _createkey "$length" "$ACCOUNT_KEY_PATH"
4c3b3608 549 fi
550
551}
552
43822d37 553#domain [length]
4c3b3608 554createDomainKey() {
555 _info "Creating domain key"
8663fb7e 556 if [ -z "$1" ] ; then
43822d37 557 _usage "Usage: $PROJECT_ENTRY --createDomainKey -d domain.com [ --keylength 2048 ]"
4c3b3608 558 return
559 fi
560
561 domain=$1
e22bcf7c 562 length=$2
43822d37 563
564 _initpath $domain "$length"
e22bcf7c 565
8663fb7e 566 if [ ! -f "$CERT_KEY_PATH" ] || ( [ "$FORCE" ] && ! [ "$IS_RENEW" ] ); then
e22bcf7c 567 _createkey "$length" "$CERT_KEY_PATH"
4c3b3608 568 else
8663fb7e 569 if [ "$IS_RENEW" ] ; then
4c3b3608 570 _info "Domain key exists, skip"
571 return 0
572 else
573 _err "Domain key exists, do you want to overwrite the key?"
41e3eafa 574 _err "Add '--force', and try again."
4c3b3608 575 return 1
576 fi
577 fi
578
579}
580
43822d37 581# domain domainlist isEcc
4c3b3608 582createCSR() {
583 _info "Creating csr"
8663fb7e 584 if [ -z "$1" ] ; then
43822d37 585 _usage "Usage: $PROJECT_ENTRY --createCSR -d domain1.com [-d domain2.com -d domain3.com ... ]"
4c3b3608 586 return
587 fi
4c3b3608 588
43822d37 589 domain="$1"
590 domainlist="$2"
591 _isEcc="$3"
592
593 _initpath "$domain" "$_isEcc"
4c3b3608 594
8663fb7e 595 if [ -f "$CSR_PATH" ] && [ "$IS_RENEW" ] && [ -z "$FORCE" ]; then
4c3b3608 596 _info "CSR exists, skip"
597 return
598 fi
599
43822d37 600 if [ ! -f "$CERT_KEY_PATH" ] ; then
601 _err "The key file is not found: $CERT_KEY_PATH"
602 _err "Please create the key file first."
603 return 1
604 fi
e22bcf7c 605 _createcsr "$domain" "$domainlist" "$CERT_KEY_PATH" "$CSR_PATH" "$DOMAIN_SSL_CONF"
606
4c3b3608 607}
608
166096dc 609_urlencode() {
4c3b3608 610 __n=$(cat)
611 echo $__n | tr '/+' '_-' | tr -d '= '
612}
613
614_time2str() {
615 #BSD
616 if date -u -d@$1 2>/dev/null ; then
617 return
618 fi
619
620 #Linux
621 if date -u -r $1 2>/dev/null ; then
622 return
623 fi
624
22ea4004 625 #Soaris
626 if _exists adb ; then
627 echo $(echo "0t${1}=Y" | adb)
628 fi
629
4c3b3608 630}
631
eae29099 632_normalizeJson() {
633 sed "s/\" *: *\([\"{\[]\)/\":\1/g" | sed "s/^ *\([^ ]\)/\1/" | tr -d "\r\n"
634}
635
44df2967 636_stat() {
637 #Linux
638 if stat -c '%U:%G' "$1" 2>/dev/null ; then
639 return
640 fi
641
642 #BSD
643 if stat -f '%Su:%Sg' "$1" 2>/dev/null ; then
644 return
645 fi
3ad08e95
TB
646
647 return 1; #error, 'stat' not found
44df2967 648}
649
166096dc 650#keyfile
651_calcjwk() {
652 keyfile="$1"
8663fb7e 653 if [ -z "$keyfile" ] ; then
43822d37 654 _usage "Usage: _calcjwk keyfile"
166096dc 655 return 1
656 fi
657 EC_SIGN=""
658 if grep "BEGIN RSA PRIVATE KEY" "$keyfile" > /dev/null 2>&1 ; then
659 _debug "RSA key"
660 pub_exp=$(openssl rsa -in $keyfile -noout -text | grep "^publicExponent:"| cut -d '(' -f 2 | cut -d 'x' -f 2 | cut -d ')' -f 1)
8663fb7e 661 if [ "${#pub_exp}" = "5" ] ; then
166096dc 662 pub_exp=0$pub_exp
663 fi
22ea4004 664 _debug3 pub_exp "$pub_exp"
166096dc 665
666 e=$(echo $pub_exp | _h2b | _base64)
22ea4004 667 _debug3 e "$e"
166096dc 668
669 modulus=$(openssl rsa -in $keyfile -modulus -noout | cut -d '=' -f 2 )
22ea4004 670 _debug3 modulus "$modulus"
19539575 671 n="$(printf "%s" "$modulus"| _h2b | _base64 | _urlencode )"
166096dc 672 jwk='{"e": "'$e'", "kty": "RSA", "n": "'$n'"}'
22ea4004 673 _debug3 jwk "$jwk"
166096dc 674
675 HEADER='{"alg": "RS256", "jwk": '$jwk'}'
22ea4004 676 HEADERPLACE_PART1='{"nonce": "'
677 HEADERPLACE_PART2='", "alg": "RS256", "jwk": '$jwk'}'
166096dc 678 elif grep "BEGIN EC PRIVATE KEY" "$keyfile" > /dev/null 2>&1 ; then
679 _debug "EC key"
680 EC_SIGN="1"
681 crv="$(openssl ec -in $keyfile -noout -text 2>/dev/null | grep "^NIST CURVE:" | cut -d ":" -f 2 | tr -d " \r\n")"
22ea4004 682 _debug3 crv "$crv"
166096dc 683
684 pubi="$(openssl ec -in $keyfile -noout -text 2>/dev/null | grep -n pub: | cut -d : -f 1)"
00a50605 685 pubi=$(_math $pubi + 1)
22ea4004 686 _debug3 pubi "$pubi"
166096dc 687
688 pubj="$(openssl ec -in $keyfile -noout -text 2>/dev/null | grep -n "ASN1 OID:" | cut -d : -f 1)"
00a50605 689 pubj=$(_math $pubj + 1)
22ea4004 690 _debug3 pubj "$pubj"
166096dc 691
692 pubtext="$(openssl ec -in $keyfile -noout -text 2>/dev/null | sed -n "$pubi,${pubj}p" | tr -d " \n\r")"
22ea4004 693 _debug3 pubtext "$pubtext"
166096dc 694
695 xlen="$(printf "$pubtext" | tr -d ':' | wc -c)"
00a50605 696 xlen=$(_math $xlen / 4)
22ea4004 697 _debug3 xlen "$xlen"
00a50605 698
19539575 699 xend=$(_math "$xend" + 1)
166096dc 700 x="$(printf $pubtext | cut -d : -f 2-$xend)"
22ea4004 701 _debug3 x "$x"
166096dc 702
703 x64="$(printf $x | tr -d : | _h2b | _base64 | _urlencode)"
22ea4004 704 _debug3 x64 "$x64"
00a50605 705
19539575 706 xend=$(_math "$xend" + 1)
166096dc 707 y="$(printf $pubtext | cut -d : -f $xend-10000)"
22ea4004 708 _debug3 y "$y"
166096dc 709
710 y64="$(printf $y | tr -d : | _h2b | _base64 | _urlencode)"
22ea4004 711 _debug3 y64 "$y64"
166096dc 712
713 jwk='{"kty": "EC", "crv": "'$crv'", "x": "'$x64'", "y": "'$y64'"}'
22ea4004 714 _debug3 jwk "$jwk"
166096dc 715
716 HEADER='{"alg": "ES256", "jwk": '$jwk'}'
22ea4004 717 HEADERPLACE_PART1='{"nonce": "'
718 HEADERPLACE_PART2='", "alg": "ES256", "jwk": '$jwk'}'
166096dc 719 else
720 _err "Only RSA or EC key is supported."
721 return 1
722 fi
723
22ea4004 724 _debug3 HEADER "$HEADER"
166096dc 725}
fac1e367 726
727
728_mktemp() {
729 if _exists mktemp ; then
730 mktemp
731 fi
732}
733
734_inithttp() {
735
736 if [ -z "$HTTP_HEADER" ] ; then
737 HTTP_HEADER="$(_mktemp)"
738 _debug2 HTTP_HEADER "$HTTP_HEADER"
739 fi
740
741 if [ -z "$CURL" ] ; then
742 CURL="curl -L --silent --dump-header $HTTP_HEADER "
743 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ] ; then
744 _CURL_DUMP="$(_mktemp)"
745 CURL="$CURL --trace-ascii $_CURL_DUMP "
746 fi
747
748 if [ "$HTTPS_INSECURE" ] ; then
749 CURL="$CURL --insecure "
750 fi
751 fi
752
753 if [ -z "$WGET" ] ; then
754 WGET="wget -q"
755 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ] ; then
756 WGET="$WGET -d "
757 fi
758 if [ "$HTTPS_INSECURE" ] ; then
759 WGET="$WGET --no-check-certificate "
760 fi
761 fi
762
763}
764
765
c839b2b0 766# body url [needbase64] [POST|PUT]
c60883ef 767_post() {
768 body="$1"
769 url="$2"
770 needbase64="$3"
a4270efa 771 httpmethod="$4"
c60883ef 772
a4270efa 773 if [ -z "$httpmethod" ] ; then
774 httpmethod="POST"
775 fi
776 _debug $httpmethod
484d9d2a 777 _debug "url" "$url"
30de13b4 778 _debug2 "body" "$body"
fac1e367 779
780 _inithttp
781
c60883ef 782 if _exists "curl" ; then
fac1e367 783 _CURL="$CURL"
ec9fc8cb 784 _debug "_CURL" "$_CURL"
8663fb7e 785 if [ "$needbase64" ] ; then
63a195e5 786 response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" --data "$body" "$url" | _base64)"
c60883ef 787 else
63a195e5 788 response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" --data "$body" "$url" )"
c60883ef 789 fi
16679b57 790 _ret="$?"
687cfcc2 791 if [ "$_ret" != "0" ] ; then
87ab2d90 792 _err "Please refer to https://curl.haxx.se/libcurl/c/libcurl-errors.html for error code: $_ret"
793 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ] ; then
794 _err "Here is the curl dump log:"
795 _err "$(cat "$_CURL_DUMP")"
796 fi
687cfcc2 797 fi
d0b748a4 798 elif _exists "wget" ; then
ec9fc8cb 799 _debug "WGET" "$WGET"
8663fb7e 800 if [ "$needbase64" ] ; then
8fb9a709 801 if [ "$httpmethod"="POST" ] ; then
802 response="$($WGET -S -O - --user-agent="$USER_AGENT" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --post-data="$body" "$url" 2>"$HTTP_HEADER" | _base64)"
803 else
804 response="$($WGET -S -O - --user-agent="$USER_AGENT" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --method $httpmethod --body-data="$body" "$url" 2>"$HTTP_HEADER" | _base64)"
805 fi
c60883ef 806 else
8fb9a709 807 if [ "$httpmethod"="POST" ] ; then
808 response="$($WGET -S -O - --user-agent="$USER_AGENT" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --post-data="$body" "$url" 2>"$HTTP_HEADER")"
809 else
810 response="$($WGET -S -O - --user-agent="$USER_AGENT" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --method $httpmethod --body-data="$body" "$url" 2>"$HTTP_HEADER")"
811 fi
c60883ef 812 fi
16679b57 813 _ret="$?"
687cfcc2 814 if [ "$_ret" != "0" ] ; then
815 _err "Please refer to https://www.gnu.org/software/wget/manual/html_node/Exit-Status.html for error code: $_ret"
816 fi
c60883ef 817 _sed_i "s/^ *//g" "$HTTP_HEADER"
d0b748a4 818 else
819 _ret="$?"
820 _err "Neither curl nor wget is found, can not do $httpmethod."
c60883ef 821 fi
16679b57 822 _debug "_ret" "$_ret"
19539575 823 printf "%s" "$response"
16679b57 824 return $_ret
c60883ef 825}
826
75da0713 827# url getheader timeout
c60883ef 828_get() {
a4270efa 829 _debug GET
c60883ef 830 url="$1"
831 onlyheader="$2"
75da0713 832 t="$3"
c60883ef 833 _debug url $url
75da0713 834 _debug "timeout" "$t"
fac1e367 835
836 _inithttp
837
c60883ef 838 if _exists "curl" ; then
75da0713 839 _CURL="$CURL"
840 if [ "$t" ] ; then
841 _CURL="$_CURL --connect-timeout $t"
842 fi
843 _debug "_CURL" "$_CURL"
8663fb7e 844 if [ "$onlyheader" ] ; then
75da0713 845 $_CURL -I --user-agent "$USER_AGENT" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" $url
c60883ef 846 else
75da0713 847 $_CURL --user-agent "$USER_AGENT" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" $url
c60883ef 848 fi
9aaf36cd 849 ret=$?
fac1e367 850 if [ "$ret" != "0" ] ; then
851 _err "Please refer to https://curl.haxx.se/libcurl/c/libcurl-errors.html for error code: $_ret"
852 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ] ; then
853 _err "Here is the curl dump log:"
854 _err "$(cat "$_CURL_DUMP")"
855 fi
856 fi
d0b748a4 857 elif _exists "wget" ; then
75da0713 858 _WGET="$WGET"
859 if [ "$t" ] ; then
860 _WGET="$_WGET --timeout=$t"
861 fi
862 _debug "_WGET" "$_WGET"
8663fb7e 863 if [ "$onlyheader" ] ; then
75da0713 864 $_WGET --user-agent="$USER_AGENT" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" -S -O /dev/null $url 2>&1 | sed 's/^[ ]*//g'
c60883ef 865 else
75da0713 866 $_WGET --user-agent="$USER_AGENT" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" -O - $url
c60883ef 867 fi
9aaf36cd 868 ret=$?
fac1e367 869 if [ "$ret" != "0" ] ; then
870 _err "Please refer to https://www.gnu.org/software/wget/manual/html_node/Exit-Status.html for error code: $_ret"
871 fi
d0b748a4 872 else
873 ret=$?
874 _err "Neither curl nor wget is found, can not do GET."
9aaf36cd 875 fi
ec9fc8cb 876 _debug "ret" "$ret"
c60883ef 877 return $ret
878}
166096dc 879
fac1e367 880
166096dc 881# url payload needbase64 keyfile
4c3b3608 882_send_signed_request() {
883 url=$1
884 payload=$2
885 needbase64=$3
166096dc 886 keyfile=$4
8663fb7e 887 if [ -z "$keyfile" ] ; then
166096dc 888 keyfile="$ACCOUNT_KEY_PATH"
889 fi
4c3b3608 890 _debug url $url
891 _debug payload "$payload"
892
166096dc 893 if ! _calcjwk "$keyfile" ; then
894 return 1
895 fi
c60883ef 896
22ea4004 897 payload64=$(printf "%s" "$payload" | _base64 | _urlencode)
898 _debug3 payload64 $payload64
4c3b3608 899
900 nonceurl="$API/directory"
a272ee4f 901 _headers="$(_get $nonceurl "onlyheader")"
902
7012b91f 903 if [ "$?" != "0" ] ; then
904 _err "Can not connect to $nonceurl to get nonce."
905 return 1
906 fi
a272ee4f 907
22ea4004 908 _debug3 _headers "$_headers"
a272ee4f 909
910 nonce="$( echo "$_headers" | grep "Replay-Nonce:" | head -1 | tr -d "\r\n " | cut -d ':' -f 2)"
911
22ea4004 912 _debug3 nonce "$nonce"
4c3b3608 913
22ea4004 914 protected="$HEADERPLACE_PART1$nonce$HEADERPLACE_PART2"
915 _debug3 protected "$protected"
4c3b3608 916
166096dc 917 protected64="$(printf "$protected" | _base64 | _urlencode)"
22ea4004 918 _debug3 protected64 "$protected64"
166096dc 919
22ea4004 920 sig=$(printf "%s" "$protected64.$payload64" | _sign "$keyfile" "sha256" | _urlencode)
921 _debug3 sig "$sig"
4c3b3608 922
923 body="{\"header\": $HEADER, \"protected\": \"$protected64\", \"payload\": \"$payload64\", \"signature\": \"$sig\"}"
22ea4004 924 _debug3 body "$body"
4c3b3608 925
bbbdcb09 926
eae29099 927 response="$(_post "$body" $url "$needbase64")"
7012b91f 928 if [ "$?" != "0" ] ; then
929 _err "Can not post to $url."
930 return 1
931 fi
eae29099 932 _debug2 original "$response"
933
934 response="$( echo "$response" | _normalizeJson )"
4c3b3608 935
c60883ef 936 responseHeaders="$(cat $HTTP_HEADER)"
4c3b3608 937
a63b05a9 938 _debug2 responseHeaders "$responseHeaders"
939 _debug2 response "$response"
c60883ef 940 code="$(grep "^HTTP" $HTTP_HEADER | tail -1 | cut -d " " -f 2 | tr -d "\r\n" )"
4c3b3608 941 _debug code $code
942
943}
944
4c3b3608 945
946#setopt "file" "opt" "=" "value" [";"]
947_setopt() {
948 __conf="$1"
949 __opt="$2"
950 __sep="$3"
951 __val="$4"
952 __end="$5"
8663fb7e 953 if [ -z "$__opt" ] ; then
43822d37 954 _usage usage: _setopt '"file" "opt" "=" "value" [";"]'
4c3b3608 955 return
956 fi
8663fb7e 957 if [ ! -f "$__conf" ] ; then
4c3b3608 958 touch "$__conf"
959 fi
960
22ea4004 961 if grep -n "^$__opt$__sep" "$__conf" > /dev/null ; then
962 _debug3 OK
dceb3aca 963 if _contains "$__val" "&" ; then
4c3b3608 964 __val="$(echo $__val | sed 's/&/\\&/g')"
965 fi
966 text="$(cat $__conf)"
6dfaaa70 967 echo "$text" | sed "s|^$__opt$__sep.*$|$__opt$__sep$__val$__end|" > "$__conf"
4c3b3608 968
22ea4004 969 elif grep -n "^#$__opt$__sep" "$__conf" > /dev/null ; then
dceb3aca 970 if _contains "$__val" "&" ; then
4c3b3608 971 __val="$(echo $__val | sed 's/&/\\&/g')"
972 fi
973 text="$(cat $__conf)"
6dfaaa70 974 echo "$text" | sed "s|^#$__opt$__sep.*$|$__opt$__sep$__val$__end|" > "$__conf"
4c3b3608 975
976 else
22ea4004 977 _debug3 APP
4c3b3608 978 echo "$__opt$__sep$__val$__end" >> "$__conf"
979 fi
22ea4004 980 _debug2 "$(grep -n "^$__opt$__sep" $__conf)"
4c3b3608 981}
982
983#_savedomainconf key value
984#save to domain.conf
985_savedomainconf() {
986 key="$1"
987 value="$2"
8663fb7e 988 if [ "$DOMAIN_CONF" ] ; then
4d2f38b0 989 _setopt "$DOMAIN_CONF" "$key" "=" "\"$value\""
990 else
991 _err "DOMAIN_CONF is empty, can not save $key=$value"
992 fi
993}
994
995#_cleardomainconf key
996_cleardomainconf() {
997 key="$1"
998 if [ "$DOMAIN_CONF" ] ; then
999 _sed_i "s/^$key.*$//" "$DOMAIN_CONF"
4c3b3608 1000 else
1001 _err "DOMAIN_CONF is empty, can not save $key=$value"
1002 fi
1003}
1004
61623d22 1005#_readdomainconf key
1006_readdomainconf() {
1007 key="$1"
1008 if [ "$DOMAIN_CONF" ] ; then
1009 (
1010 eval $(grep "^$key *=" "$DOMAIN_CONF")
1011 eval "printf \"%s\" \"\$$key\""
1012 )
1013 else
1014 _err "DOMAIN_CONF is empty, can not read $key"
1015 fi
1016}
1017
4c3b3608 1018#_saveaccountconf key value
1019_saveaccountconf() {
1020 key="$1"
1021 value="$2"
8663fb7e 1022 if [ "$ACCOUNT_CONF_PATH" ] ; then
4d2f38b0 1023 _setopt "$ACCOUNT_CONF_PATH" "$key" "=" "\"$value\""
4c3b3608 1024 else
1025 _err "ACCOUNT_CONF_PATH is empty, can not save $key=$value"
1026 fi
1027}
1028
fac1e367 1029#_clearaccountconf key
1030_clearaccountconf() {
1031 key="$1"
1032 if [ "$ACCOUNT_CONF_PATH" ] ; then
1033 _sed_i "s/^$key.*$//" "$ACCOUNT_CONF_PATH"
1034 else
1035 _err "ACCOUNT_CONF_PATH is empty, can not clear $key"
1036 fi
1037}
1038
4c3b3608 1039_startserver() {
1040 content="$1"
6fc1447f 1041 _debug "startserver: $$"
1b2e940d 1042 nchelp="$(nc -h 2>&1)"
850c1128 1043
399306a1 1044 if echo "$nchelp" | grep "\-q[ ,]" >/dev/null ; then
850c1128 1045 _NC="nc -q 1 -l"
1046 else
f76eb452 1047 if echo "$nchelp" | grep "GNU netcat" >/dev/null && echo "$nchelp" | grep "\-c, \-\-close" >/dev/null ; then
1048 _NC="nc -c -l"
6d60f288 1049 elif echo "$nchelp" | grep "\-N" |grep "Shutdown the network socket after EOF on stdin" >/dev/null ; then
1050 _NC="nc -N -l"
f76eb452 1051 else
1052 _NC="nc -l"
1053 fi
4c3b3608 1054 fi
1b2e940d 1055
f76eb452 1056 _debug "_NC" "$_NC"
39c8f79f 1057 _debug Le_HTTPPort "$Le_HTTPPort"
4c3b3608 1058# while true ; do
8663fb7e 1059 if [ "$DEBUG" ] ; then
2c554a4b 1060 if ! printf "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC -p $Le_HTTPPort ; then
1061 printf "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC $Le_HTTPPort ;
3aff11f6 1062 fi
4c3b3608 1063 else
2c554a4b 1064 if ! printf "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC -p $Le_HTTPPort > /dev/null 2>&1; then
1065 printf "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC $Le_HTTPPort > /dev/null 2>&1
3aff11f6 1066 fi
4c3b3608 1067 fi
8663fb7e 1068 if [ "$?" != "0" ] ; then
051c706d 1069 _err "nc listen error."
6fc1447f 1070 exit 1
051c706d 1071 fi
4c3b3608 1072# done
1073}
1074
6fc1447f 1075_stopserver(){
4c3b3608 1076 pid="$1"
6fc1447f 1077 _debug "pid" "$pid"
8663fb7e 1078 if [ -z "$pid" ] ; then
6fc1447f 1079 return
1080 fi
e22bcf7c 1081
dcf9cb58 1082 _debug2 "Le_HTTPPort" "$Le_HTTPPort"
1083 if [ "$Le_HTTPPort" ] ; then
1084 if [ "$DEBUG" ] ; then
22ea4004 1085 _get "http://localhost:$Le_HTTPPort" "" 1
dcf9cb58 1086 else
22ea4004 1087 _get "http://localhost:$Le_HTTPPort" "" 1 >/dev/null 2>&1
dcf9cb58 1088 fi
1089 fi
1090
1091 _debug2 "Le_TLSPort" "$Le_TLSPort"
1092 if [ "$Le_TLSPort" ] ; then
1093 if [ "$DEBUG" ] ; then
75da0713 1094 _get "https://localhost:$Le_TLSPort" "" 1
1095 _get "https://localhost:$Le_TLSPort" "" 1
dcf9cb58 1096 else
75da0713 1097 _get "https://localhost:$Le_TLSPort" "" 1 >/dev/null 2>&1
1098 _get "https://localhost:$Le_TLSPort" "" 1 >/dev/null 2>&1
dcf9cb58 1099 fi
1100 fi
4c3b3608 1101}
1102
e22bcf7c 1103
1104# _starttlsserver san_a san_b port content
1105_starttlsserver() {
1106 _info "Starting tls server."
1107 san_a="$1"
1108 san_b="$2"
1109 port="$3"
1110 content="$4"
1111
1112 _debug san_a "$san_a"
1113 _debug san_b "$san_b"
1114 _debug port "$port"
1115
1116 #create key TLS_KEY
1117 if ! _createkey "2048" "$TLS_KEY" ; then
1118 _err "Create tls validation key error."
1119 return 1
1120 fi
1121
1122 #create csr
1123 alt="$san_a"
1124 if [ "$san_b" ] ; then
1125 alt="$alt,$san_b"
1126 fi
1127 if ! _createcsr "tls.acme.sh" "$alt" "$TLS_KEY" "$TLS_CSR" "$TLS_CONF" ; then
1128 _err "Create tls validation csr error."
1129 return 1
1130 fi
1131
1132 #self signed
1133 if ! _signcsr "$TLS_KEY" "$TLS_CSR" "$TLS_CONF" "$TLS_CERT" ; then
1134 _err "Create tls validation cert error."
1135 return 1
1136 fi
1137
1138 #start openssl
bdbf323f 1139 _debug "openssl s_server -cert \"$TLS_CERT\" -key \"$TLS_KEY\" -accept $port -naccept 1 -tlsextdebug"
fbad6a39 1140 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ] ; then
75da0713 1141 (printf "HTTP/1.1 200 OK\r\n\r\n$content" | openssl s_server -cert "$TLS_CERT" -key "$TLS_KEY" -accept $port -tlsextdebug ) &
331c4bb6 1142 else
75da0713 1143 (printf "HTTP/1.1 200 OK\r\n\r\n$content" | openssl s_server -cert "$TLS_CERT" -key "$TLS_KEY" -accept $port >/dev/null 2>&1) &
331c4bb6 1144 fi
1145
e22bcf7c 1146 serverproc="$!"
1147 sleep 2
1148 _debug serverproc $serverproc
1149}
1150
43822d37 1151#[domain] [keylength]
4c3b3608 1152_initpath() {
1153
8663fb7e 1154 if [ -z "$LE_WORKING_DIR" ] ; then
6cc11ffb 1155 LE_WORKING_DIR=$HOME/.$PROJECT_NAME
4c3b3608 1156 fi
1157
d53289d7 1158 _DEFAULT_ACCOUNT_CONF_PATH="$LE_WORKING_DIR/account.conf"
1159
8663fb7e 1160 if [ -z "$ACCOUNT_CONF_PATH" ] ; then
1161 if [ -f "$_DEFAULT_ACCOUNT_CONF_PATH" ] ; then
1162 . "$_DEFAULT_ACCOUNT_CONF_PATH"
635695ec 1163 fi
d53289d7 1164 fi
1165
8663fb7e 1166 if [ -z "$ACCOUNT_CONF_PATH" ] ; then
d53289d7 1167 ACCOUNT_CONF_PATH="$_DEFAULT_ACCOUNT_CONF_PATH"
4c3b3608 1168 fi
1169
8663fb7e 1170 if [ -f "$ACCOUNT_CONF_PATH" ] ; then
1171 . "$ACCOUNT_CONF_PATH"
4c3b3608 1172 fi
1173
8663fb7e 1174 if [ "$IN_CRON" ] ; then
1175 if [ ! "$_USER_PATH_EXPORTED" ] ; then
281aa349 1176 _USER_PATH_EXPORTED=1
1177 export PATH="$USER_PATH:$PATH"
1178 fi
1179 fi
1180
8663fb7e 1181 if [ -z "$API" ] ; then
1182 if [ -z "$STAGE" ] ; then
4c3b3608 1183 API="$DEFAULT_CA"
1184 else
1185 API="$STAGE_CA"
1186 _info "Using stage api:$API"
1187 fi
1188 fi
1189
8663fb7e 1190 if [ -z "$ACME_DIR" ] ; then
4c3b3608 1191 ACME_DIR="/home/.acme"
1192 fi
1193
8663fb7e 1194 if [ -z "$APACHE_CONF_BACKUP_DIR" ] ; then
8a144f4d 1195 APACHE_CONF_BACKUP_DIR="$LE_WORKING_DIR"
4c3b3608 1196 fi
1197
8663fb7e 1198 if [ -z "$USER_AGENT" ] ; then
bbbdcb09 1199 USER_AGENT="$DEFAULT_USER_AGENT"
1200 fi
1201
1202 HTTP_HEADER="$LE_WORKING_DIR/http.header"
b2817897 1203
1204 _DEFAULT_ACCOUNT_KEY_PATH="$LE_WORKING_DIR/account.key"
8663fb7e 1205 if [ -z "$ACCOUNT_KEY_PATH" ] ; then
b2817897 1206 ACCOUNT_KEY_PATH="$_DEFAULT_ACCOUNT_KEY_PATH"
4c3b3608 1207 fi
b2817897 1208
a79b26af
RD
1209 _DEFAULT_CERT_HOME="$LE_WORKING_DIR"
1210 if [ -z "$CERT_HOME" ] ; then
1211 CERT_HOME="$_DEFAULT_CERT_HOME"
1212 fi
1213
b2817897 1214 domain="$1"
43822d37 1215 length="$2"
8663fb7e 1216 if [ -z "$domain" ] ; then
4c3b3608 1217 return 0
1218 fi
1219
4c3b3608 1220
8663fb7e 1221 if [ -z "$DOMAIN_PATH" ] ; then
43822d37 1222 domainhome="$CERT_HOME/$domain"
1223 domainhomeecc="$CERT_HOME/$domain$ECC_SUFFIX"
1224
4c3b3608 1225 DOMAIN_PATH="$domainhome"
43822d37 1226
1227 if _isEccKey "$length" ; then
1228 DOMAIN_PATH="$domainhomeecc"
1229 else
1230 if [ ! -d "$domainhome" ] && [ -d "$domainhomeecc" ] ; then
1231 _info "The domain '$domain' seems to be a ECC domain, please add '$(__red "--ecc")' parameter next time."
1232 DOMAIN_PATH="$domainhomeecc"
1233 fi
1234 fi
1235 _debug DOMAIN_PATH "$DOMAIN_PATH"
4c3b3608 1236 fi
43822d37 1237
8663fb7e 1238 if [ -z "$DOMAIN_CONF" ] ; then
43822d37 1239 DOMAIN_CONF="$DOMAIN_PATH/$domain.conf"
4c3b3608 1240 fi
1241
8663fb7e 1242 if [ -z "$DOMAIN_SSL_CONF" ] ; then
43822d37 1243 DOMAIN_SSL_CONF="$DOMAIN_PATH/$domain.ssl.conf"
4c3b3608 1244 fi
1245
8663fb7e 1246 if [ -z "$CSR_PATH" ] ; then
43822d37 1247 CSR_PATH="$DOMAIN_PATH/$domain.csr"
4c3b3608 1248 fi
8663fb7e 1249 if [ -z "$CERT_KEY_PATH" ] ; then
43822d37 1250 CERT_KEY_PATH="$DOMAIN_PATH/$domain.key"
4c3b3608 1251 fi
8663fb7e 1252 if [ -z "$CERT_PATH" ] ; then
43822d37 1253 CERT_PATH="$DOMAIN_PATH/$domain.cer"
4c3b3608 1254 fi
8663fb7e 1255 if [ -z "$CA_CERT_PATH" ] ; then
43822d37 1256 CA_CERT_PATH="$DOMAIN_PATH/ca.cer"
4c3b3608 1257 fi
8663fb7e 1258 if [ -z "$CERT_FULLCHAIN_PATH" ] ; then
43822d37 1259 CERT_FULLCHAIN_PATH="$DOMAIN_PATH/fullchain.cer"
caf1fc10 1260 fi
8663fb7e 1261 if [ -z "$CERT_PFX_PATH" ] ; then
43822d37 1262 CERT_PFX_PATH="$DOMAIN_PATH/$domain.pfx"
ac2d5123 1263 fi
e22bcf7c 1264
1265 if [ -z "$TLS_CONF" ] ; then
43822d37 1266 TLS_CONF="$DOMAIN_PATH/tls.valdation.conf"
e22bcf7c 1267 fi
1268 if [ -z "$TLS_CERT" ] ; then
43822d37 1269 TLS_CERT="$DOMAIN_PATH/tls.valdation.cert"
e22bcf7c 1270 fi
1271 if [ -z "$TLS_KEY" ] ; then
43822d37 1272 TLS_KEY="$DOMAIN_PATH/tls.valdation.key"
e22bcf7c 1273 fi
1274 if [ -z "$TLS_CSR" ] ; then
43822d37 1275 TLS_CSR="$DOMAIN_PATH/tls.valdation.csr"
e22bcf7c 1276 fi
1277
4c3b3608 1278}
1279
1280
1281_apachePath() {
c3dd3ef0 1282 _APACHECTL="apachectl"
80a0a7b5 1283 if ! _exists apachectl ; then
e4a19585 1284 if _exists apache2ctl ; then
c3dd3ef0 1285 _APACHECTL="apache2ctl"
e4a19585 1286 else
bc96082f 1287 _err "'apachectl not found. It seems that apache is not installed, or you are not root user.'"
e4a19585 1288 _err "Please use webroot mode to try again."
1289 return 1
1290 fi
80a0a7b5 1291 fi
c3dd3ef0 1292 httpdconfname="$($_APACHECTL -V | grep SERVER_CONFIG_FILE= | cut -d = -f 2 | tr -d '"' )"
78768e98 1293 _debug httpdconfname "$httpdconfname"
dceb3aca 1294 if _startswith "$httpdconfname" '/' ; then
d62ee940 1295 httpdconf="$httpdconfname"
c456d954 1296 httpdconfname="$(basename $httpdconfname)"
d62ee940 1297 else
c3dd3ef0 1298 httpdroot="$($_APACHECTL -V | grep HTTPD_ROOT= | cut -d = -f 2 | tr -d '"' )"
78768e98 1299 _debug httpdroot "$httpdroot"
d62ee940 1300 httpdconf="$httpdroot/$httpdconfname"
8f63baf7 1301 httpdconfname="$(basename $httpdconfname)"
d62ee940 1302 fi
78768e98 1303 _debug httpdconf "$httpdconf"
8f63baf7 1304 _debug httpdconfname "$httpdconfname"
78768e98 1305 if [ ! -f "$httpdconf" ] ; then
1306 _err "Apache Config file not found" "$httpdconf"
4c3b3608 1307 return 1
1308 fi
1309 return 0
1310}
1311
1312_restoreApache() {
8663fb7e 1313 if [ -z "$usingApache" ] ; then
4c3b3608 1314 return 0
1315 fi
1316 _initpath
1317 if ! _apachePath ; then
1318 return 1
1319 fi
1320
8663fb7e 1321 if [ ! -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname" ] ; then
4c3b3608 1322 _debug "No config file to restore."
1323 return 0
1324 fi
1325
ff3bce32 1326 cat "$APACHE_CONF_BACKUP_DIR/$httpdconfname" > "$httpdconf"
5ef501c5 1327 _debug "Restored: $httpdconf."
c3dd3ef0 1328 if ! $_APACHECTL -t >/dev/null 2>&1 ; then
4c3b3608 1329 _err "Sorry, restore apache config error, please contact me."
1330 return 1;
1331 fi
5ef501c5 1332 _debug "Restored successfully."
4c3b3608 1333 rm -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname"
1334 return 0
1335}
1336
1337_setApache() {
1338 _initpath
1339 if ! _apachePath ; then
1340 return 1
1341 fi
1342
5fc5016d 1343 #test the conf first
869578ce 1344 _info "Checking if there is an error in the apache config file before starting."
c3dd3ef0 1345 _msg="$($_APACHECTL -t 2>&1 )"
5fc5016d 1346 if [ "$?" != "0" ] ; then
869578ce 1347 _err "Sorry, apache config file has error, please fix it first, then try again."
1348 _err "Don't worry, there is nothing changed to your system."
5fc5016d 1349 _err "$_msg"
1350 return 1;
1351 else
1352 _info "OK"
1353 fi
1354
4c3b3608 1355 #backup the conf
5778811a 1356 _debug "Backup apache config file" "$httpdconf"
8f63baf7 1357 if ! cp "$httpdconf" "$APACHE_CONF_BACKUP_DIR/" ; then
869578ce 1358 _err "Can not backup apache config file, so abort. Don't worry, the apache config is not changed."
8f63baf7 1359 _err "This might be a bug of $PROJECT_NAME , pleae report issue: $PROJECT"
1360 return 1
1361 fi
4c3b3608 1362 _info "JFYI, Config file $httpdconf is backuped to $APACHE_CONF_BACKUP_DIR/$httpdconfname"
1363 _info "In case there is an error that can not be restored automatically, you may try restore it yourself."
1364 _info "The backup file will be deleted on sucess, just forget it."
1365
1366 #add alias
b09d597c 1367
c3dd3ef0 1368 apacheVer="$($_APACHECTL -V | grep "Server version:" | cut -d : -f 2 | cut -d " " -f 2 | cut -d '/' -f 2 )"
b09d597c 1369 _debug "apacheVer" "$apacheVer"
1370 apacheMajer="$(echo "$apacheVer" | cut -d . -f 1)"
1371 apacheMinor="$(echo "$apacheVer" | cut -d . -f 2)"
1372
5778811a 1373 if [ "$apacheVer" ] && [ "$apacheMajer$apacheMinor" -ge "24" ] ; then
b09d597c 1374 echo "
4c3b3608 1375Alias /.well-known/acme-challenge $ACME_DIR
1376
1377<Directory $ACME_DIR >
1378Require all granted
b09d597c 1379</Directory>
5778811a 1380 " >> "$httpdconf"
b09d597c 1381 else
1382 echo "
1383Alias /.well-known/acme-challenge $ACME_DIR
1384
1385<Directory $ACME_DIR >
1386Order allow,deny
1387Allow from all
4c3b3608 1388</Directory>
5778811a 1389 " >> "$httpdconf"
b09d597c 1390 fi
1391
c3dd3ef0 1392 _msg="$($_APACHECTL -t 2>&1 )"
5fc5016d 1393 if [ "$?" != "0" ] ; then
1394 _err "Sorry, apache config error"
1395 if _restoreApache ; then
869578ce 1396 _err "The apache config file is restored."
5fc5016d 1397 else
869578ce 1398 _err "Sorry, The apache config file can not be restored, please report bug."
5fc5016d 1399 fi
4c3b3608 1400 return 1;
1401 fi
1402
8663fb7e 1403 if [ ! -d "$ACME_DIR" ] ; then
4c3b3608 1404 mkdir -p "$ACME_DIR"
1405 chmod 755 "$ACME_DIR"
1406 fi
1407
c3dd3ef0 1408 if ! $_APACHECTL graceful ; then
1409 _err "Sorry, $_APACHECTL graceful error, please contact me."
4c3b3608 1410 _restoreApache
1411 return 1;
1412 fi
1413 usingApache="1"
1414 return 0
1415}
1416
5ef501c5 1417_clearup() {
4c3b3608 1418 _stopserver $serverproc
1419 serverproc=""
1420 _restoreApache
e22bcf7c 1421 if [ -z "$DEBUG" ] ; then
1422 rm -f "$TLS_CONF"
1423 rm -f "$TLS_CERT"
1424 rm -f "$TLS_KEY"
1425 rm -f "$TLS_CSR"
1426 fi
4c3b3608 1427}
1428
1429# webroot removelevel tokenfile
1430_clearupwebbroot() {
1431 __webroot="$1"
8663fb7e 1432 if [ -z "$__webroot" ] ; then
4c3b3608 1433 _debug "no webroot specified, skip"
1434 return 0
1435 fi
1436
dcf9cb58 1437 _rmpath=""
8663fb7e 1438 if [ "$2" = '1' ] ; then
dcf9cb58 1439 _rmpath="$__webroot/.well-known"
8663fb7e 1440 elif [ "$2" = '2' ] ; then
dcf9cb58 1441 _rmpath="$__webroot/.well-known/acme-challenge"
8663fb7e 1442 elif [ "$2" = '3' ] ; then
dcf9cb58 1443 _rmpath="$__webroot/.well-known/acme-challenge/$3"
4c3b3608 1444 else
cc179731 1445 _debug "Skip for removelevel:$2"
4c3b3608 1446 fi
1447
dcf9cb58 1448 if [ "$_rmpath" ] ; then
1449 if [ "$DEBUG" ] ; then
1450 _debug "Debugging, skip removing: $_rmpath"
1451 else
1452 rm -rf "$_rmpath"
1453 fi
1454 fi
1455
4c3b3608 1456 return 0
1457
1458}
1459
1460issue() {
8663fb7e 1461 if [ -z "$2" ] ; then
43822d37 1462 _usage "Usage: $PROJECT_ENTRY --issue -d a.com -w /path/to/webroot/a.com/ "
4c3b3608 1463 return 1
1464 fi
1465 Le_Webroot="$1"
1466 Le_Domain="$2"
1467 Le_Alt="$3"
1468 Le_Keylength="$4"
1469 Le_RealCertPath="$5"
1470 Le_RealKeyPath="$6"
1471 Le_RealCACertPath="$7"
1472 Le_ReloadCmd="$8"
a63b05a9 1473 Le_RealFullChainPath="$9"
4c3b3608 1474
eccec5f6 1475 #remove these later.
8663fb7e 1476 if [ "$Le_Webroot" = "dns-cf" ] ; then
eccec5f6 1477 Le_Webroot="dns_cf"
1478 fi
8663fb7e 1479 if [ "$Le_Webroot" = "dns-dp" ] ; then
eccec5f6 1480 Le_Webroot="dns_dp"
1481 fi
8663fb7e 1482 if [ "$Le_Webroot" = "dns-cx" ] ; then
eccec5f6 1483 Le_Webroot="dns_cx"
1484 fi
4c3b3608 1485
43822d37 1486 if [ ! "$IS_RENEW" ] ; then
1487 _initpath $Le_Domain "$Le_Keylength"
1488 mkdir -p "$DOMAIN_PATH"
1489 fi
eccec5f6 1490
8663fb7e 1491 if [ -f "$DOMAIN_CONF" ] ; then
61623d22 1492 Le_NextRenewTime=$(_readdomainconf Le_NextRenewTime)
a4270efa 1493 _debug Le_NextRenewTime "$Le_NextRenewTime"
1494 if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ $(date -u "+%s" ) -lt $Le_NextRenewTime ] ; then
4c3b3608 1495 _info "Skip, Next renewal time is: $(grep "^Le_NextRenewTimeStr" "$DOMAIN_CONF" | cut -d '=' -f 2)"
cc179731 1496 return $RENEW_SKIP
4c3b3608 1497 fi
1498 fi
96a46cfc 1499
4d2f38b0 1500 _savedomainconf "Le_Domain" "$Le_Domain"
1501 _savedomainconf "Le_Alt" "$Le_Alt"
1502 _savedomainconf "Le_Webroot" "$Le_Webroot"
4c3b3608 1503
61623d22 1504
8663fb7e 1505 if [ "$Le_Alt" = "no" ] ; then
4c3b3608 1506 Le_Alt=""
1507 fi
61623d22 1508
c53da1ef 1509 if _hasfield "$Le_Webroot" "no" ; then
4c3b3608 1510 _info "Standalone mode."
5ef501c5 1511 if ! _exists "nc" ; then
4c3b3608 1512 _err "Please install netcat(nc) tools first."
1513 return 1
1514 fi
1515
8663fb7e 1516 if [ -z "$Le_HTTPPort" ] ; then
4c3b3608 1517 Le_HTTPPort=80
054cb72e 1518 else
1519 _savedomainconf "Le_HTTPPort" "$Le_HTTPPort"
1520 fi
4c3b3608 1521
251fc37c 1522 netprc="$(_ss "$Le_HTTPPort" | grep "$Le_HTTPPort")"
8663fb7e 1523 if [ "$netprc" ] ; then
4c3b3608 1524 _err "$netprc"
1525 _err "tcp port $Le_HTTPPort is already used by $(echo "$netprc" | cut -d : -f 4)"
1526 _err "Please stop it first"
1527 return 1
1528 fi
1529 fi
1530
e22bcf7c 1531 if _hasfield "$Le_Webroot" "$W_TLS" ; then
1532 _info "Standalone tls mode."
1533
1534 if [ -z "$Le_TLSPort" ] ; then
1535 Le_TLSPort=443
1536 else
1537 _savedomainconf "Le_TLSPort" "$Le_TLSPort"
1538 fi
1539
1540 netprc="$(_ss "$Le_TLSPort" | grep "$Le_TLSPort")"
1541 if [ "$netprc" ] ; then
1542 _err "$netprc"
1543 _err "tcp port $Le_TLSPort is already used by $(echo "$netprc" | cut -d : -f 4)"
1544 _err "Please stop it first"
1545 return 1
1546 fi
1547 fi
1548
c53da1ef 1549 if _hasfield "$Le_Webroot" "apache" ; then
4c3b3608 1550 if ! _setApache ; then
1551 _err "set up apache error. Report error to me."
1552 return 1
1553 fi
4c3b3608 1554 else
1555 usingApache=""
1556 fi
1557
8663fb7e 1558 if [ ! -f "$ACCOUNT_KEY_PATH" ] ; then
41e3eafa 1559 if ! createAccountKey $Le_Domain $Le_Keylength ; then
1560 _err "Create account key error."
8663fb7e 1561 if [ "$usingApache" ] ; then
5ef501c5 1562 _restoreApache
1563 fi
41e3eafa 1564 return 1
1565 fi
1566 fi
1567
166096dc 1568 if ! _calcjwk "$ACCOUNT_KEY_PATH" ; then
8663fb7e 1569 if [ "$usingApache" ] ; then
5ef501c5 1570 _restoreApache
1571 fi
166096dc 1572 return 1
1573 fi
1574
22ea4004 1575 accountkey_json=$(printf "%s" "$jwk" | tr -d ' ' )
1576 thumbprint=$(printf "%s" "$accountkey_json" | _digest "sha256" | _urlencode)
4c3b3608 1577
f574e581 1578 regjson='{"resource": "new-reg", "agreement": "'$AGREEMENT'"}'
1579 if [ "$ACCOUNT_EMAIL" ] ; then
1580 regjson='{"resource": "new-reg", "contact": ["mailto: '$ACCOUNT_EMAIL'"], "agreement": "'$AGREEMENT'"}'
1581 fi
1582
88fab7d6 1583 accountkeyhash="$(cat "$ACCOUNT_KEY_PATH" | _digest "sha256" )"
f574e581 1584 accountkeyhash="$(echo $accountkeyhash$API$regjson | _digest "sha256" )"
8663fb7e 1585 if [ "$accountkeyhash" != "$ACCOUNT_KEY_HASH" ] ; then
f574e581 1586 _info "Registering account"
1587 _send_signed_request "$API/acme/new-reg" "$regjson"
8663fb7e 1588 if [ "$code" = "" ] || [ "$code" = '201' ] ; then
166096dc 1589 _info "Registered"
f574e581 1590 echo "$response" > $LE_WORKING_DIR/account.json
8663fb7e 1591 elif [ "$code" = '409' ] ; then
166096dc 1592 _info "Already registered"
1593 else
1594 _err "Register account Error: $response"
1595 _clearup
1596 return 1
1597 fi
1598 ACCOUNT_KEY_HASH="$accountkeyhash"
1599 _saveaccountconf "ACCOUNT_KEY_HASH" "$ACCOUNT_KEY_HASH"
1600 else
1601 _info "Skip register account key"
1602 fi
1603
58f41a19 1604 if [ "$Le_Keylength" = "no" ] ; then
1605 Le_Keylength=""
1606 fi
1607
61623d22 1608 _key=$(_readdomainconf Le_Keylength)
1609 _debug "Read key length:$_key"
d8d10bc4 1610 if [ ! -f "$CERT_KEY_PATH" ] || [ "$Le_Keylength" != "$_key" ] ; then
41e3eafa 1611 if ! createDomainKey $Le_Domain $Le_Keylength ; then
1612 _err "Create domain key error."
5ef501c5 1613 _clearup
41e3eafa 1614 return 1
1615 fi
4c3b3608 1616 fi
1617
61623d22 1618 _savedomainconf "Le_Keylength" "$Le_Keylength"
58f41a19 1619
61623d22 1620
43822d37 1621 if ! _createcsr "$Le_Domain" "$Le_Alt" "$CERT_KEY_PATH" "$CSR_PATH" "$DOMAIN_SSL_CONF" ; then
4c3b3608 1622 _err "Create CSR error."
5ef501c5 1623 _clearup
4c3b3608 1624 return 1
1625 fi
a63b05a9 1626
4c3b3608 1627 vlist="$Le_Vlist"
1628 # verify each domain
1629 _info "Verify each domain"
1630 sep='#'
8663fb7e 1631 if [ -z "$vlist" ] ; then
4c3b3608 1632 alldomains=$(echo "$Le_Domain,$Le_Alt" | tr ',' ' ' )
a63b05a9 1633 _index=1
1634 _currentRoot=""
4c3b3608 1635 for d in $alldomains
a63b05a9 1636 do
1637 _info "Getting webroot for domain" $d
1638 _w="$(echo $Le_Webroot | cut -d , -f $_index)"
1639 _debug _w "$_w"
8663fb7e 1640 if [ "$_w" ] ; then
a63b05a9 1641 _currentRoot="$_w"
1642 fi
1643 _debug "_currentRoot" "$_currentRoot"
00a50605 1644 _index=$(_math $_index + 1)
a63b05a9 1645
1646 vtype="$VTYPE_HTTP"
dceb3aca 1647 if _startswith "$_currentRoot" "dns" ; then
a63b05a9 1648 vtype="$VTYPE_DNS"
1649 fi
e22bcf7c 1650
1651 if [ "$_currentRoot" = "$W_TLS" ] ; then
1652 vtype="$VTYPE_TLS"
1653 fi
1654
4c3b3608 1655 _info "Getting token for domain" $d
c4d8fd83 1656
1657 if ! _send_signed_request "$API/acme/new-authz" "{\"resource\": \"new-authz\", \"identifier\": {\"type\": \"dns\", \"value\": \"$d\"}}" ; then
1658 _err "Can not get domain token."
1659 _clearup
1660 return 1
1661 fi
1662
8663fb7e 1663 if [ ! -z "$code" ] && [ ! "$code" = '201' ] ; then
4c3b3608 1664 _err "new-authz error: $response"
1665 _clearup
1666 return 1
1667 fi
1668
22ea4004 1669 entry="$(printf "%s\n" "$response" | _egrep_o '[^{]*"type":"'$vtype'"[^}]*')"
4c3b3608 1670 _debug entry "$entry"
19539575 1671 if [ -z "$entry" ] ; then
1672 _err "Error, can not get domain token $d"
1673 _clearup
1674 return 1
1675 fi
22ea4004 1676 token="$(printf "%s\n" "$entry" | _egrep_o '"token":"[^"]*' | cut -d : -f 2 | tr -d '"')"
4c3b3608 1677 _debug token $token
1678
22ea4004 1679 uri="$(printf "%s\n" "$entry" | _egrep_o '"uri":"[^"]*'| cut -d : -f 2,3 | tr -d '"' )"
4c3b3608 1680 _debug uri $uri
1681
1682 keyauthorization="$token.$thumbprint"
1683 _debug keyauthorization "$keyauthorization"
1684
d35bf517 1685
1686 if printf "$response" | grep '"status":"valid"' >/dev/null 2>&1 ; then
1687 _info "$d is already verified, skip."
1688 keyauthorization=$STATE_VERIFIED
1689 _debug keyauthorization "$keyauthorization"
ec603bee 1690 fi
1691
d35bf517 1692
a63b05a9 1693 dvlist="$d$sep$keyauthorization$sep$uri$sep$vtype$sep$_currentRoot"
4c3b3608 1694 _debug dvlist "$dvlist"
1695
1696 vlist="$vlist$dvlist,"
1697
1698 done
1699
1700 #add entry
1701 dnsadded=""
1702 ventries=$(echo "$vlist" | tr ',' ' ' )
1703 for ventry in $ventries
1704 do
1705 d=$(echo $ventry | cut -d $sep -f 1)
1706 keyauthorization=$(echo $ventry | cut -d $sep -f 2)
a63b05a9 1707 vtype=$(echo $ventry | cut -d $sep -f 4)
1708 _currentRoot=$(echo $ventry | cut -d $sep -f 5)
ec603bee 1709
bd5e57d8 1710 if [ "$keyauthorization" = "$STATE_VERIFIED" ] ; then
ec603bee 1711 _info "$d is already verified, skip $vtype."
1712 continue
1713 fi
1714
8663fb7e 1715 if [ "$vtype" = "$VTYPE_DNS" ] ; then
4c3b3608 1716 dnsadded='0'
1717 txtdomain="_acme-challenge.$d"
1718 _debug txtdomain "$txtdomain"
22ea4004 1719 txt="$(printf "%s" "$keyauthorization" | _digest "sha256" | _urlencode)"
4c3b3608 1720 _debug txt "$txt"
1721 #dns
1722 #1. check use api
1723 d_api=""
8663fb7e 1724 if [ -f "$LE_WORKING_DIR/$d/$_currentRoot" ] ; then
a63b05a9 1725 d_api="$LE_WORKING_DIR/$d/$_currentRoot"
8663fb7e 1726 elif [ -f "$LE_WORKING_DIR/$d/$_currentRoot.sh" ] ; then
a63b05a9 1727 d_api="$LE_WORKING_DIR/$d/$_currentRoot.sh"
8663fb7e 1728 elif [ -f "$LE_WORKING_DIR/$_currentRoot" ] ; then
a63b05a9 1729 d_api="$LE_WORKING_DIR/$_currentRoot"
8663fb7e 1730 elif [ -f "$LE_WORKING_DIR/$_currentRoot.sh" ] ; then
a63b05a9 1731 d_api="$LE_WORKING_DIR/$_currentRoot.sh"
8663fb7e 1732 elif [ -f "$LE_WORKING_DIR/dnsapi/$_currentRoot" ] ; then
a63b05a9 1733 d_api="$LE_WORKING_DIR/dnsapi/$_currentRoot"
8663fb7e 1734 elif [ -f "$LE_WORKING_DIR/dnsapi/$_currentRoot.sh" ] ; then
a63b05a9 1735 d_api="$LE_WORKING_DIR/dnsapi/$_currentRoot.sh"
4c3b3608 1736 fi
1737 _debug d_api "$d_api"
1738
8663fb7e 1739 if [ "$d_api" ] ; then
4c3b3608 1740 _info "Found domain api file: $d_api"
1741 else
1742 _err "Add the following TXT record:"
1743 _err "Domain: $txtdomain"
1744 _err "TXT value: $txt"
1745 _err "Please be aware that you prepend _acme-challenge. before your domain"
1746 _err "so the resulting subdomain will be: $txtdomain"
1747 continue
1748 fi
4c3b3608 1749
73b8b120 1750 (
8663fb7e 1751 if ! . $d_api ; then
73b8b120 1752 _err "Load file $d_api error. Please check your api file and try again."
1753 return 1
1754 fi
1755
158f22f7 1756 addcommand="${_currentRoot}_add"
d53289d7 1757 if ! _exists $addcommand ; then
73b8b120 1758 _err "It seems that your api file is not correct, it must have a function named: $addcommand"
1759 return 1
1760 fi
1761
1762 if ! $addcommand $txtdomain $txt ; then
1763 _err "Error add txt for domain:$txtdomain"
1764 return 1
1765 fi
1766 )
4c3b3608 1767
8663fb7e 1768 if [ "$?" != "0" ] ; then
5ef501c5 1769 _clearup
4c3b3608 1770 return 1
1771 fi
1772 dnsadded='1'
1773 fi
1774 done
1775
8663fb7e 1776 if [ "$dnsadded" = '0' ] ; then
4d2f38b0 1777 _savedomainconf "Le_Vlist" "$vlist"
4c3b3608 1778 _debug "Dns record not added yet, so, save to $DOMAIN_CONF and exit."
1779 _err "Please add the TXT records to the domains, and retry again."
5ef501c5 1780 _clearup
4c3b3608 1781 return 1
1782 fi
1783
1784 fi
1785
8663fb7e 1786 if [ "$dnsadded" = '1' ] ; then
0e38c60d 1787 if [ -z "$Le_DNSSleep" ] ; then
4a4dacb5 1788 Le_DNSSleep=$DEFAULT_DNS_SLEEP
0e38c60d 1789 else
1790 _savedomainconf "Le_DNSSleep" "$Le_DNSSleep"
1791 fi
1792
1793 _info "Sleep $Le_DNSSleep seconds for the txt records to take effect"
1794 sleep $Le_DNSSleep
4c3b3608 1795 fi
1796
1797 _debug "ok, let's start to verify"
a63b05a9 1798
4c3b3608 1799 ventries=$(echo "$vlist" | tr ',' ' ' )
1800 for ventry in $ventries
1801 do
1802 d=$(echo $ventry | cut -d $sep -f 1)
1803 keyauthorization=$(echo $ventry | cut -d $sep -f 2)
1804 uri=$(echo $ventry | cut -d $sep -f 3)
a63b05a9 1805 vtype=$(echo $ventry | cut -d $sep -f 4)
1806 _currentRoot=$(echo $ventry | cut -d $sep -f 5)
ec603bee 1807
bd5e57d8 1808 if [ "$keyauthorization" = "$STATE_VERIFIED" ] ; then
ec603bee 1809 _info "$d is already verified, skip $vtype."
1810 continue
1811 fi
1812
4c3b3608 1813 _info "Verifying:$d"
1814 _debug "d" "$d"
1815 _debug "keyauthorization" "$keyauthorization"
1816 _debug "uri" "$uri"
1817 removelevel=""
e22bcf7c 1818 token="$(printf "%s" "$keyauthorization" | cut -d '.' -f 1)"
a63b05a9 1819
1820 _debug "_currentRoot" "$_currentRoot"
1821
1822
8663fb7e 1823 if [ "$vtype" = "$VTYPE_HTTP" ] ; then
1824 if [ "$_currentRoot" = "no" ] ; then
4c3b3608 1825 _info "Standalone mode server"
1826 _startserver "$keyauthorization" &
8663fb7e 1827 if [ "$?" != "0" ] ; then
5ef501c5 1828 _clearup
6fc1447f 1829 return 1
1830 fi
4c3b3608 1831 serverproc="$!"
1832 sleep 2
1833 _debug serverproc $serverproc
6fc1447f 1834
4c3b3608 1835 else
8663fb7e 1836 if [ "$_currentRoot" = "apache" ] ; then
6f930641 1837 wellknown_path="$ACME_DIR"
1838 else
a63b05a9 1839 wellknown_path="$_currentRoot/.well-known/acme-challenge"
8663fb7e 1840 if [ ! -d "$_currentRoot/.well-known" ] ; then
6f930641 1841 removelevel='1'
8663fb7e 1842 elif [ ! -d "$_currentRoot/.well-known/acme-challenge" ] ; then
6f930641 1843 removelevel='2'
1844 else
1845 removelevel='3'
1846 fi
4c3b3608 1847 fi
6f930641 1848
4c3b3608 1849 _debug wellknown_path "$wellknown_path"
6f930641 1850
4c3b3608 1851 _debug "writing token:$token to $wellknown_path/$token"
1852
1853 mkdir -p "$wellknown_path"
7939b419 1854 printf "%s" "$keyauthorization" > "$wellknown_path/$token"
8663fb7e 1855 if [ ! "$usingApache" ] ; then
32fdc196
TB
1856 if webroot_owner=$(_stat $_currentRoot) ; then
1857 _debug "Changing owner/group of .well-known to $webroot_owner"
1858 chown -R $webroot_owner "$_currentRoot/.well-known"
1859 else
1860 _debug "not chaning owner/group of webroot";
1861 fi
df886ffa 1862 fi
4c3b3608 1863
1864 fi
e22bcf7c 1865
1866 elif [ "$vtype" = "$VTYPE_TLS" ] ; then
1867 #create A
1868 #_hash_A="$(printf "%s" $token | _digest "sha256" "hex" )"
1869 #_debug2 _hash_A "$_hash_A"
1870 #_x="$(echo $_hash_A | cut -c 1-32)"
1871 #_debug2 _x "$_x"
1872 #_y="$(echo $_hash_A | cut -c 33-64)"
1873 #_debug2 _y "$_y"
1874 #_SAN_A="$_x.$_y.token.acme.invalid"
1875 #_debug2 _SAN_A "$_SAN_A"
1876
1877 #create B
1878 _hash_B="$(printf "%s" $keyauthorization | _digest "sha256" "hex" )"
1879 _debug2 _hash_B "$_hash_B"
1880 _x="$(echo $_hash_B | cut -c 1-32)"
1881 _debug2 _x "$_x"
1882 _y="$(echo $_hash_B | cut -c 33-64)"
1883 _debug2 _y "$_y"
1884
1885 #_SAN_B="$_x.$_y.ka.acme.invalid"
1886
1887 _SAN_B="$_x.$_y.acme.invalid"
1888 _debug2 _SAN_B "$_SAN_B"
1889
1890 if ! _starttlsserver "$_SAN_B" "$_SAN_A" "$Le_TLSPort" "$keyauthorization" ; then
1891 _err "Start tls server error."
1892 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
1893 _clearup
1894 return 1
1895 fi
4c3b3608 1896 fi
1897
c4d8fd83 1898 if ! _send_signed_request $uri "{\"resource\": \"challenge\", \"keyAuthorization\": \"$keyauthorization\"}" ; then
1899 _err "$d:Can not get challenge: $response"
1900 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
1901 _clearup
1902 return 1
1903 fi
4c3b3608 1904
8663fb7e 1905 if [ ! -z "$code" ] && [ ! "$code" = '202' ] ; then
c60883ef 1906 _err "$d:Challenge error: $response"
a63b05a9 1907 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4c3b3608 1908 _clearup
1909 return 1
1910 fi
1911
6fc1447f 1912 waittimes=0
8663fb7e 1913 if [ -z "$MAX_RETRY_TIMES" ] ; then
6fc1447f 1914 MAX_RETRY_TIMES=30
1915 fi
1916
2ee5d873 1917 while true ; do
00a50605 1918 waittimes=$(_math $waittimes + 1)
8663fb7e 1919 if [ "$waittimes" -ge "$MAX_RETRY_TIMES" ] ; then
6fc1447f 1920 _err "$d:Timeout"
1921 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
1922 _clearup
1923 return 1
1924 fi
1925
4c3b3608 1926 _debug "sleep 5 secs to verify"
1927 sleep 5
1928 _debug "checking"
9aaf36cd 1929 response="$(_get $uri)"
8663fb7e 1930 if [ "$?" != "0" ] ; then
c60883ef 1931 _err "$d:Verify error:$response"
a63b05a9 1932 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4c3b3608 1933 _clearup
1934 return 1
1935 fi
9aaf36cd 1936 _debug2 original "$response"
1937
1938 response="$(echo "$response" | _normalizeJson )"
7012b91f 1939 _debug2 response "$response"
4c3b3608 1940
22ea4004 1941 status=$(echo "$response" | _egrep_o '"status":"[^"]*' | cut -d : -f 2 | tr -d '"')
8663fb7e 1942 if [ "$status" = "valid" ] ; then
4c3b3608 1943 _info "Success"
1944 _stopserver $serverproc
1945 serverproc=""
a63b05a9 1946 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4c3b3608 1947 break;
1948 fi
1949
8663fb7e 1950 if [ "$status" = "invalid" ] ; then
22ea4004 1951 error="$(echo "$response" | _egrep_o '"error":\{[^}]*}')"
b7ec6789 1952 _debug2 error "$error"
22ea4004 1953 errordetail="$(echo $error | _egrep_o '"detail": *"[^"]*"' | cut -d '"' -f 4)"
b7ec6789 1954 _debug2 errordetail "$errordetail"
1955 if [ "$errordetail" ] ; then
1956 _err "$d:Verify error:$errordetail"
1957 else
1958 _err "$d:Verify error:$error"
1959 fi
dcf9cb58 1960 if [ "$DEBUG" ] ; then
1961 if [ "$vtype" = "$VTYPE_HTTP" ] ; then
1962 _get "http://$d/.well-known/acme-challenge/$token"
1963 fi
1964 fi
a63b05a9 1965 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4c3b3608 1966 _clearup
1967 return 1;
1968 fi
1969
8663fb7e 1970 if [ "$status" = "pending" ] ; then
4c3b3608 1971 _info "Pending"
1972 else
1973 _err "$d:Verify error:$response"
a63b05a9 1974 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4c3b3608 1975 _clearup
1976 return 1
1977 fi
1978
1979 done
1980
1981 done
1982
1983 _clearup
1984 _info "Verify finished, start to sign."
fa8311dc 1985 der="$(_getfile "${CSR_PATH}" "${BEGIN_CSR}" "${END_CSR}" | tr -d "\r\n" | _urlencode)"
c4d8fd83 1986
1987 if ! _send_signed_request "$API/acme/new-cert" "{\"resource\": \"new-cert\", \"csr\": \"$der\"}" "needbase64" ; then
1988 _err "Sign failed."
1989 return 1
1990 fi
4c3b3608 1991
1992
22ea4004 1993 Le_LinkCert="$(grep -i '^Location.*$' $HTTP_HEADER | head -1 | tr -d "\r\n" | cut -d " " -f 2)"
4d2f38b0 1994 _savedomainconf "Le_LinkCert" "$Le_LinkCert"
4c3b3608 1995
8663fb7e 1996 if [ "$Le_LinkCert" ] ; then
88fab7d6 1997 echo "$BEGIN_CERT" > "$CERT_PATH"
c60883ef 1998 _get "$Le_LinkCert" | _base64 "multiline" >> "$CERT_PATH"
88fab7d6 1999 echo "$END_CERT" >> "$CERT_PATH"
43822d37 2000 _info "$(__green "Cert success.")"
4c3b3608 2001 cat "$CERT_PATH"
2002
2003 _info "Your cert is in $CERT_PATH"
caf1fc10 2004 cp "$CERT_PATH" "$CERT_FULLCHAIN_PATH"
281aa349 2005
8663fb7e 2006 if [ ! "$USER_PATH" ] || [ ! "$IN_CRON" ] ; then
281aa349 2007 USER_PATH="$PATH"
2008 _saveaccountconf "USER_PATH" "$USER_PATH"
2009 fi
4c3b3608 2010 fi
2011
2012
8663fb7e 2013 if [ -z "$Le_LinkCert" ] ; then
eae29099 2014 response="$(echo $response | _dbase64 "multiline" | _normalizeJson )"
22ea4004 2015 _err "Sign failed: $(echo "$response" | _egrep_o '"detail":"[^"]*"')"
4c3b3608 2016 return 1
2017 fi
2018
4d2f38b0 2019 _cleardomainconf "Le_Vlist"
4c3b3608 2020
bbbdcb09 2021 Le_LinkIssuer=$(grep -i '^Link' $HTTP_HEADER | head -1 | cut -d " " -f 2| cut -d ';' -f 1 | tr -d '<>' )
fac1e367 2022 if ! _contains "$Le_LinkIssuer" ":" ; then
2023 Le_LinkIssuer="$API$Le_LinkIssuer"
2024 fi
2025
4d2f38b0 2026 _savedomainconf "Le_LinkIssuer" "$Le_LinkIssuer"
4c3b3608 2027
8663fb7e 2028 if [ "$Le_LinkIssuer" ] ; then
88fab7d6 2029 echo "$BEGIN_CERT" > "$CA_CERT_PATH"
c60883ef 2030 _get "$Le_LinkIssuer" | _base64 "multiline" >> "$CA_CERT_PATH"
88fab7d6 2031 echo "$END_CERT" >> "$CA_CERT_PATH"
4c3b3608 2032 _info "The intermediate CA cert is in $CA_CERT_PATH"
caf1fc10 2033 cat "$CA_CERT_PATH" >> "$CERT_FULLCHAIN_PATH"
2034 _info "And the full chain certs is there: $CERT_FULLCHAIN_PATH"
4c3b3608 2035 fi
2036
2037 Le_CertCreateTime=$(date -u "+%s")
4d2f38b0 2038 _savedomainconf "Le_CertCreateTime" "$Le_CertCreateTime"
4c3b3608 2039
2040 Le_CertCreateTimeStr=$(date -u )
4d2f38b0 2041 _savedomainconf "Le_CertCreateTimeStr" "$Le_CertCreateTimeStr"
4c3b3608 2042
523c7682 2043 if [ -z "$Le_RenewalDays" ] || [ "$Le_RenewalDays" -lt "0" ] || [ "$Le_RenewalDays" -gt "$MAX_RENEW" ] ; then
2044 Le_RenewalDays=$MAX_RENEW
054cb72e 2045 else
2046 _savedomainconf "Le_RenewalDays" "$Le_RenewalDays"
13d7cae9 2047 fi
2048
fac1e367 2049 if [ "$HTTPS_INSECURE" ] ; then
2050 _saveaccountconf HTTPS_INSECURE "$HTTPS_INSECURE"
2051 else
2052 _clearaccountconf "HTTPS_INSECURE"
13d7cae9 2053 fi
00a50605 2054
2055 Le_NextRenewTime=$(_math $Le_CertCreateTime + $Le_RenewalDays \* 24 \* 60 \* 60)
4d2f38b0 2056 _savedomainconf "Le_NextRenewTime" "$Le_NextRenewTime"
4c3b3608 2057
2058 Le_NextRenewTimeStr=$( _time2str $Le_NextRenewTime )
4d2f38b0 2059 _savedomainconf "Le_NextRenewTimeStr" "$Le_NextRenewTimeStr"
4c3b3608 2060
2061
4c0d3f1b 2062 if [ "$Le_RealCertPath$Le_RealKeyPath$Le_RealCACertPath$Le_ReloadCmd$Le_RealFullChainPath" ] ; then
43822d37 2063 _installcert
01f54558 2064 fi
4c0d3f1b 2065
4c3b3608 2066}
2067
43822d37 2068#domain [isEcc]
4c3b3608 2069renew() {
2070 Le_Domain="$1"
8663fb7e 2071 if [ -z "$Le_Domain" ] ; then
43822d37 2072 _usage "Usage: $PROJECT_ENTRY --renew -d domain.com [--ecc]"
4c3b3608 2073 return 1
2074 fi
2075
43822d37 2076 _isEcc="$2"
2077
2078 _initpath $Le_Domain "$_isEcc"
2079
2080 _info "Renew: '$Le_Domain'"
8663fb7e 2081 if [ ! -f "$DOMAIN_CONF" ] ; then
43822d37 2082 _info "'$Le_Domain' is not a issued domain, skip."
4c3b3608 2083 return 0;
2084 fi
2085
1e6b68f5 2086 if [ "$Le_RenewalDays" ] ; then
2087 _savedomainconf Le_RenewalDays "$Le_RenewalDays"
2088 fi
2089
8663fb7e 2090 . "$DOMAIN_CONF"
2091 if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(date -u "+%s" )" -lt "$Le_NextRenewTime" ] ; then
4c3b3608 2092 _info "Skip, Next renewal time is: $Le_NextRenewTimeStr"
cc179731 2093 return $RENEW_SKIP
4c3b3608 2094 fi
2095
2096 IS_RENEW="1"
a63b05a9 2097 issue "$Le_Webroot" "$Le_Domain" "$Le_Alt" "$Le_Keylength" "$Le_RealCertPath" "$Le_RealKeyPath" "$Le_RealCACertPath" "$Le_ReloadCmd" "$Le_RealFullChainPath"
22ea4004 2098 res=$?
4c3b3608 2099 IS_RENEW=""
2100
2101 return $res
2102}
2103
cc179731 2104#renewAll [stopRenewOnError]
4c3b3608 2105renewAll() {
2106 _initpath
cc179731 2107 _stopRenewOnError="$1"
2108 _debug "_stopRenewOnError" "$_stopRenewOnError"
2109 _ret="0"
43822d37 2110
b2817897 2111 for d in $(ls -F ${CERT_HOME}/ | grep [^.].*[.].*/$ ) ; do
4c3b3608 2112 d=$(echo $d | cut -d '/' -f 1)
43822d37 2113 (
2114 if _endswith $d "$ECC_SUFFIX" ; then
2115 _isEcc=$(echo $d | cut -d "$ECC_SEP" -f 2)
2116 d=$(echo $d | cut -d "$ECC_SEP" -f 1)
2117 fi
2118 renew "$d" "$_isEcc"
4d2f38b0 2119 )
cc179731 2120 rc="$?"
2121 _debug "Return code: $rc"
2122 if [ "$rc" != "0" ] ; then
2123 if [ "$rc" = "$RENEW_SKIP" ] ; then
2124 _info "Skipped $d"
2125 elif [ "$_stopRenewOnError" ] ; then
2126 _err "Error renew $d, stop now."
2127 return $rc
2128 else
2129 _ret="$rc"
2130 _err "Error renew $d, Go ahead to next one."
2131 fi
2132 fi
4c3b3608 2133 done
cc179731 2134 return $_ret
4c3b3608 2135}
2136
dcf4f8f6 2137
6d7eda3e 2138list() {
22ea4004 2139 _raw="$1"
6d7eda3e 2140 _initpath
dcf4f8f6 2141
2142 _sep="|"
2143 if [ "$_raw" ] ; then
43822d37 2144 printf "Main_Domain${_sep}KeyLength${_sep}SAN_Domains${_sep}Created${_sep}Renew\n"
dcf4f8f6 2145 for d in $(ls -F ${CERT_HOME}/ | grep [^.].*[.].*/$ ) ; do
2146 d=$(echo $d | cut -d '/' -f 1)
2147 (
43822d37 2148 if _endswith $d "$ECC_SUFFIX" ; then
2149 _isEcc=$(echo $d | cut -d "$ECC_SEP" -f 2)
2150 d=$(echo $d | cut -d "$ECC_SEP" -f 1)
2151 fi
2152 _initpath $d "$_isEcc"
dcf4f8f6 2153 if [ -f "$DOMAIN_CONF" ] ; then
2154 . "$DOMAIN_CONF"
43822d37 2155 printf "$Le_Domain${_sep}\"$Le_Keylength\"${_sep}$Le_Alt${_sep}$Le_CertCreateTimeStr${_sep}$Le_NextRenewTimeStr\n"
dcf4f8f6 2156 fi
2157 )
2158 done
2159 else
22ea4004 2160 if _exists column ; then
2161 list "raw" | column -t -s "$_sep"
2162 else
43822d37 2163 list "raw" | tr "$_sep" '\t'
22ea4004 2164 fi
dcf4f8f6 2165 fi
6d7eda3e 2166
2167
2168}
2169
4c3b3608 2170installcert() {
2171 Le_Domain="$1"
8663fb7e 2172 if [ -z "$Le_Domain" ] ; then
43822d37 2173 _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 2174 return 1
2175 fi
2176
2177 Le_RealCertPath="$2"
2178 Le_RealKeyPath="$3"
2179 Le_RealCACertPath="$4"
2180 Le_ReloadCmd="$5"
a63b05a9 2181 Le_RealFullChainPath="$6"
43822d37 2182 _isEcc="$7"
2183
2184 _initpath $Le_Domain "$_isEcc"
2185 if [ ! -d "$DOMAIN_PATH" ] ; then
2186 _err "Domain is not valid:'$Le_Domain'"
2187 return 1
2188 fi
2189
2190 _installcert
2191}
4c3b3608 2192
43822d37 2193
2194_installcert() {
4c3b3608 2195
4d2f38b0 2196 _savedomainconf "Le_RealCertPath" "$Le_RealCertPath"
2197 _savedomainconf "Le_RealCACertPath" "$Le_RealCACertPath"
2198 _savedomainconf "Le_RealKeyPath" "$Le_RealKeyPath"
2199 _savedomainconf "Le_ReloadCmd" "$Le_ReloadCmd"
2200 _savedomainconf "Le_RealFullChainPath" "$Le_RealFullChainPath"
4c3b3608 2201
4d2f38b0 2202 if [ "$Le_RealCertPath" = "no" ] ; then
2203 Le_RealCertPath=""
2204 fi
2205 if [ "$Le_RealKeyPath" = "no" ] ; then
2206 Le_RealKeyPath=""
2207 fi
2208 if [ "$Le_RealCACertPath" = "no" ] ; then
2209 Le_RealCACertPath=""
2210 fi
2211 if [ "$Le_ReloadCmd" = "no" ] ; then
2212 Le_ReloadCmd=""
2213 fi
2214 if [ "$Le_RealFullChainPath" = "no" ] ; then
2215 Le_RealFullChainPath=""
2216 fi
2217
2218 _installed="0"
8663fb7e 2219 if [ "$Le_RealCertPath" ] ; then
4d2f38b0 2220 _installed=1
2221 _info "Installing cert to:$Le_RealCertPath"
43822d37 2222 if [ -f "$Le_RealCertPath" ] && [ ! "$IS_RENEW" ] ; then
ff3bce32 2223 cp "$Le_RealCertPath" "$Le_RealCertPath".bak
4c3b3608 2224 fi
2225 cat "$CERT_PATH" > "$Le_RealCertPath"
2226 fi
2227
8663fb7e 2228 if [ "$Le_RealCACertPath" ] ; then
4d2f38b0 2229 _installed=1
2230 _info "Installing CA to:$Le_RealCACertPath"
8663fb7e 2231 if [ "$Le_RealCACertPath" = "$Le_RealCertPath" ] ; then
4c3b3608 2232 echo "" >> "$Le_RealCACertPath"
2233 cat "$CA_CERT_PATH" >> "$Le_RealCACertPath"
2234 else
43822d37 2235 if [ -f "$Le_RealCACertPath" ] && [ ! "$IS_RENEW" ] ; then
ff3bce32 2236 cp "$Le_RealCACertPath" "$Le_RealCACertPath".bak
78552b18 2237 fi
4c3b3608 2238 cat "$CA_CERT_PATH" > "$Le_RealCACertPath"
2239 fi
2240 fi
2241
2242
8663fb7e 2243 if [ "$Le_RealKeyPath" ] ; then
4d2f38b0 2244 _installed=1
2245 _info "Installing key to:$Le_RealKeyPath"
43822d37 2246 if [ -f "$Le_RealKeyPath" ] && [ ! "$IS_RENEW" ] ; then
ff3bce32 2247 cp "$Le_RealKeyPath" "$Le_RealKeyPath".bak
4c3b3608 2248 fi
2249 cat "$CERT_KEY_PATH" > "$Le_RealKeyPath"
2250 fi
a63b05a9 2251
8663fb7e 2252 if [ "$Le_RealFullChainPath" ] ; then
4d2f38b0 2253 _installed=1
2254 _info "Installing full chain to:$Le_RealFullChainPath"
43822d37 2255 if [ -f "$Le_RealFullChainPath" ] && [ ! "$IS_RENEW" ] ; then
ff3bce32 2256 cp "$Le_RealFullChainPath" "$Le_RealFullChainPath".bak
a63b05a9 2257 fi
2258 cat "$CERT_FULLCHAIN_PATH" > "$Le_RealFullChainPath"
2259 fi
4c3b3608 2260
8663fb7e 2261 if [ "$Le_ReloadCmd" ] ; then
4d2f38b0 2262 _installed=1
4c3b3608 2263 _info "Run Le_ReloadCmd: $Le_ReloadCmd"
4d2f38b0 2264 if (cd "$DOMAIN_PATH" && eval "$Le_ReloadCmd") ; then
43822d37 2265 _info "$(__green "Reload success")"
4d2f38b0 2266 else
2267 _err "Reload error for :$Le_Domain"
2268 fi
2269 fi
2270
4c3b3608 2271
2272}
2273
2274installcronjob() {
2275 _initpath
77546ea5 2276 if ! _exists "crontab" ; then
2277 _err "crontab doesn't exist, so, we can not install cron jobs."
2278 _err "All your certs will not be renewed automatically."
a7b7355d 2279 _err "You must add your own cron job to call '$PROJECT_ENTRY --cron' everyday."
77546ea5 2280 return 1
2281 fi
2282
4c3b3608 2283 _info "Installing cron job"
a7b7355d 2284 if ! crontab -l | grep "$PROJECT_ENTRY --cron" ; then
8663fb7e 2285 if [ -f "$LE_WORKING_DIR/$PROJECT_ENTRY" ] ; then
a7b7355d 2286 lesh="\"$LE_WORKING_DIR\"/$PROJECT_ENTRY"
4c3b3608 2287 else
a7b7355d 2288 _err "Can not install cronjob, $PROJECT_ENTRY not found."
4c3b3608 2289 return 1
2290 fi
22ea4004 2291 if _exists uname && uname -a | grep solaris >/dev/null ; then
2292 crontab -l | { cat; echo "0 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null"; } | crontab --
2293 else
2294 crontab -l | { cat; echo "0 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null"; } | crontab -
2295 fi
4c3b3608 2296 fi
8663fb7e 2297 if [ "$?" != "0" ] ; then
4c3b3608 2298 _err "Install cron job failed. You need to manually renew your certs."
2299 _err "Or you can add cronjob by yourself:"
a7b7355d 2300 _err "$lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null"
4c3b3608 2301 return 1
2302 fi
2303}
2304
2305uninstallcronjob() {
37db5b81 2306 if ! _exists "crontab" ; then
2307 return
2308 fi
4c3b3608 2309 _info "Removing cron job"
a7b7355d 2310 cr="$(crontab -l | grep "$PROJECT_ENTRY --cron")"
8663fb7e 2311 if [ "$cr" ] ; then
22ea4004 2312 if _exists uname && uname -a | grep solaris >/dev/null ; then
2313 crontab -l | sed "/$PROJECT_ENTRY --cron/d" | crontab --
2314 else
2315 crontab -l | sed "/$PROJECT_ENTRY --cron/d" | crontab -
2316 fi
a7b7355d 2317 LE_WORKING_DIR="$(echo "$cr" | cut -d ' ' -f 9 | tr -d '"')"
4c3b3608 2318 _info LE_WORKING_DIR "$LE_WORKING_DIR"
2319 fi
2320 _initpath
a7b7355d 2321
4c3b3608 2322}
2323
6cb415f5 2324revoke() {
2325 Le_Domain="$1"
8663fb7e 2326 if [ -z "$Le_Domain" ] ; then
43822d37 2327 _usage "Usage: $PROJECT_ENTRY --revoke -d domain.com"
6cb415f5 2328 return 1
2329 fi
2330
43822d37 2331 _isEcc="$2"
2332
2333 _initpath $Le_Domain "$_isEcc"
8663fb7e 2334 if [ ! -f "$DOMAIN_CONF" ] ; then
6cb415f5 2335 _err "$Le_Domain is not a issued domain, skip."
2336 return 1;
2337 fi
2338
8663fb7e 2339 if [ ! -f "$CERT_PATH" ] ; then
6cb415f5 2340 _err "Cert for $Le_Domain $CERT_PATH is not found, skip."
2341 return 1
2342 fi
2343
2344 cert="$(_getfile "${CERT_PATH}" "${BEGIN_CERT}" "${END_CERT}"| tr -d "\r\n" | _urlencode)"
2345
8663fb7e 2346 if [ -z "$cert" ] ; then
6cb415f5 2347 _err "Cert for $Le_Domain is empty found, skip."
2348 return 1
2349 fi
2350
2351 data="{\"resource\": \"revoke-cert\", \"certificate\": \"$cert\"}"
2352 uri="$API/acme/revoke-cert"
2353
2354 _info "Try domain key first."
2355 if _send_signed_request $uri "$data" "" "$CERT_KEY_PATH"; then
8663fb7e 2356 if [ -z "$response" ] ; then
6cb415f5 2357 _info "Revoke success."
2358 rm -f $CERT_PATH
2359 return 0
2360 else
2361 _err "Revoke error by domain key."
c9c31c04 2362 _err "$response"
6cb415f5 2363 fi
2364 fi
2365
2366 _info "Then try account key."
2367
2368 if _send_signed_request $uri "$data" "" "$ACCOUNT_KEY_PATH" ; then
8663fb7e 2369 if [ -z "$response" ] ; then
6cb415f5 2370 _info "Revoke success."
2371 rm -f $CERT_PATH
2372 return 0
2373 else
2374 _err "Revoke error."
c9c31c04 2375 _debug "$response"
6cb415f5 2376 fi
2377 fi
2378 return 1
2379}
4c3b3608 2380
2381# Detect profile file if not specified as environment variable
2382_detect_profile() {
a63b05a9 2383 if [ -n "$PROFILE" -a -f "$PROFILE" ] ; then
4c3b3608 2384 echo "$PROFILE"
2385 return
2386 fi
2387
4c3b3608 2388 DETECTED_PROFILE=''
4c3b3608 2389 SHELLTYPE="$(basename "/$SHELL")"
2390
8663fb7e 2391 if [ "$SHELLTYPE" = "bash" ] ; then
2392 if [ -f "$HOME/.bashrc" ] ; then
4c3b3608 2393 DETECTED_PROFILE="$HOME/.bashrc"
8663fb7e 2394 elif [ -f "$HOME/.bash_profile" ] ; then
4c3b3608 2395 DETECTED_PROFILE="$HOME/.bash_profile"
2396 fi
8663fb7e 2397 elif [ "$SHELLTYPE" = "zsh" ] ; then
4c3b3608 2398 DETECTED_PROFILE="$HOME/.zshrc"
2399 fi
2400
8663fb7e 2401 if [ -z "$DETECTED_PROFILE" ] ; then
2402 if [ -f "$HOME/.profile" ] ; then
4c3b3608 2403 DETECTED_PROFILE="$HOME/.profile"
8663fb7e 2404 elif [ -f "$HOME/.bashrc" ] ; then
4c3b3608 2405 DETECTED_PROFILE="$HOME/.bashrc"
8663fb7e 2406 elif [ -f "$HOME/.bash_profile" ] ; then
4c3b3608 2407 DETECTED_PROFILE="$HOME/.bash_profile"
8663fb7e 2408 elif [ -f "$HOME/.zshrc" ] ; then
4c3b3608 2409 DETECTED_PROFILE="$HOME/.zshrc"
2410 fi
2411 fi
2412
8663fb7e 2413 if [ ! -z "$DETECTED_PROFILE" ] ; then
4c3b3608 2414 echo "$DETECTED_PROFILE"
2415 fi
2416}
2417
2418_initconf() {
2419 _initpath
8663fb7e 2420 if [ ! -f "$ACCOUNT_CONF_PATH" ] ; then
d53289d7 2421 echo "#ACCOUNT_CONF_PATH=xxxx
2422
2423#Account configurations:
4c3b3608 2424#Here are the supported macros, uncomment them to make them take effect.
d53289d7 2425
4c3b3608 2426#ACCOUNT_EMAIL=aaa@aaa.com # the account email used to register account.
5fd3f21b 2427#ACCOUNT_KEY_PATH=\"/path/to/account.key\"
b2817897 2428#CERT_HOME=\"/path/to/cert/home\"
4c3b3608 2429
2430#STAGE=1 # Use the staging api
2431#FORCE=1 # Force to issue cert
2432#DEBUG=1 # Debug mode
2433
166096dc 2434#ACCOUNT_KEY_HASH=account key hash
2435
8814a348 2436#USER_AGENT=\"$USER_AGENT\"
281aa349 2437
2438#USER_PATH=""
2439
4c3b3608 2440#dns api
2441#######################
2442#Cloudflare:
2443#api key
3d49985a 2444#CF_Key=\"sdfsdfsdfljlbjkljlkjsdfoiwje\"
4c3b3608 2445#account email
3d49985a 2446#CF_Email=\"xxxx@sss.com\"
4c3b3608 2447
2448#######################
2449#Dnspod.cn:
2450#api key id
3d49985a 2451#DP_Id=\"1234\"
4c3b3608 2452#api key
3d49985a 2453#DP_Key=\"sADDsdasdgdsf\"
4c3b3608 2454
2455#######################
2456#Cloudxns.com:
3d49985a 2457#CX_Key=\"1234\"
4c3b3608 2458#
3d49985a 2459#CX_Secret=\"sADDsdasdgdsf\"
30de13b4 2460
2461#######################
2462#Godaddy.com:
2463#GD_Key=\"sdfdsgdgdfdasfds\"
2464#
2465#GD_Secret=\"sADDsdasdfsdfdssdgdsf\"
4c3b3608 2466
2467 " > $ACCOUNT_CONF_PATH
2468 fi
2469}
2470
c8e9a31e 2471# nocron
c60883ef 2472_precheck() {
c8e9a31e 2473 _nocron="$1"
2474
c60883ef 2475 if ! _exists "curl" && ! _exists "wget"; then
2476 _err "Please install curl or wget first, we need to access http resources."
4c3b3608 2477 return 1
2478 fi
2479
c8e9a31e 2480 if [ -z "$_nocron" ] ; then
2481 if ! _exists "crontab" ; then
2482 _err "It is recommended to install crontab first. try to install 'cron, crontab, crontabs or vixie-cron'."
2483 _err "We need to set cron job to renew the certs automatically."
2484 _err "Otherwise, your certs will not be able to be renewed automatically."
2485 if [ -z "$FORCE" ] ; then
2486 _err "Please add '--force' and try install again to go without crontab."
2487 _err "./$PROJECT_ENTRY --install --force"
2488 return 1
2489 fi
77546ea5 2490 fi
4c3b3608 2491 fi
2492
c60883ef 2493 if ! _exists "openssl" ; then
2494 _err "Please install openssl first."
2495 _err "We need openssl to generate keys."
4c3b3608 2496 return 1
2497 fi
2498
c60883ef 2499 if ! _exists "nc" ; then
2500 _err "It is recommended to install nc first, try to install 'nc' or 'netcat'."
2501 _err "We use nc for standalone server if you use standalone mode."
2502 _err "If you don't use standalone mode, just ignore this warning."
2503 fi
2504
2505 return 0
2506}
2507
0a7c9364 2508_setShebang() {
2509 _file="$1"
2510 _shebang="$2"
2511 if [ -z "$_shebang" ] ; then
43822d37 2512 _usage "Usage: file shebang"
0a7c9364 2513 return 1
2514 fi
2515 cp "$_file" "$_file.tmp"
2516 echo "$_shebang" > "$_file"
2517 sed -n 2,99999p "$_file.tmp" >> "$_file"
2518 rm -f "$_file.tmp"
2519}
2520
94dc5f33 2521_installalias() {
2522 _initpath
2523
2524 _envfile="$LE_WORKING_DIR/$PROJECT_ENTRY.env"
2525 if [ "$_upgrading" ] && [ "$_upgrading" = "1" ] ; then
2526 echo "$(cat $_envfile)" | sed "s|^LE_WORKING_DIR.*$||" > "$_envfile"
2527 echo "$(cat $_envfile)" | sed "s|^alias le.*$||" > "$_envfile"
2528 echo "$(cat $_envfile)" | sed "s|^alias le.sh.*$||" > "$_envfile"
2529 fi
2530
1786a5e5 2531 _setopt "$_envfile" "export LE_WORKING_DIR" "=" "\"$LE_WORKING_DIR\""
94dc5f33 2532 _setopt "$_envfile" "alias $PROJECT_ENTRY" "=" "\"$LE_WORKING_DIR/$PROJECT_ENTRY\""
2533
2534 _profile="$(_detect_profile)"
2535 if [ "$_profile" ] ; then
2536 _debug "Found profile: $_profile"
2537 _setopt "$_profile" ". \"$_envfile\""
2538 _info "OK, Close and reopen your terminal to start using $PROJECT_NAME"
2539 else
2540 _info "No profile is found, you will need to go into $LE_WORKING_DIR to use $PROJECT_NAME"
2541 fi
2542
2543
2544 #for csh
2545 _cshfile="$LE_WORKING_DIR/$PROJECT_ENTRY.csh"
94dc5f33 2546 _csh_profile="$HOME/.cshrc"
2547 if [ -f "$_csh_profile" ] ; then
6626371d 2548 _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\""
2549 _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY\""
94dc5f33 2550 _setopt "$_csh_profile" "source \"$_cshfile\""
2551 fi
acafa585 2552
2553 #for tcsh
2554 _tcsh_profile="$HOME/.tcshrc"
2555 if [ -f "$_tcsh_profile" ] ; then
2556 _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\""
2557 _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY\""
2558 _setopt "$_tcsh_profile" "source \"$_cshfile\""
2559 fi
94dc5f33 2560
2561}
2562
c8e9a31e 2563# nocron
c60883ef 2564install() {
c8e9a31e 2565 _nocron="$1"
c60883ef 2566 if ! _initpath ; then
2567 _err "Install failed."
4c3b3608 2568 return 1
2569 fi
52677b0a 2570 if [ "$_nocron" ] ; then
2571 _debug "Skip install cron job"
2572 fi
2573
c8e9a31e 2574 if ! _precheck "$_nocron" ; then
c60883ef 2575 _err "Pre-check failed, can not install."
4c3b3608 2576 return 1
2577 fi
c60883ef 2578
6cc11ffb 2579 #convert from le
8663fb7e 2580 if [ -d "$HOME/.le" ] ; then
6cc11ffb 2581 for envfile in "le.env" "le.sh.env"
2582 do
8663fb7e 2583 if [ -f "$HOME/.le/$envfile" ] ; then
6cc11ffb 2584 if grep "le.sh" "$HOME/.le/$envfile" >/dev/null ; then
2585 _upgrading="1"
2586 _info "You are upgrading from le.sh"
2587 _info "Renaming \"$HOME/.le\" to $LE_WORKING_DIR"
2588 mv "$HOME/.le" "$LE_WORKING_DIR"
2589 mv "$LE_WORKING_DIR/$envfile" "$LE_WORKING_DIR/$PROJECT_ENTRY.env"
2590 break;
2591 fi
2592 fi
2593 done
2594 fi
2595
4c3b3608 2596 _info "Installing to $LE_WORKING_DIR"
635695ec 2597
4a0f23e2 2598 if ! mkdir -p "$LE_WORKING_DIR" ; then
90035252 2599 _err "Can not create working dir: $LE_WORKING_DIR"
4a0f23e2 2600 return 1
2601 fi
2602
762978f8 2603 chmod 700 "$LE_WORKING_DIR"
2604
a7b7355d 2605 cp $PROJECT_ENTRY "$LE_WORKING_DIR/" && chmod +x "$LE_WORKING_DIR/$PROJECT_ENTRY"
4c3b3608 2606
8663fb7e 2607 if [ "$?" != "0" ] ; then
a7b7355d 2608 _err "Install failed, can not copy $PROJECT_ENTRY"
4c3b3608 2609 return 1
2610 fi
2611
a7b7355d 2612 _info "Installed to $LE_WORKING_DIR/$PROJECT_ENTRY"
4c3b3608 2613
94dc5f33 2614 _installalias
4c3b3608 2615
8663fb7e 2616 if [ -d "dnsapi" ] ; then
6ed1c718 2617 mkdir -p $LE_WORKING_DIR/dnsapi
2618 cp dnsapi/* $LE_WORKING_DIR/dnsapi/
2619 fi
d53289d7 2620
8663fb7e 2621 if [ ! -f "$ACCOUNT_CONF_PATH" ] ; then
4c3b3608 2622 _initconf
2623 fi
6cc11ffb 2624
8663fb7e 2625 if [ "$_DEFAULT_ACCOUNT_CONF_PATH" != "$ACCOUNT_CONF_PATH" ] ; then
635695ec 2626 _setopt "$_DEFAULT_ACCOUNT_CONF_PATH" "ACCOUNT_CONF_PATH" "=" "\"$ACCOUNT_CONF_PATH\""
6cc11ffb 2627 fi
2628
8663fb7e 2629 if [ "$_DEFAULT_CERT_HOME" != "$CERT_HOME" ] ; then
b2817897 2630 _saveaccountconf "CERT_HOME" "$CERT_HOME"
2631 fi
2632
8663fb7e 2633 if [ "$_DEFAULT_ACCOUNT_KEY_PATH" != "$ACCOUNT_KEY_PATH" ] ; then
b2817897 2634 _saveaccountconf "ACCOUNT_KEY_PATH" "$ACCOUNT_KEY_PATH"
2635 fi
2636
c8e9a31e 2637 if [ -z "$_nocron" ] ; then
2638 installcronjob
2639 fi
0a7c9364 2640
641989fd 2641 if [ -z "$NO_DETECT_SH" ] ; then
2642 #Modify shebang
2643 if _exists bash ; then
2644 _info "Good, bash is installed, change the shebang to use bash as prefered."
2645 _shebang='#!/usr/bin/env bash'
2646 _setShebang "$LE_WORKING_DIR/$PROJECT_ENTRY" "$_shebang"
2647 if [ -d "$LE_WORKING_DIR/dnsapi" ] ; then
2648 for _apifile in $(ls "$LE_WORKING_DIR/dnsapi/"*.sh) ; do
2649 _setShebang "$_apifile" "$_shebang"
2650 done
2651 fi
0a7c9364 2652 fi
2653 fi
2654
4c3b3608 2655 _info OK
2656}
2657
52677b0a 2658# nocron
4c3b3608 2659uninstall() {
52677b0a 2660 _nocron="$1"
2661 if [ -z "$_nocron" ] ; then
2662 uninstallcronjob
2663 fi
4c3b3608 2664 _initpath
2665
2666 _profile="$(_detect_profile)"
8663fb7e 2667 if [ "$_profile" ] ; then
7203a1c1 2668 text="$(cat $_profile)"
94dc5f33 2669 echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.env\"$||" > "$_profile"
4c3b3608 2670 fi
2671
94dc5f33 2672 _csh_profile="$HOME/.cshrc"
2673 if [ -f "$_csh_profile" ] ; then
2674 text="$(cat $_csh_profile)"
2675 echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" > "$_csh_profile"
2676 fi
2677
acafa585 2678 _tcsh_profile="$HOME/.tcshrc"
2679 if [ -f "$_tcsh_profile" ] ; then
2680 text="$(cat $_tcsh_profile)"
2681 echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" > "$_tcsh_profile"
2682 fi
2683
a7b7355d 2684 rm -f $LE_WORKING_DIR/$PROJECT_ENTRY
4c3b3608 2685 _info "The keys and certs are in $LE_WORKING_DIR, you can remove them by yourself."
2686
2687}
2688
2689cron() {
281aa349 2690 IN_CRON=1
4c3b3608 2691 renewAll
cc179731 2692 _ret="$?"
281aa349 2693 IN_CRON=""
cc179731 2694 return $_ret
4c3b3608 2695}
2696
2697version() {
a63b05a9 2698 echo "$PROJECT"
2699 echo "v$VER"
4c3b3608 2700}
2701
2702showhelp() {
2703 version
a7b7355d 2704 echo "Usage: $PROJECT_ENTRY command ...[parameters]....
a63b05a9 2705Commands:
2706 --help, -h Show this help message.
2707 --version, -v Show version info.
a7b7355d 2708 --install Install $PROJECT_NAME to your system.
2709 --uninstall Uninstall $PROJECT_NAME, and uninstall the cron job.
52677b0a 2710 --upgrade Upgrade $PROJECT_NAME to the latest code from $PROJECT
a63b05a9 2711 --issue Issue a cert.
2712 --installcert Install the issued cert to apache/nginx or any other server.
2713 --renew, -r Renew a cert.
2714 --renewAll Renew all the certs
2715 --revoke Revoke a cert.
6d7eda3e 2716 --list List all the certs
a63b05a9 2717 --installcronjob Install the cron job to renew certs, you don't need to call this. The 'install' command can automatically install the cron job.
2718 --uninstallcronjob Uninstall the cron job. The 'uninstall' command can do this automatically.
2719 --cron Run cron job to renew all the certs.
2720 --toPkcs Export the certificate and key to a pfx file.
2721 --createAccountKey, -cak Create an account private key, professional use.
2722 --createDomainKey, -cdk Create an domain private key, professional use.
2723 --createCSR, -ccsr Create CSR , professional use.
2724
2725Parameters:
2726 --domain, -d domain.tld Specifies a domain, used to issue, renew or revoke etc.
2727 --force, -f Used to force to install or force to renew a cert immediately.
2728 --staging, --test Use staging server, just for test.
2729 --debug Output debug info.
2730
2731 --webroot, -w /path/to/webroot Specifies the web root folder for web root mode.
2732 --standalone Use standalone mode.
e22bcf7c 2733 --tls Use standalone tls mode.
a63b05a9 2734 --apache Use apache mode.
eccec5f6 2735 --dns [dns_cf|dns_dp|dns_cx|/path/to/api/file] Use dns mode or dns api.
4a4dacb5 2736 --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 2737
2738 --keylength, -k [2048] Specifies the domain key length: 2048, 3072, 4096, 8192 or ec-256, ec-384.
2739 --accountkeylength, -ak [2048] Specifies the account key length.
2740
2741 These parameters are to install the cert to nginx/apache or anyother server after issue/renew a cert:
2742
2743 --certpath /path/to/real/cert/file After issue/renew, the cert will be copied to this path.
2744 --keypath /path/to/real/key/file After issue/renew, the key will be copied to this path.
2745 --capath /path/to/real/ca/file After issue/renew, the intermediate cert will be copied to this path.
2746 --fullchainpath /path/to/fullchain/file After issue/renew, the fullchain cert will be copied to this path.
2747
2748 --reloadcmd \"service nginx reload\" After issue/renew, it's used to reload the server.
2749
2750 --accountconf Specifies a customized account config file.
635695ec 2751 --home Specifies the home dir for $PROJECT_NAME .
39c8f79f 2752 --certhome Specifies the home dir to save all the certs, only valid for '--install' command.
635695ec 2753 --useragent Specifies the user agent string. it will be saved for future use too.
b5eb4b90 2754 --accountemail Specifies the account email for registering, Only valid for the '--install' command.
06625071 2755 --accountkey Specifies the account key path, Only valid for the '--install' command.
523c7682 2756 --days Specifies the days to renew the cert when using '--issue' command. The max value is $MAX_RENEW days.
39c8f79f 2757 --httpport Specifies the standalone listening port. Only valid if the server is behind a reverse proxy or load balancer.
e22bcf7c 2758 --tlsport Specifies the standalone tls listening port. Only valid if the server is behind a reverse proxy or load balancer.
dcf4f8f6 2759 --listraw Only used for '--list' command, list the certs in raw format.
c8e9a31e 2760 --stopRenewOnError, -se Only valid for '--renewall' command. Stop if one cert has error in renewal.
13d7cae9 2761 --insecure Do not check the server certificate, in some devices, the api server's certificate may not be trusted.
bc96082f 2762 --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 2763 --ecc Specifies to use the ECC cert. Valid for '--installcert', '--renew', '--revoke', '--toPkcs' and '--createCSR'
4c3b3608 2764 "
2765}
2766
52677b0a 2767# nocron
4a0f23e2 2768_installOnline() {
2769 _info "Installing from online archive."
52677b0a 2770 _nocron="$1"
8663fb7e 2771 if [ ! "$BRANCH" ] ; then
4a0f23e2 2772 BRANCH="master"
2773 fi
2774 _initpath
2775 target="$PROJECT/archive/$BRANCH.tar.gz"
2776 _info "Downloading $target"
2777 localname="$BRANCH.tar.gz"
2778 if ! _get "$target" > $localname ; then
2779 _debug "Download error."
2780 return 1
2781 fi
0bbe6eef 2782 (
4a0f23e2 2783 _info "Extracting $localname"
2784 tar xzf $localname
0bbe6eef 2785
6cc11ffb 2786 cd "$PROJECT_NAME-$BRANCH"
a7b7355d 2787 chmod +x $PROJECT_ENTRY
52677b0a 2788 if ./$PROJECT_ENTRY install "$_nocron" ; then
4a0f23e2 2789 _info "Install success!"
2790 fi
2791
2792 cd ..
0bbe6eef 2793
6cc11ffb 2794 rm -rf "$PROJECT_NAME-$BRANCH"
4a0f23e2 2795 rm -f "$localname"
0bbe6eef 2796 )
4a0f23e2 2797}
2798
52677b0a 2799upgrade() {
2800 if (
d0b748a4 2801 cd "$LE_WORKING_DIR"
52677b0a 2802 _installOnline "nocron"
2803 ) ; then
2804 _info "Upgrade success!"
096d8992 2805 exit 0
52677b0a 2806 else
2807 _err "Upgrade failed!"
096d8992 2808 exit 1
52677b0a 2809 fi
2810}
a63b05a9 2811
2812_process() {
2813 _CMD=""
2814 _domain=""
2815 _altdomains="no"
2816 _webroot=""
bdbf323f 2817 _keylength=""
2818 _accountkeylength=""
2819 _certpath=""
2820 _keypath=""
2821 _capath=""
2822 _fullchainpath=""
4d2f38b0 2823 _reloadcmd=""
a63b05a9 2824 _password=""
635695ec 2825 _accountconf=""
2826 _useragent=""
b5eb4b90 2827 _accountemail=""
2828 _accountkey=""
b2817897 2829 _certhome=""
39c8f79f 2830 _httpport=""
e22bcf7c 2831 _tlsport=""
0e38c60d 2832 _dnssleep=""
dcf4f8f6 2833 _listraw=""
cc179731 2834 _stopRenewOnError=""
13d7cae9 2835 _insecure=""
c8e9a31e 2836 _nocron=""
43822d37 2837 _ecc=""
8663fb7e 2838 while [ ${#} -gt 0 ] ; do
a63b05a9 2839 case "${1}" in
2840
2841 --help|-h)
2842 showhelp
2843 return
2844 ;;
2845 --version|-v)
2846 version
2847 return
2848 ;;
2849 --install)
2850 _CMD="install"
2851 ;;
2852 --uninstall)
2853 _CMD="uninstall"
2854 ;;
52677b0a 2855 --upgrade)
2856 _CMD="upgrade"
2857 ;;
a63b05a9 2858 --issue)
2859 _CMD="issue"
2860 ;;
2861 --installcert|-i)
2862 _CMD="installcert"
2863 ;;
2864 --renew|-r)
2865 _CMD="renew"
2866 ;;
4d2f38b0 2867 --renewAll|--renewall)
a63b05a9 2868 _CMD="renewAll"
2869 ;;
2870 --revoke)
2871 _CMD="revoke"
2872 ;;
6d7eda3e 2873 --list)
2874 _CMD="list"
2875 ;;
a63b05a9 2876 --installcronjob)
2877 _CMD="installcronjob"
2878 ;;
2879 --uninstallcronjob)
2880 _CMD="uninstallcronjob"
2881 ;;
2882 --cron)
2883 _CMD="cron"
2884 ;;
2885 --toPkcs)
2886 _CMD="toPkcs"
2887 ;;
2888 --createAccountKey|--createaccountkey|-cak)
2889 _CMD="createAccountKey"
2890 ;;
2891 --createDomainKey|--createdomainkey|-cdk)
2892 _CMD="createDomainKey"
2893 ;;
2894 --createCSR|--createcsr|-ccr)
2895 _CMD="createCSR"
2896 ;;
2897
2898
2899 --domain|-d)
2900 _dvalue="$2"
2901
ee1737a5 2902 if [ "$_dvalue" ] ; then
2903 if _startswith "$_dvalue" "-" ; then
2904 _err "'$_dvalue' is not a valid domain for parameter '$1'"
2905 return 1
2906 fi
2907
2908 if [ -z "$_domain" ] ; then
2909 _domain="$_dvalue"
a63b05a9 2910 else
ee1737a5 2911 if [ "$_altdomains" = "no" ] ; then
2912 _altdomains="$_dvalue"
2913 else
2914 _altdomains="$_altdomains,$_dvalue"
2915 fi
a63b05a9 2916 fi
2917 fi
ee1737a5 2918
a63b05a9 2919 shift
2920 ;;
2921
2922 --force|-f)
2923 FORCE="1"
2924 ;;
2925 --staging|--test)
2926 STAGE="1"
2927 ;;
2928 --debug)
8663fb7e 2929 if [ -z "$2" ] || _startswith "$2" "-" ; then
a63b05a9 2930 DEBUG="1"
2931 else
2932 DEBUG="$2"
2933 shift
6fc1447f 2934 fi
a63b05a9 2935 ;;
a63b05a9 2936 --webroot|-w)
2937 wvalue="$2"
8663fb7e 2938 if [ -z "$_webroot" ] ; then
a63b05a9 2939 _webroot="$wvalue"
2940 else
2941 _webroot="$_webroot,$wvalue"
2942 fi
2943 shift
2944 ;;
2945 --standalone)
2946 wvalue="no"
8663fb7e 2947 if [ -z "$_webroot" ] ; then
a63b05a9 2948 _webroot="$wvalue"
2949 else
2950 _webroot="$_webroot,$wvalue"
2951 fi
2952 ;;
2953 --apache)
2954 wvalue="apache"
8663fb7e 2955 if [ -z "$_webroot" ] ; then
a63b05a9 2956 _webroot="$wvalue"
2957 else
2958 _webroot="$_webroot,$wvalue"
2959 fi
2960 ;;
e22bcf7c 2961 --tls)
2962 wvalue="$W_TLS"
2963 if [ -z "$_webroot" ] ; then
2964 _webroot="$wvalue"
2965 else
2966 _webroot="$_webroot,$wvalue"
2967 fi
2968 ;;
a63b05a9 2969 --dns)
2970 wvalue="dns"
dceb3aca 2971 if ! _startswith "$2" "-" ; then
a63b05a9 2972 wvalue="$2"
2973 shift
2974 fi
8663fb7e 2975 if [ -z "$_webroot" ] ; then
a63b05a9 2976 _webroot="$wvalue"
2977 else
2978 _webroot="$_webroot,$wvalue"
2979 fi
2980 ;;
0e38c60d 2981 --dnssleep)
2982 _dnssleep="$2"
2983 Le_DNSSleep="$_dnssleep"
2984 shift
2985 ;;
2986
a63b05a9 2987 --keylength|-k)
2988 _keylength="$2"
2ce87fe2 2989 if [ "$_accountkeylength" = "no" ] ; then
2990 _accountkeylength="$2"
2991 fi
a63b05a9 2992 shift
2993 ;;
2994 --accountkeylength|-ak)
2ce87fe2 2995 _accountkeylength="$2"
a63b05a9 2996 shift
2997 ;;
2998
2999 --certpath)
3000 _certpath="$2"
3001 shift
3002 ;;
3003 --keypath)
3004 _keypath="$2"
3005 shift
3006 ;;
3007 --capath)
3008 _capath="$2"
3009 shift
3010 ;;
3011 --fullchainpath)
3012 _fullchainpath="$2"
3013 shift
3014 ;;
635695ec 3015 --reloadcmd|--reloadCmd)
a63b05a9 3016 _reloadcmd="$2"
3017 shift
3018 ;;
3019 --password)
3020 _password="$2"
3021 shift
3022 ;;
3023 --accountconf)
635695ec 3024 _accountconf="$2"
3025 ACCOUNT_CONF_PATH="$_accountconf"
a7b7355d 3026 shift
a63b05a9 3027 ;;
a7b7355d 3028 --home)
a63b05a9 3029 LE_WORKING_DIR="$2"
a7b7355d 3030 shift
a63b05a9 3031 ;;
b2817897 3032 --certhome)
3033 _certhome="$2"
3034 CERT_HOME="$_certhome"
3035 shift
3036 ;;
635695ec 3037 --useragent)
3038 _useragent="$2"
3039 USER_AGENT="$_useragent"
3040 shift
3041 ;;
b5eb4b90 3042 --accountemail )
3043 _accountemail="$2"
3044 ACCOUNT_EMAIL="$_accountemail"
3045 shift
3046 ;;
3047 --accountkey )
3048 _accountkey="$2"
3049 ACCOUNT_KEY_PATH="$_accountkey"
3050 shift
3051 ;;
06625071 3052 --days )
3053 _days="$2"
3054 Le_RenewalDays="$_days"
3055 shift
3056 ;;
39c8f79f 3057 --httpport )
3058 _httpport="$2"
3059 Le_HTTPPort="$_httpport"
3060 shift
3061 ;;
e22bcf7c 3062 --tlsport )
3063 _tlsport="$2"
3064 Le_TLSPort="$_tlsport"
3065 shift
3066 ;;
3067
dcf4f8f6 3068 --listraw )
3069 _listraw="raw"
3070 ;;
cc179731 3071 --stopRenewOnError|--stoprenewonerror|-se )
3072 _stopRenewOnError="1"
3073 ;;
13d7cae9 3074 --insecure)
3075 _insecure="1"
fac1e367 3076 HTTPS_INSECURE="1"
13d7cae9 3077 ;;
c8e9a31e 3078 --nocron)
3079 _nocron="1"
3080 ;;
43822d37 3081 --ecc)
3082 _ecc="isEcc"
3083 ;;
3084
a63b05a9 3085 *)
3086 _err "Unknown parameter : $1"
3087 return 1
3088 ;;
3089 esac
3090
3091 shift 1
3092 done
3093
dcf9cb58 3094 if [ "$DEBUG" ] ; then
3095 version
3096 fi
a63b05a9 3097
3098 case "${_CMD}" in
c8e9a31e 3099 install) install "$_nocron" ;;
bc96082f 3100 uninstall) uninstall "$_nocron" ;;
52677b0a 3101 upgrade) upgrade ;;
a63b05a9 3102 issue)
70a55875 3103 issue "$_webroot" "$_domain" "$_altdomains" "$_keylength" "$_certpath" "$_keypath" "$_capath" "$_reloadcmd" "$_fullchainpath"
a63b05a9 3104 ;;
3105 installcert)
43822d37 3106 installcert "$_domain" "$_certpath" "$_keypath" "$_capath" "$_reloadcmd" "$_fullchainpath" "$_ecc"
a63b05a9 3107 ;;
3108 renew)
43822d37 3109 renew "$_domain" "$_ecc"
a63b05a9 3110 ;;
3111 renewAll)
cc179731 3112 renewAll "$_stopRenewOnError"
a63b05a9 3113 ;;
3114 revoke)
43822d37 3115 revoke "$_domain" "$_ecc"
a63b05a9 3116 ;;
6d7eda3e 3117 list)
dcf4f8f6 3118 list "$_listraw"
6d7eda3e 3119 ;;
a63b05a9 3120 installcronjob) installcronjob ;;
3121 uninstallcronjob) uninstallcronjob ;;
3122 cron) cron ;;
3123 toPkcs)
43822d37 3124 toPkcs "$_domain" "$_password" "$_ecc"
a63b05a9 3125 ;;
3126 createAccountKey)
3127 createAccountKey "$_domain" "$_accountkeylength"
3128 ;;
3129 createDomainKey)
3130 createDomainKey "$_domain" "$_keylength"
3131 ;;
3132 createCSR)
43822d37 3133 createCSR "$_domain" "$_altdomains" "$_ecc"
a63b05a9 3134 ;;
3135
3136 *)
3137 _err "Invalid command: $_CMD"
3138 showhelp;
3139 return 1
3140 ;;
3141 esac
d3595686 3142 _ret="$?"
3143 if [ "$_ret" != "0" ] ; then
3144 return $_ret
3145 fi
a63b05a9 3146
8663fb7e 3147 if [ "$_useragent" ] ; then
635695ec 3148 _saveaccountconf "USER_AGENT" "$_useragent"
3149 fi
8663fb7e 3150 if [ "$_accountemail" ] ; then
b5eb4b90 3151 _saveaccountconf "ACCOUNT_EMAIL" "$_accountemail"
3152 fi
b2817897 3153
635695ec 3154
a63b05a9 3155}
3156
3157
8663fb7e 3158if [ "$INSTALLONLINE" ] ; then
d1f97fc8 3159 INSTALLONLINE=""
4a0f23e2 3160 _installOnline $BRANCH
3161 exit
3162fi
4c3b3608 3163
8663fb7e 3164if [ -z "$1" ] ; then
4c3b3608 3165 showhelp
3166else
036e9d10 3167 if echo "$1" | grep "^-" >/dev/null 2>&1 ; then
a63b05a9 3168 _process "$@"
3169 else
3170 "$@"
3171 fi
4c3b3608 3172fi
a63b05a9 3173
3174