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