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