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