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