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