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