]>
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 | ||
523c7682 | 30 | MAX_RENEW=80 |
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 | |
fac1e367 | 1030 | |
166096dc | 1031 | # url payload needbase64 keyfile |
4c3b3608 | 1032 | _send_signed_request() { |
1033 | url=$1 | |
1034 | payload=$2 | |
1035 | needbase64=$3 | |
166096dc | 1036 | keyfile=$4 |
8663fb7e | 1037 | if [ -z "$keyfile" ] ; then |
166096dc | 1038 | keyfile="$ACCOUNT_KEY_PATH" |
1039 | fi | |
4c3b3608 | 1040 | _debug url $url |
1041 | _debug payload "$payload" | |
1042 | ||
166096dc | 1043 | if ! _calcjwk "$keyfile" ; then |
1044 | return 1 | |
1045 | fi | |
c60883ef | 1046 | |
22ea4004 | 1047 | payload64=$(printf "%s" "$payload" | _base64 | _urlencode) |
1048 | _debug3 payload64 $payload64 | |
4c3b3608 | 1049 | |
1050 | nonceurl="$API/directory" | |
a272ee4f | 1051 | _headers="$(_get $nonceurl "onlyheader")" |
1052 | ||
7012b91f | 1053 | if [ "$?" != "0" ] ; then |
1054 | _err "Can not connect to $nonceurl to get nonce." | |
1055 | return 1 | |
1056 | fi | |
a272ee4f | 1057 | |
22ea4004 | 1058 | _debug3 _headers "$_headers" |
a272ee4f | 1059 | |
ef858ef0 | 1060 | nonce="$( echo "$_headers" | grep "Replay-Nonce:" | head -n 1 | tr -d "\r\n " | cut -d ':' -f 2)" |
a272ee4f | 1061 | |
22ea4004 | 1062 | _debug3 nonce "$nonce" |
4c3b3608 | 1063 | |
22ea4004 | 1064 | protected="$HEADERPLACE_PART1$nonce$HEADERPLACE_PART2" |
1065 | _debug3 protected "$protected" | |
4c3b3608 | 1066 | |
166096dc | 1067 | protected64="$(printf "$protected" | _base64 | _urlencode)" |
22ea4004 | 1068 | _debug3 protected64 "$protected64" |
166096dc | 1069 | |
22ea4004 | 1070 | sig=$(printf "%s" "$protected64.$payload64" | _sign "$keyfile" "sha256" | _urlencode) |
1071 | _debug3 sig "$sig" | |
4c3b3608 | 1072 | |
1073 | body="{\"header\": $HEADER, \"protected\": \"$protected64\", \"payload\": \"$payload64\", \"signature\": \"$sig\"}" | |
22ea4004 | 1074 | _debug3 body "$body" |
4c3b3608 | 1075 | |
bbbdcb09 | 1076 | |
eae29099 | 1077 | response="$(_post "$body" $url "$needbase64")" |
7012b91f | 1078 | if [ "$?" != "0" ] ; then |
9f43c270 | 1079 | _err "Can not post to $url" |
7012b91f | 1080 | return 1 |
1081 | fi | |
eae29099 | 1082 | _debug2 original "$response" |
1083 | ||
1084 | response="$( echo "$response" | _normalizeJson )" | |
4c3b3608 | 1085 | |
c60883ef | 1086 | responseHeaders="$(cat $HTTP_HEADER)" |
4c3b3608 | 1087 | |
a63b05a9 | 1088 | _debug2 responseHeaders "$responseHeaders" |
1089 | _debug2 response "$response" | |
c60883ef | 1090 | code="$(grep "^HTTP" $HTTP_HEADER | tail -1 | cut -d " " -f 2 | tr -d "\r\n" )" |
4c3b3608 | 1091 | _debug code $code |
1092 | ||
1093 | } | |
1094 | ||
4c3b3608 | 1095 | |
1096 | #setopt "file" "opt" "=" "value" [";"] | |
1097 | _setopt() { | |
1098 | __conf="$1" | |
1099 | __opt="$2" | |
1100 | __sep="$3" | |
1101 | __val="$4" | |
1102 | __end="$5" | |
8663fb7e | 1103 | if [ -z "$__opt" ] ; then |
43822d37 | 1104 | _usage usage: _setopt '"file" "opt" "=" "value" [";"]' |
4c3b3608 | 1105 | return |
1106 | fi | |
8663fb7e | 1107 | if [ ! -f "$__conf" ] ; then |
4c3b3608 | 1108 | touch "$__conf" |
1109 | fi | |
1110 | ||
22ea4004 | 1111 | if grep -n "^$__opt$__sep" "$__conf" > /dev/null ; then |
1112 | _debug3 OK | |
dceb3aca | 1113 | if _contains "$__val" "&" ; then |
4c3b3608 | 1114 | __val="$(echo $__val | sed 's/&/\\&/g')" |
1115 | fi | |
1116 | text="$(cat $__conf)" | |
6dfaaa70 | 1117 | echo "$text" | sed "s|^$__opt$__sep.*$|$__opt$__sep$__val$__end|" > "$__conf" |
4c3b3608 | 1118 | |
22ea4004 | 1119 | elif grep -n "^#$__opt$__sep" "$__conf" > /dev/null ; then |
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 | |
1126 | else | |
22ea4004 | 1127 | _debug3 APP |
4c3b3608 | 1128 | echo "$__opt$__sep$__val$__end" >> "$__conf" |
1129 | fi | |
22ea4004 | 1130 | _debug2 "$(grep -n "^$__opt$__sep" $__conf)" |
4c3b3608 | 1131 | } |
1132 | ||
1133 | #_savedomainconf key value | |
1134 | #save to domain.conf | |
1135 | _savedomainconf() { | |
0c9546cc | 1136 | _sdkey="$1" |
1137 | _sdvalue="$2" | |
8663fb7e | 1138 | if [ "$DOMAIN_CONF" ] ; then |
0c9546cc | 1139 | _setopt "$DOMAIN_CONF" "$_sdkey" "=" "\"$_sdvalue\"" |
4d2f38b0 | 1140 | else |
0c9546cc | 1141 | _err "DOMAIN_CONF is empty, can not save $_sdkey=$_sdvalue" |
4d2f38b0 | 1142 | fi |
1143 | } | |
1144 | ||
1145 | #_cleardomainconf key | |
1146 | _cleardomainconf() { | |
0c9546cc | 1147 | _sdkey="$1" |
4d2f38b0 | 1148 | if [ "$DOMAIN_CONF" ] ; then |
0c9546cc | 1149 | _sed_i "s/^$_sdkey.*$//" "$DOMAIN_CONF" |
4c3b3608 | 1150 | else |
0c9546cc | 1151 | _err "DOMAIN_CONF is empty, can not save $_sdkey=$value" |
4c3b3608 | 1152 | fi |
1153 | } | |
1154 | ||
61623d22 | 1155 | #_readdomainconf key |
1156 | _readdomainconf() { | |
0c9546cc | 1157 | _sdkey="$1" |
61623d22 | 1158 | if [ "$DOMAIN_CONF" ] ; then |
1159 | ( | |
0c9546cc | 1160 | eval $(grep "^$_sdkey *=" "$DOMAIN_CONF") |
1161 | eval "printf \"%s\" \"\$$_sdkey\"" | |
61623d22 | 1162 | ) |
1163 | else | |
0c9546cc | 1164 | _err "DOMAIN_CONF is empty, can not read $_sdkey" |
61623d22 | 1165 | fi |
1166 | } | |
1167 | ||
4c3b3608 | 1168 | #_saveaccountconf key value |
1169 | _saveaccountconf() { | |
0c9546cc | 1170 | _sckey="$1" |
1171 | _scvalue="$2" | |
8663fb7e | 1172 | if [ "$ACCOUNT_CONF_PATH" ] ; then |
0c9546cc | 1173 | _setopt "$ACCOUNT_CONF_PATH" "$_sckey" "=" "\"$_scvalue\"" |
4c3b3608 | 1174 | else |
0c9546cc | 1175 | _err "ACCOUNT_CONF_PATH is empty, can not save $_sckey=$_scvalue" |
4c3b3608 | 1176 | fi |
1177 | } | |
1178 | ||
fac1e367 | 1179 | #_clearaccountconf key |
1180 | _clearaccountconf() { | |
0c9546cc | 1181 | _scvalue="$1" |
fac1e367 | 1182 | if [ "$ACCOUNT_CONF_PATH" ] ; then |
0c9546cc | 1183 | _sed_i "s/^$_scvalue.*$//" "$ACCOUNT_CONF_PATH" |
fac1e367 | 1184 | else |
0c9546cc | 1185 | _err "ACCOUNT_CONF_PATH is empty, can not clear $_scvalue" |
fac1e367 | 1186 | fi |
1187 | } | |
1188 | ||
0463b5d6 | 1189 | # content localaddress |
4c3b3608 | 1190 | _startserver() { |
1191 | content="$1" | |
0463b5d6 | 1192 | ncaddr="$2" |
1193 | _debug "ncaddr" "$ncaddr" | |
1194 | ||
6fc1447f | 1195 | _debug "startserver: $$" |
1b2e940d | 1196 | nchelp="$(nc -h 2>&1)" |
850c1128 | 1197 | |
399306a1 | 1198 | if echo "$nchelp" | grep "\-q[ ,]" >/dev/null ; then |
0463b5d6 | 1199 | _NC="nc -q 1 -l $ncaddr" |
850c1128 | 1200 | else |
f76eb452 | 1201 | if echo "$nchelp" | grep "GNU netcat" >/dev/null && echo "$nchelp" | grep "\-c, \-\-close" >/dev/null ; then |
0463b5d6 | 1202 | _NC="nc -c -l $ncaddr" |
6d60f288 | 1203 | elif echo "$nchelp" | grep "\-N" |grep "Shutdown the network socket after EOF on stdin" >/dev/null ; then |
0463b5d6 | 1204 | _NC="nc -N -l $ncaddr" |
f76eb452 | 1205 | else |
0463b5d6 | 1206 | _NC="nc -l $ncaddr" |
f76eb452 | 1207 | fi |
4c3b3608 | 1208 | fi |
1b2e940d | 1209 | |
39c8f79f | 1210 | _debug Le_HTTPPort "$Le_HTTPPort" |
6ae0f7f5 | 1211 | _debug Le_Listen_V4 "$Le_Listen_V4" |
1212 | _debug Le_Listen_V6 "$Le_Listen_V6" | |
1213 | if [ "$Le_Listen_V4" ] ; then | |
1214 | _NC="$_NC -4" | |
1215 | elif [ "$Le_Listen_V6" ] ; then | |
1216 | _NC="$_NC -6" | |
1217 | fi | |
1218 | _debug "_NC" "$_NC" | |
1219 | ||
4c3b3608 | 1220 | # while true ; do |
8663fb7e | 1221 | if [ "$DEBUG" ] ; then |
2c554a4b | 1222 | if ! printf "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC -p $Le_HTTPPort ; then |
1223 | printf "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC $Le_HTTPPort ; | |
3aff11f6 | 1224 | fi |
4c3b3608 | 1225 | else |
2c554a4b | 1226 | if ! printf "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC -p $Le_HTTPPort > /dev/null 2>&1; then |
1227 | printf "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC $Le_HTTPPort > /dev/null 2>&1 | |
3aff11f6 | 1228 | fi |
4c3b3608 | 1229 | fi |
8663fb7e | 1230 | if [ "$?" != "0" ] ; then |
051c706d | 1231 | _err "nc listen error." |
6fc1447f | 1232 | exit 1 |
051c706d | 1233 | fi |
4c3b3608 | 1234 | # done |
1235 | } | |
1236 | ||
6fc1447f | 1237 | _stopserver(){ |
4c3b3608 | 1238 | pid="$1" |
6fc1447f | 1239 | _debug "pid" "$pid" |
8663fb7e | 1240 | if [ -z "$pid" ] ; then |
6fc1447f | 1241 | return |
1242 | fi | |
e22bcf7c | 1243 | |
dcf9cb58 | 1244 | _debug2 "Le_HTTPPort" "$Le_HTTPPort" |
1245 | if [ "$Le_HTTPPort" ] ; then | |
276b51d9 | 1246 | if [ "$DEBUG" ] && [ "$DEBUG" -gt "3" ] ; then |
22ea4004 | 1247 | _get "http://localhost:$Le_HTTPPort" "" 1 |
dcf9cb58 | 1248 | else |
22ea4004 | 1249 | _get "http://localhost:$Le_HTTPPort" "" 1 >/dev/null 2>&1 |
dcf9cb58 | 1250 | fi |
1251 | fi | |
1252 | ||
1253 | _debug2 "Le_TLSPort" "$Le_TLSPort" | |
1254 | if [ "$Le_TLSPort" ] ; then | |
276b51d9 | 1255 | if [ "$DEBUG" ] && [ "$DEBUG" -gt "3" ] ; then |
75da0713 | 1256 | _get "https://localhost:$Le_TLSPort" "" 1 |
1257 | _get "https://localhost:$Le_TLSPort" "" 1 | |
dcf9cb58 | 1258 | else |
75da0713 | 1259 | _get "https://localhost:$Le_TLSPort" "" 1 >/dev/null 2>&1 |
1260 | _get "https://localhost:$Le_TLSPort" "" 1 >/dev/null 2>&1 | |
dcf9cb58 | 1261 | fi |
1262 | fi | |
4c3b3608 | 1263 | } |
1264 | ||
fdcb6b72 | 1265 | # sleep sec |
1266 | _sleep() { | |
1267 | _sleep_sec="$1" | |
1268 | if [ "$__INTERACTIVE" ] ; then | |
fdcb6b72 | 1269 | _sleep_c="$_sleep_sec" |
1270 | while [ "$_sleep_c" -ge "0" ] ; | |
1271 | do | |
c583d6bb | 1272 | printf "\r \r" |
fdcb6b72 | 1273 | __green "$_sleep_c" |
1274 | _sleep_c="$(_math $_sleep_c - 1)" | |
1275 | sleep 1 | |
1276 | done | |
c583d6bb | 1277 | printf "\r" |
fdcb6b72 | 1278 | else |
1279 | sleep "$_sleep_sec" | |
1280 | fi | |
1281 | } | |
e22bcf7c | 1282 | |
6ae0f7f5 | 1283 | # _starttlsserver san_a san_b port content _ncaddr |
e22bcf7c | 1284 | _starttlsserver() { |
1285 | _info "Starting tls server." | |
1286 | san_a="$1" | |
1287 | san_b="$2" | |
1288 | port="$3" | |
1289 | content="$4" | |
6ae0f7f5 | 1290 | opaddr="$5" |
e22bcf7c | 1291 | |
1292 | _debug san_a "$san_a" | |
1293 | _debug san_b "$san_b" | |
1294 | _debug port "$port" | |
1295 | ||
1296 | #create key TLS_KEY | |
1297 | if ! _createkey "2048" "$TLS_KEY" ; then | |
1298 | _err "Create tls validation key error." | |
1299 | return 1 | |
1300 | fi | |
1301 | ||
1302 | #create csr | |
1303 | alt="$san_a" | |
1304 | if [ "$san_b" ] ; then | |
1305 | alt="$alt,$san_b" | |
1306 | fi | |
1307 | if ! _createcsr "tls.acme.sh" "$alt" "$TLS_KEY" "$TLS_CSR" "$TLS_CONF" ; then | |
1308 | _err "Create tls validation csr error." | |
1309 | return 1 | |
1310 | fi | |
1311 | ||
1312 | #self signed | |
1313 | if ! _signcsr "$TLS_KEY" "$TLS_CSR" "$TLS_CONF" "$TLS_CERT" ; then | |
1314 | _err "Create tls validation cert error." | |
1315 | return 1 | |
1316 | fi | |
1317 | ||
6ae0f7f5 | 1318 | __S_OPENSSL="openssl s_server -cert $TLS_CERT -key $TLS_KEY " |
1319 | if [ "$opaddr" ] ; then | |
1320 | __S_OPENSSL="$__S_OPENSSL -accept $opaddr:$port" | |
1321 | else | |
1322 | __S_OPENSSL="$__S_OPENSSL -accept $port" | |
1323 | fi | |
1324 | ||
1325 | _debug Le_Listen_V4 "$Le_Listen_V4" | |
1326 | _debug Le_Listen_V6 "$Le_Listen_V6" | |
1327 | if [ "$Le_Listen_V4" ] ; then | |
1328 | __S_OPENSSL="$__S_OPENSSL -4" | |
1329 | elif [ "$Le_Listen_V6" ] ; then | |
1330 | __S_OPENSSL="$__S_OPENSSL -6" | |
1331 | fi | |
1332 | ||
e22bcf7c | 1333 | #start openssl |
6ae0f7f5 | 1334 | _debug "$__S_OPENSSL" |
fbad6a39 | 1335 | if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ] ; then |
6ae0f7f5 | 1336 | (printf "HTTP/1.1 200 OK\r\n\r\n$content" | $__S_OPENSSL -tlsextdebug ) & |
331c4bb6 | 1337 | else |
6ae0f7f5 | 1338 | (printf "HTTP/1.1 200 OK\r\n\r\n$content" | $__S_OPENSSL >/dev/null 2>&1) & |
331c4bb6 | 1339 | fi |
1340 | ||
e22bcf7c | 1341 | serverproc="$!" |
c583d6bb | 1342 | sleep 2 |
e22bcf7c | 1343 | _debug serverproc $serverproc |
1344 | } | |
1345 | ||
18e46962 | 1346 | #file |
1347 | _readlink() { | |
1348 | _rf="$1" | |
1349 | if ! readlink -f "$_rf" 2>/dev/null; then | |
7da50703 | 1350 | if _startswith "$_rf" "\./$PROJECT_ENTRY" ; then |
1351 | printf -- "%s" "$(pwd)/$PROJECT_ENTRY" | |
1352 | return 0 | |
1353 | fi | |
18e46962 | 1354 | readlink "$_rf" |
1355 | fi | |
1356 | } | |
1357 | ||
5ea6e9c9 | 1358 | __initHome() { |
f3e4cea3 | 1359 | if [ -z "$_SCRIPT_HOME" ] ; then |
1360 | if _exists readlink && _exists dirname ; then | |
1361 | _debug "Lets guess script dir." | |
1362 | _debug "_SCRIPT_" "$_SCRIPT_" | |
18e46962 | 1363 | _script="$(_readlink "$_SCRIPT_")" |
f3e4cea3 | 1364 | _debug "_script" "$_script" |
1365 | _script_home="$(dirname "$_script")" | |
1366 | _debug "_script_home" "$_script_home" | |
1367 | if [ -d "$_script_home" ] ; then | |
1368 | _SCRIPT_HOME="$_script_home" | |
1369 | else | |
1370 | _err "It seems the script home is not correct:$_script_home" | |
1371 | fi | |
1372 | fi | |
1373 | fi | |
1374 | ||
1375 | ||
8663fb7e | 1376 | if [ -z "$LE_WORKING_DIR" ] ; then |
f3e4cea3 | 1377 | if [ -f "$DEFAULT_INSTALL_HOME/account.conf" ] ; then |
7da50703 | 1378 | _debug "It seems that $PROJECT_NAME is already installed in $DEFAULT_INSTALL_HOME" |
f3e4cea3 | 1379 | LE_WORKING_DIR="$DEFAULT_INSTALL_HOME" |
1380 | else | |
1381 | LE_WORKING_DIR="$_SCRIPT_HOME" | |
1382 | fi | |
4c3b3608 | 1383 | fi |
1384 | ||
f3e4cea3 | 1385 | if [ -z "$LE_WORKING_DIR" ] ; then |
1386 | _debug "Using default home:$DEFAULT_INSTALL_HOME" | |
1387 | LE_WORKING_DIR="$DEFAULT_INSTALL_HOME" | |
1388 | fi | |
7da50703 | 1389 | export LE_WORKING_DIR |
f3e4cea3 | 1390 | |
d53289d7 | 1391 | _DEFAULT_ACCOUNT_CONF_PATH="$LE_WORKING_DIR/account.conf" |
1392 | ||
8663fb7e | 1393 | if [ -z "$ACCOUNT_CONF_PATH" ] ; then |
1394 | if [ -f "$_DEFAULT_ACCOUNT_CONF_PATH" ] ; then | |
1395 | . "$_DEFAULT_ACCOUNT_CONF_PATH" | |
635695ec | 1396 | fi |
d53289d7 | 1397 | fi |
1398 | ||
8663fb7e | 1399 | if [ -z "$ACCOUNT_CONF_PATH" ] ; then |
d53289d7 | 1400 | ACCOUNT_CONF_PATH="$_DEFAULT_ACCOUNT_CONF_PATH" |
4c3b3608 | 1401 | fi |
d0871bda | 1402 | |
1403 | DEFAULT_LOG_FILE="$LE_WORKING_DIR/$PROJECT_NAME.log" | |
5c48e139 | 1404 | |
1405 | DEFAULT_CA_HOME="$LE_WORKING_DIR/ca" | |
5ea6e9c9 | 1406 | } |
1407 | ||
1408 | #[domain] [keylength] | |
1409 | _initpath() { | |
1410 | ||
1411 | __initHome | |
1412 | ||
8663fb7e | 1413 | if [ -f "$ACCOUNT_CONF_PATH" ] ; then |
1414 | . "$ACCOUNT_CONF_PATH" | |
4c3b3608 | 1415 | fi |
1416 | ||
8663fb7e | 1417 | if [ "$IN_CRON" ] ; then |
1418 | if [ ! "$_USER_PATH_EXPORTED" ] ; then | |
281aa349 | 1419 | _USER_PATH_EXPORTED=1 |
1420 | export PATH="$USER_PATH:$PATH" | |
1421 | fi | |
1422 | fi | |
5c48e139 | 1423 | |
1424 | if [ -z "$CA_HOME" ] ; then | |
1425 | CA_HOME="$DEFAULT_CA_HOME" | |
1426 | fi | |
281aa349 | 1427 | |
8663fb7e | 1428 | if [ -z "$API" ] ; then |
1429 | if [ -z "$STAGE" ] ; then | |
4c3b3608 | 1430 | API="$DEFAULT_CA" |
1431 | else | |
1432 | API="$STAGE_CA" | |
1433 | _info "Using stage api:$API" | |
1434 | fi | |
1435 | fi | |
1436 | ||
5c48e139 | 1437 | _API_HOST="$(echo "$API" | cut -d : -f 2 | tr -d '/')" |
1438 | CA_DIR="$CA_HOME/$_API_HOST" | |
1439 | ||
1440 | _DEFAULT_CA_CONF="$CA_DIR/ca.conf" | |
1441 | ||
1442 | if [ -z "$CA_CONF" ] ; then | |
1443 | CA_CONF="$_DEFAULT_CA_CONF" | |
1444 | fi | |
1445 | ||
1446 | if [ -f "$CA_CONF" ] ; then | |
1447 | . "$CA_CONF" | |
1448 | fi | |
1449 | ||
8663fb7e | 1450 | if [ -z "$ACME_DIR" ] ; then |
4c3b3608 | 1451 | ACME_DIR="/home/.acme" |
1452 | fi | |
1453 | ||
8663fb7e | 1454 | if [ -z "$APACHE_CONF_BACKUP_DIR" ] ; then |
8a144f4d | 1455 | APACHE_CONF_BACKUP_DIR="$LE_WORKING_DIR" |
4c3b3608 | 1456 | fi |
1457 | ||
8663fb7e | 1458 | if [ -z "$USER_AGENT" ] ; then |
bbbdcb09 | 1459 | USER_AGENT="$DEFAULT_USER_AGENT" |
1460 | fi | |
1461 | ||
933c169d | 1462 | if [ -z "$HTTP_HEADER" ] ; then |
1463 | HTTP_HEADER="$LE_WORKING_DIR/http.header" | |
1464 | fi | |
b2817897 | 1465 | |
5c48e139 | 1466 | _OLD_ACCOUNT_KEY="$LE_WORKING_DIR/account.key" |
1467 | _OLD_ACCOUNT_JSON="$LE_WORKING_DIR/account.json" | |
1468 | ||
1469 | _DEFAULT_ACCOUNT_KEY_PATH="$CA_DIR/account.key" | |
1470 | _DEFAULT_ACCOUNT_JSON_PATH="$CA_DIR/account.json" | |
8663fb7e | 1471 | if [ -z "$ACCOUNT_KEY_PATH" ] ; then |
b2817897 | 1472 | ACCOUNT_KEY_PATH="$_DEFAULT_ACCOUNT_KEY_PATH" |
4c3b3608 | 1473 | fi |
b2817897 | 1474 | |
5c48e139 | 1475 | if [ -z "$ACCOUNT_JSON_PATH" ] ; then |
1476 | ACCOUNT_JSON_PATH="$_DEFAULT_ACCOUNT_JSON_PATH" | |
1477 | fi | |
1478 | ||
1479 | ||
a79b26af RD |
1480 | _DEFAULT_CERT_HOME="$LE_WORKING_DIR" |
1481 | if [ -z "$CERT_HOME" ] ; then | |
1482 | CERT_HOME="$_DEFAULT_CERT_HOME" | |
1483 | fi | |
1484 | ||
5fbc47eb | 1485 | if [ -z "$1" ] ; then |
4c3b3608 | 1486 | return 0 |
1487 | fi | |
5c48e139 | 1488 | |
1489 | mkdir -p "$CA_DIR" | |
1490 | ||
5fbc47eb | 1491 | domain="$1" |
1492 | _ilength="$2" | |
4c3b3608 | 1493 | |
8663fb7e | 1494 | if [ -z "$DOMAIN_PATH" ] ; then |
43822d37 | 1495 | domainhome="$CERT_HOME/$domain" |
1496 | domainhomeecc="$CERT_HOME/$domain$ECC_SUFFIX" | |
1497 | ||
4c3b3608 | 1498 | DOMAIN_PATH="$domainhome" |
43822d37 | 1499 | |
5fbc47eb | 1500 | if _isEccKey "$_ilength" ; then |
43822d37 | 1501 | DOMAIN_PATH="$domainhomeecc" |
1502 | else | |
1503 | if [ ! -d "$domainhome" ] && [ -d "$domainhomeecc" ] ; then | |
6d4e903b | 1504 | _info "The domain '$domain' seems to have a ECC cert already, please add '$(__red "--ecc")' parameter if you want to use that cert." |
43822d37 | 1505 | fi |
1506 | fi | |
1507 | _debug DOMAIN_PATH "$DOMAIN_PATH" | |
4c3b3608 | 1508 | fi |
43822d37 | 1509 | |
933c169d | 1510 | if [ ! -d "$DOMAIN_PATH" ] ; then |
1511 | if ! mkdir -p "$DOMAIN_PATH" ; then | |
1512 | _err "Can not create domain path: $DOMAIN_PATH" | |
1513 | return 1 | |
1514 | fi | |
1515 | fi | |
1516 | ||
8663fb7e | 1517 | if [ -z "$DOMAIN_CONF" ] ; then |
43822d37 | 1518 | DOMAIN_CONF="$DOMAIN_PATH/$domain.conf" |
4c3b3608 | 1519 | fi |
1520 | ||
8663fb7e | 1521 | if [ -z "$DOMAIN_SSL_CONF" ] ; then |
0c9546cc | 1522 | DOMAIN_SSL_CONF="$DOMAIN_PATH/$domain.csr.conf" |
4c3b3608 | 1523 | fi |
1524 | ||
8663fb7e | 1525 | if [ -z "$CSR_PATH" ] ; then |
43822d37 | 1526 | CSR_PATH="$DOMAIN_PATH/$domain.csr" |
4c3b3608 | 1527 | fi |
8663fb7e | 1528 | if [ -z "$CERT_KEY_PATH" ] ; then |
43822d37 | 1529 | CERT_KEY_PATH="$DOMAIN_PATH/$domain.key" |
4c3b3608 | 1530 | fi |
8663fb7e | 1531 | if [ -z "$CERT_PATH" ] ; then |
43822d37 | 1532 | CERT_PATH="$DOMAIN_PATH/$domain.cer" |
4c3b3608 | 1533 | fi |
8663fb7e | 1534 | if [ -z "$CA_CERT_PATH" ] ; then |
43822d37 | 1535 | CA_CERT_PATH="$DOMAIN_PATH/ca.cer" |
4c3b3608 | 1536 | fi |
8663fb7e | 1537 | if [ -z "$CERT_FULLCHAIN_PATH" ] ; then |
43822d37 | 1538 | CERT_FULLCHAIN_PATH="$DOMAIN_PATH/fullchain.cer" |
caf1fc10 | 1539 | fi |
8663fb7e | 1540 | if [ -z "$CERT_PFX_PATH" ] ; then |
43822d37 | 1541 | CERT_PFX_PATH="$DOMAIN_PATH/$domain.pfx" |
ac2d5123 | 1542 | fi |
e22bcf7c | 1543 | |
1544 | if [ -z "$TLS_CONF" ] ; then | |
43822d37 | 1545 | TLS_CONF="$DOMAIN_PATH/tls.valdation.conf" |
e22bcf7c | 1546 | fi |
1547 | if [ -z "$TLS_CERT" ] ; then | |
43822d37 | 1548 | TLS_CERT="$DOMAIN_PATH/tls.valdation.cert" |
e22bcf7c | 1549 | fi |
1550 | if [ -z "$TLS_KEY" ] ; then | |
43822d37 | 1551 | TLS_KEY="$DOMAIN_PATH/tls.valdation.key" |
e22bcf7c | 1552 | fi |
1553 | if [ -z "$TLS_CSR" ] ; then | |
43822d37 | 1554 | TLS_CSR="$DOMAIN_PATH/tls.valdation.csr" |
e22bcf7c | 1555 | fi |
1556 | ||
4c3b3608 | 1557 | } |
1558 | ||
1559 | ||
1560 | _apachePath() { | |
c3dd3ef0 | 1561 | _APACHECTL="apachectl" |
80a0a7b5 | 1562 | if ! _exists apachectl ; then |
e4a19585 | 1563 | if _exists apache2ctl ; then |
c3dd3ef0 | 1564 | _APACHECTL="apache2ctl" |
e4a19585 | 1565 | else |
bc96082f | 1566 | _err "'apachectl not found. It seems that apache is not installed, or you are not root user.'" |
e4a19585 | 1567 | _err "Please use webroot mode to try again." |
1568 | return 1 | |
1569 | fi | |
80a0a7b5 | 1570 | fi |
c3dd3ef0 | 1571 | httpdconfname="$($_APACHECTL -V | grep SERVER_CONFIG_FILE= | cut -d = -f 2 | tr -d '"' )" |
78768e98 | 1572 | _debug httpdconfname "$httpdconfname" |
dceb3aca | 1573 | if _startswith "$httpdconfname" '/' ; then |
d62ee940 | 1574 | httpdconf="$httpdconfname" |
c456d954 | 1575 | httpdconfname="$(basename $httpdconfname)" |
d62ee940 | 1576 | else |
c3dd3ef0 | 1577 | httpdroot="$($_APACHECTL -V | grep HTTPD_ROOT= | cut -d = -f 2 | tr -d '"' )" |
78768e98 | 1578 | _debug httpdroot "$httpdroot" |
d62ee940 | 1579 | httpdconf="$httpdroot/$httpdconfname" |
8f63baf7 | 1580 | httpdconfname="$(basename $httpdconfname)" |
d62ee940 | 1581 | fi |
78768e98 | 1582 | _debug httpdconf "$httpdconf" |
8f63baf7 | 1583 | _debug httpdconfname "$httpdconfname" |
78768e98 | 1584 | if [ ! -f "$httpdconf" ] ; then |
1585 | _err "Apache Config file not found" "$httpdconf" | |
4c3b3608 | 1586 | return 1 |
1587 | fi | |
1588 | return 0 | |
1589 | } | |
1590 | ||
1591 | _restoreApache() { | |
8663fb7e | 1592 | if [ -z "$usingApache" ] ; then |
4c3b3608 | 1593 | return 0 |
1594 | fi | |
1595 | _initpath | |
1596 | if ! _apachePath ; then | |
1597 | return 1 | |
1598 | fi | |
1599 | ||
8663fb7e | 1600 | if [ ! -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname" ] ; then |
4c3b3608 | 1601 | _debug "No config file to restore." |
1602 | return 0 | |
1603 | fi | |
1604 | ||
ff3bce32 | 1605 | cat "$APACHE_CONF_BACKUP_DIR/$httpdconfname" > "$httpdconf" |
5ef501c5 | 1606 | _debug "Restored: $httpdconf." |
c3dd3ef0 | 1607 | if ! $_APACHECTL -t >/dev/null 2>&1 ; then |
4c3b3608 | 1608 | _err "Sorry, restore apache config error, please contact me." |
1609 | return 1; | |
1610 | fi | |
5ef501c5 | 1611 | _debug "Restored successfully." |
4c3b3608 | 1612 | rm -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname" |
1613 | return 0 | |
1614 | } | |
1615 | ||
1616 | _setApache() { | |
1617 | _initpath | |
1618 | if ! _apachePath ; then | |
1619 | return 1 | |
1620 | fi | |
1621 | ||
5fc5016d | 1622 | #test the conf first |
869578ce | 1623 | _info "Checking if there is an error in the apache config file before starting." |
c3dd3ef0 | 1624 | _msg="$($_APACHECTL -t 2>&1 )" |
5fc5016d | 1625 | if [ "$?" != "0" ] ; then |
869578ce | 1626 | _err "Sorry, apache config file has error, please fix it first, then try again." |
1627 | _err "Don't worry, there is nothing changed to your system." | |
5fc5016d | 1628 | _err "$_msg" |
1629 | return 1; | |
1630 | else | |
1631 | _info "OK" | |
1632 | fi | |
1633 | ||
4c3b3608 | 1634 | #backup the conf |
5778811a | 1635 | _debug "Backup apache config file" "$httpdconf" |
8f63baf7 | 1636 | if ! cp "$httpdconf" "$APACHE_CONF_BACKUP_DIR/" ; then |
869578ce | 1637 | _err "Can not backup apache config file, so abort. Don't worry, the apache config is not changed." |
8f63baf7 | 1638 | _err "This might be a bug of $PROJECT_NAME , pleae report issue: $PROJECT" |
1639 | return 1 | |
1640 | fi | |
4c3b3608 | 1641 | _info "JFYI, Config file $httpdconf is backuped to $APACHE_CONF_BACKUP_DIR/$httpdconfname" |
1642 | _info "In case there is an error that can not be restored automatically, you may try restore it yourself." | |
1643 | _info "The backup file will be deleted on sucess, just forget it." | |
1644 | ||
1645 | #add alias | |
b09d597c | 1646 | |
c3dd3ef0 | 1647 | apacheVer="$($_APACHECTL -V | grep "Server version:" | cut -d : -f 2 | cut -d " " -f 2 | cut -d '/' -f 2 )" |
b09d597c | 1648 | _debug "apacheVer" "$apacheVer" |
1649 | apacheMajer="$(echo "$apacheVer" | cut -d . -f 1)" | |
1650 | apacheMinor="$(echo "$apacheVer" | cut -d . -f 2)" | |
1651 | ||
5778811a | 1652 | if [ "$apacheVer" ] && [ "$apacheMajer$apacheMinor" -ge "24" ] ; then |
b09d597c | 1653 | echo " |
4c3b3608 | 1654 | Alias /.well-known/acme-challenge $ACME_DIR |
1655 | ||
1656 | <Directory $ACME_DIR > | |
1657 | Require all granted | |
b09d597c | 1658 | </Directory> |
5778811a | 1659 | " >> "$httpdconf" |
b09d597c | 1660 | else |
1661 | echo " | |
1662 | Alias /.well-known/acme-challenge $ACME_DIR | |
1663 | ||
1664 | <Directory $ACME_DIR > | |
1665 | Order allow,deny | |
1666 | Allow from all | |
4c3b3608 | 1667 | </Directory> |
5778811a | 1668 | " >> "$httpdconf" |
b09d597c | 1669 | fi |
1670 | ||
c3dd3ef0 | 1671 | _msg="$($_APACHECTL -t 2>&1 )" |
5fc5016d | 1672 | if [ "$?" != "0" ] ; then |
1673 | _err "Sorry, apache config error" | |
1674 | if _restoreApache ; then | |
869578ce | 1675 | _err "The apache config file is restored." |
5fc5016d | 1676 | else |
869578ce | 1677 | _err "Sorry, The apache config file can not be restored, please report bug." |
5fc5016d | 1678 | fi |
4c3b3608 | 1679 | return 1; |
1680 | fi | |
1681 | ||
8663fb7e | 1682 | if [ ! -d "$ACME_DIR" ] ; then |
4c3b3608 | 1683 | mkdir -p "$ACME_DIR" |
1684 | chmod 755 "$ACME_DIR" | |
1685 | fi | |
1686 | ||
c3dd3ef0 | 1687 | if ! $_APACHECTL graceful ; then |
1688 | _err "Sorry, $_APACHECTL graceful error, please contact me." | |
4c3b3608 | 1689 | _restoreApache |
1690 | return 1; | |
1691 | fi | |
1692 | usingApache="1" | |
1693 | return 0 | |
1694 | } | |
1695 | ||
5ef501c5 | 1696 | _clearup() { |
4c3b3608 | 1697 | _stopserver $serverproc |
1698 | serverproc="" | |
1699 | _restoreApache | |
e22bcf7c | 1700 | if [ -z "$DEBUG" ] ; then |
1701 | rm -f "$TLS_CONF" | |
1702 | rm -f "$TLS_CERT" | |
1703 | rm -f "$TLS_KEY" | |
1704 | rm -f "$TLS_CSR" | |
1705 | fi | |
4c3b3608 | 1706 | } |
1707 | ||
1708 | # webroot removelevel tokenfile | |
1709 | _clearupwebbroot() { | |
1710 | __webroot="$1" | |
8663fb7e | 1711 | if [ -z "$__webroot" ] ; then |
4c3b3608 | 1712 | _debug "no webroot specified, skip" |
1713 | return 0 | |
1714 | fi | |
1715 | ||
dcf9cb58 | 1716 | _rmpath="" |
8663fb7e | 1717 | if [ "$2" = '1' ] ; then |
dcf9cb58 | 1718 | _rmpath="$__webroot/.well-known" |
8663fb7e | 1719 | elif [ "$2" = '2' ] ; then |
dcf9cb58 | 1720 | _rmpath="$__webroot/.well-known/acme-challenge" |
8663fb7e | 1721 | elif [ "$2" = '3' ] ; then |
dcf9cb58 | 1722 | _rmpath="$__webroot/.well-known/acme-challenge/$3" |
4c3b3608 | 1723 | else |
cc179731 | 1724 | _debug "Skip for removelevel:$2" |
4c3b3608 | 1725 | fi |
1726 | ||
dcf9cb58 | 1727 | if [ "$_rmpath" ] ; then |
1728 | if [ "$DEBUG" ] ; then | |
1729 | _debug "Debugging, skip removing: $_rmpath" | |
1730 | else | |
1731 | rm -rf "$_rmpath" | |
1732 | fi | |
1733 | fi | |
1734 | ||
4c3b3608 | 1735 | return 0 |
1736 | ||
1737 | } | |
1738 | ||
b0070f03 | 1739 | _on_before_issue() { |
30c2d84c | 1740 | _debug _on_before_issue |
0463b5d6 | 1741 | if _hasfield "$Le_Webroot" "$NO_VALUE" ; then |
1742 | if ! _exists "nc" ; then | |
1743 | _err "Please install netcat(nc) tools first." | |
1744 | return 1 | |
1745 | fi | |
1746 | elif ! _hasfield "$Le_Webroot" "$W_TLS" ; then | |
1747 | #no need to check anymore | |
1748 | return 0 | |
1749 | fi | |
1750 | ||
1751 | _debug Le_LocalAddress "$Le_LocalAddress" | |
1752 | ||
1753 | alldomains=$(echo "$Le_Domain,$Le_Alt" | tr ',' ' ' ) | |
1754 | _index=1 | |
1755 | _currentRoot="" | |
1756 | _addrIndex=1 | |
1757 | for d in $alldomains | |
1758 | do | |
1759 | _debug "Check for domain" $d | |
1760 | _currentRoot="$(_getfield "$Le_Webroot" $_index)" | |
1761 | _debug "_currentRoot" "$_currentRoot" | |
1762 | _index=$(_math $_index + 1) | |
1763 | _checkport="" | |
1764 | if [ "$_currentRoot" = "$NO_VALUE" ] ; then | |
1765 | _info "Standalone mode." | |
1766 | if [ -z "$Le_HTTPPort" ] ; then | |
1767 | Le_HTTPPort=80 | |
1768 | else | |
1769 | _savedomainconf "Le_HTTPPort" "$Le_HTTPPort" | |
1770 | fi | |
1771 | _checkport="$Le_HTTPPort" | |
1772 | elif [ "$_currentRoot" = "$W_TLS" ] ; then | |
1773 | _info "Standalone tls mode." | |
1774 | if [ -z "$Le_TLSPort" ] ; then | |
1775 | Le_TLSPort=443 | |
1776 | else | |
1777 | _savedomainconf "Le_TLSPort" "$Le_TLSPort" | |
1778 | fi | |
1779 | _checkport="$Le_TLSPort" | |
1780 | fi | |
1781 | ||
1782 | if [ "$_checkport" ] ; then | |
1783 | _debug _checkport "$_checkport" | |
1784 | _checkaddr="$(_getfield "$Le_LocalAddress" $_addrIndex)" | |
1785 | _debug _checkaddr "$_checkaddr" | |
1786 | ||
1787 | _addrIndex="$(_math $_addrIndex + 1)" | |
1788 | ||
1789 | _netprc="$(_ss "$_checkport" | grep "$_checkport")" | |
1790 | netprc="$(echo "$_netprc" | grep "$_checkaddr")" | |
1791 | if [ -z "$netprc" ] ; then | |
1792 | netprc="$(echo "$_netprc" | grep "$LOCAL_ANY_ADDRESS")" | |
1793 | fi | |
1794 | if [ "$netprc" ] ; then | |
1795 | _err "$netprc" | |
1796 | _err "tcp port $_checkport is already used by $(echo "$netprc" | cut -d : -f 4)" | |
1797 | _err "Please stop it first" | |
1798 | return 1 | |
1799 | fi | |
1800 | fi | |
1801 | done | |
1802 | ||
1803 | if _hasfield "$Le_Webroot" "apache" ; then | |
1804 | if ! _setApache ; then | |
1805 | _err "set up apache error. Report error to me." | |
1806 | return 1 | |
1807 | fi | |
1808 | else | |
1809 | usingApache="" | |
1810 | fi | |
1811 | ||
b0070f03 | 1812 | #run pre hook |
1813 | if [ "$Le_PreHook" ] ; then | |
1814 | _info "Run pre hook:'$Le_PreHook'" | |
1815 | if ! ( | |
1816 | cd "$DOMAIN_PATH" && eval "$Le_PreHook" | |
1817 | ) ; then | |
1818 | _err "Error when run pre hook." | |
1819 | return 1 | |
1820 | fi | |
1821 | fi | |
1822 | } | |
1823 | ||
1824 | _on_issue_err() { | |
30c2d84c | 1825 | _debug _on_issue_err |
a73c5b33 | 1826 | if [ "$LOG_FILE" ] ; then |
1827 | _err "Please check log file for more details: $LOG_FILE" | |
1828 | else | |
1829 | _err "Please use add '--debug' or '--log' to check more details." | |
1830 | _err "See: $_DEBUG_WIKI" | |
1831 | fi | |
1832 | ||
b0070f03 | 1833 | #run the post hook |
1834 | if [ "$Le_PostHook" ] ; then | |
1835 | _info "Run post hook:'$Le_PostHook'" | |
1836 | if ! ( | |
1837 | cd "$DOMAIN_PATH" && eval "$Le_PostHook" | |
1838 | ) ; then | |
1839 | _err "Error when run post hook." | |
1840 | return 1 | |
1841 | fi | |
1842 | fi | |
1843 | } | |
1844 | ||
1845 | _on_issue_success() { | |
30c2d84c | 1846 | _debug _on_issue_success |
b0070f03 | 1847 | #run the post hook |
1848 | if [ "$Le_PostHook" ] ; then | |
1849 | _info "Run post hook:'$Le_PostHook'" | |
1850 | if ! ( | |
1851 | cd "$DOMAIN_PATH" && eval "$Le_PostHook" | |
1852 | ) ; then | |
1853 | _err "Error when run post hook." | |
1854 | return 1 | |
1855 | fi | |
1856 | fi | |
1857 | ||
1858 | #run renew hook | |
1859 | if [ "$IS_RENEW" ] && [ "$Le_RenewHook" ] ; then | |
1860 | _info "Run renew hook:'$Le_RenewHook'" | |
1861 | if ! ( | |
1862 | cd "$DOMAIN_PATH" && eval "$Le_RenewHook" | |
1863 | ) ; then | |
1864 | _err "Error when run renew hook." | |
1865 | return 1 | |
1866 | fi | |
1867 | fi | |
1868 | ||
1869 | } | |
1870 | ||
eb59817e | 1871 | updateaccount() { |
1872 | _initpath | |
1873 | _regAccount | |
1874 | } | |
b0070f03 | 1875 | |
eb59817e | 1876 | registeraccount() { |
1877 | _initpath | |
1878 | _regAccount | |
1879 | } | |
d404e92d | 1880 | |
1881 | _regAccount() { | |
1882 | _initpath | |
5c48e139 | 1883 | |
1884 | if [ ! -f "$ACCOUNT_KEY_PATH" ] && [ -f "$_OLD_ACCOUNT_KEY" ]; then | |
1885 | _info "mv $_OLD_ACCOUNT_KEY to $ACCOUNT_KEY_PATH" | |
1886 | mv "$_OLD_ACCOUNT_KEY" "$ACCOUNT_KEY_PATH" | |
1887 | fi | |
1888 | ||
1889 | if [ ! -f "$ACCOUNT_JSON_PATH" ] && [ -f "$_OLD_ACCOUNT_JSON" ]; then | |
1890 | _info "mv $_OLD_ACCOUNT_JSON to $ACCOUNT_JSON_PATH" | |
1891 | mv "$_OLD_ACCOUNT_JSON" "$ACCOUNT_JSON_PATH" | |
1892 | fi | |
1893 | ||
d404e92d | 1894 | if [ ! -f "$ACCOUNT_KEY_PATH" ] ; then |
1895 | _acck="no" | |
1896 | if [ "$Le_Keylength" ] ; then | |
1897 | _acck="$Le_Keylength" | |
1898 | fi | |
1899 | if ! createAccountKey "$_acck" ; then | |
1900 | _err "Create account key error." | |
1901 | return 1 | |
1902 | fi | |
1903 | fi | |
1904 | ||
1905 | if ! _calcjwk "$ACCOUNT_KEY_PATH" ; then | |
1906 | return 1 | |
1907 | fi | |
1908 | ||
1909 | _updateTos="" | |
1910 | _reg_res="new-reg" | |
1911 | while true ; | |
1912 | do | |
1913 | _debug AGREEMENT "$AGREEMENT" | |
1914 | accountkey_json=$(printf "%s" "$jwk" | tr -d ' ' ) | |
1915 | thumbprint=$(printf "%s" "$accountkey_json" | _digest "sha256" | _urlencode) | |
1916 | ||
1917 | regjson='{"resource": "'$_reg_res'", "agreement": "'$AGREEMENT'"}' | |
1918 | ||
1919 | if [ "$ACCOUNT_EMAIL" ] ; then | |
1920 | regjson='{"resource": "'$_reg_res'", "contact": ["mailto: '$ACCOUNT_EMAIL'"], "agreement": "'$AGREEMENT'"}' | |
1921 | fi | |
1922 | ||
1923 | if [ -z "$_updateTos" ] ; then | |
1924 | _info "Registering account" | |
1925 | ||
1926 | if ! _send_signed_request "$API/acme/new-reg" "$regjson" ; then | |
1927 | _err "Register account Error: $response" | |
1928 | return 1 | |
1929 | fi | |
1930 | ||
1931 | if [ "$code" = "" ] || [ "$code" = '201' ] ; then | |
5c48e139 | 1932 | echo "$response" > $ACCOUNT_JSON_PATH |
d404e92d | 1933 | _info "Registered" |
1934 | elif [ "$code" = '409' ] ; then | |
1935 | _info "Already registered" | |
1936 | else | |
1937 | _err "Register account Error: $response" | |
1938 | return 1 | |
1939 | fi | |
1940 | ||
1941 | _accUri="$(echo "$responseHeaders" | grep "^Location:" | cut -d ' ' -f 2| tr -d "\r\n")" | |
1942 | _debug "_accUri" "$_accUri" | |
d404e92d | 1943 | |
1944 | _tos="$(echo "$responseHeaders" | grep "^Link:.*rel=\"terms-of-service\"" | _egrep_o "<.*>" | tr -d '<>')" | |
1945 | _debug "_tos" "$_tos" | |
1946 | if [ -z "$_tos" ] ; then | |
1947 | _debug "Use default tos: $DEFAULT_AGREEMENT" | |
1948 | _tos="$DEFAULT_AGREEMENT" | |
1949 | fi | |
1950 | if [ "$_tos" != "$AGREEMENT" ]; then | |
1951 | _updateTos=1 | |
1952 | AGREEMENT="$_tos" | |
1953 | _reg_res="reg" | |
1954 | continue | |
1955 | fi | |
1956 | ||
1957 | else | |
1958 | _debug "Update tos: $_tos" | |
1959 | if ! _send_signed_request "$_accUri" "$regjson" ; then | |
1960 | _err "Update tos error." | |
1961 | return 1 | |
1962 | fi | |
1963 | if [ "$code" = '202' ] ; then | |
eb59817e | 1964 | _info "Update success." |
d404e92d | 1965 | else |
eb59817e | 1966 | _err "Update error." |
d404e92d | 1967 | return 1 |
1968 | fi | |
1969 | fi | |
1970 | return 0 | |
1971 | done | |
1972 | ||
1973 | } | |
1974 | ||
1975 | ||
10afcaca | 1976 | #webroot, domain domainlist keylength |
4c3b3608 | 1977 | issue() { |
8663fb7e | 1978 | if [ -z "$2" ] ; then |
43822d37 | 1979 | _usage "Usage: $PROJECT_ENTRY --issue -d a.com -w /path/to/webroot/a.com/ " |
4c3b3608 | 1980 | return 1 |
1981 | fi | |
1982 | Le_Webroot="$1" | |
1983 | Le_Domain="$2" | |
1984 | Le_Alt="$3" | |
1985 | Le_Keylength="$4" | |
1986 | Le_RealCertPath="$5" | |
1987 | Le_RealKeyPath="$6" | |
1988 | Le_RealCACertPath="$7" | |
1989 | Le_ReloadCmd="$8" | |
a63b05a9 | 1990 | Le_RealFullChainPath="$9" |
b0070f03 | 1991 | Le_PreHook="${10}" |
1992 | Le_PostHook="${11}" | |
1993 | Le_RenewHook="${12}" | |
0463b5d6 | 1994 | Le_LocalAddress="${13}" |
4c3b3608 | 1995 | |
eccec5f6 | 1996 | #remove these later. |
8663fb7e | 1997 | if [ "$Le_Webroot" = "dns-cf" ] ; then |
eccec5f6 | 1998 | Le_Webroot="dns_cf" |
1999 | fi | |
8663fb7e | 2000 | if [ "$Le_Webroot" = "dns-dp" ] ; then |
eccec5f6 | 2001 | Le_Webroot="dns_dp" |
2002 | fi | |
8663fb7e | 2003 | if [ "$Le_Webroot" = "dns-cx" ] ; then |
eccec5f6 | 2004 | Le_Webroot="dns_cx" |
2005 | fi | |
950172dc | 2006 | _debug "Using api: $API" |
4c3b3608 | 2007 | |
43822d37 | 2008 | if [ ! "$IS_RENEW" ] ; then |
2009 | _initpath $Le_Domain "$Le_Keylength" | |
2010 | mkdir -p "$DOMAIN_PATH" | |
2011 | fi | |
eccec5f6 | 2012 | |
8663fb7e | 2013 | if [ -f "$DOMAIN_CONF" ] ; then |
61623d22 | 2014 | Le_NextRenewTime=$(_readdomainconf Le_NextRenewTime) |
a4270efa | 2015 | _debug Le_NextRenewTime "$Le_NextRenewTime" |
3aae1ae3 | 2016 | if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ $(_time) -lt $Le_NextRenewTime ] ; then |
e2053b22 | 2017 | _info "Skip, Next renewal time is: $(__green "$(_readdomainconf Le_NextRenewTimeStr)")" |
2018 | _info "Add '$(__red '--force')' to force to renew." | |
cc179731 | 2019 | return $RENEW_SKIP |
4c3b3608 | 2020 | fi |
2021 | fi | |
96a46cfc | 2022 | |
4d2f38b0 | 2023 | _savedomainconf "Le_Domain" "$Le_Domain" |
2024 | _savedomainconf "Le_Alt" "$Le_Alt" | |
2025 | _savedomainconf "Le_Webroot" "$Le_Webroot" | |
4c3b3608 | 2026 | |
b0070f03 | 2027 | _savedomainconf "Le_PreHook" "$Le_PreHook" |
2028 | _savedomainconf "Le_PostHook" "$Le_PostHook" | |
2029 | _savedomainconf "Le_RenewHook" "$Le_RenewHook" | |
0463b5d6 | 2030 | _savedomainconf "Le_LocalAddress" "$Le_LocalAddress" |
2031 | ||
6ae0f7f5 | 2032 | |
f6dcd989 | 2033 | Le_API="$API" |
2034 | _savedomainconf "Le_API" "$Le_API" | |
2035 | ||
3f4513b3 | 2036 | if [ "$Le_Alt" = "$NO_VALUE" ] ; then |
4c3b3608 | 2037 | Le_Alt="" |
2038 | fi | |
4c3b3608 | 2039 | |
d404e92d | 2040 | if [ "$Le_Keylength" = "$NO_VALUE" ] ; then |
2041 | Le_Keylength="" | |
2042 | fi | |
2043 | ||
0463b5d6 | 2044 | if ! _on_before_issue ; then |
2045 | _err "_on_before_issue." | |
2046 | return 1 | |
4c3b3608 | 2047 | fi |
0463b5d6 | 2048 | |
d404e92d | 2049 | if ! _regAccount ; then |
b0070f03 | 2050 | _on_issue_err |
166096dc | 2051 | return 1 |
2052 | fi | |
2053 | ||
166096dc | 2054 | |
10afcaca | 2055 | if [ -f "$CSR_PATH" ] && [ ! -f "$CERT_KEY_PATH" ] ; then |
2056 | _info "Signing from existing CSR." | |
2057 | else | |
2058 | _key=$(_readdomainconf Le_Keylength) | |
2059 | _debug "Read key length:$_key" | |
2060 | if [ ! -f "$CERT_KEY_PATH" ] || [ "$Le_Keylength" != "$_key" ] ; then | |
2061 | if ! createDomainKey $Le_Domain $Le_Keylength ; then | |
2062 | _err "Create domain key error." | |
2063 | _clearup | |
b0070f03 | 2064 | _on_issue_err |
10afcaca | 2065 | return 1 |
2066 | fi | |
2067 | fi | |
2068 | ||
2069 | if ! _createcsr "$Le_Domain" "$Le_Alt" "$CERT_KEY_PATH" "$CSR_PATH" "$DOMAIN_SSL_CONF" ; then | |
2070 | _err "Create CSR error." | |
5ef501c5 | 2071 | _clearup |
b0070f03 | 2072 | _on_issue_err |
41e3eafa | 2073 | return 1 |
2074 | fi | |
4c3b3608 | 2075 | fi |
10afcaca | 2076 | |
61623d22 | 2077 | _savedomainconf "Le_Keylength" "$Le_Keylength" |
58f41a19 | 2078 | |
4c3b3608 | 2079 | vlist="$Le_Vlist" |
2080 | # verify each domain | |
2081 | _info "Verify each domain" | |
2082 | sep='#' | |
8663fb7e | 2083 | if [ -z "$vlist" ] ; then |
4c3b3608 | 2084 | alldomains=$(echo "$Le_Domain,$Le_Alt" | tr ',' ' ' ) |
a63b05a9 | 2085 | _index=1 |
2086 | _currentRoot="" | |
4c3b3608 | 2087 | for d in $alldomains |
a63b05a9 | 2088 | do |
2089 | _info "Getting webroot for domain" $d | |
2090 | _w="$(echo $Le_Webroot | cut -d , -f $_index)" | |
0463b5d6 | 2091 | _info _w "$_w" |
8663fb7e | 2092 | if [ "$_w" ] ; then |
a63b05a9 | 2093 | _currentRoot="$_w" |
2094 | fi | |
2095 | _debug "_currentRoot" "$_currentRoot" | |
00a50605 | 2096 | _index=$(_math $_index + 1) |
a63b05a9 | 2097 | |
2098 | vtype="$VTYPE_HTTP" | |
dceb3aca | 2099 | if _startswith "$_currentRoot" "dns" ; then |
a63b05a9 | 2100 | vtype="$VTYPE_DNS" |
2101 | fi | |
e22bcf7c | 2102 | |
2103 | if [ "$_currentRoot" = "$W_TLS" ] ; then | |
2104 | vtype="$VTYPE_TLS" | |
2105 | fi | |
2106 | ||
0c00e870 | 2107 | _info "Getting new-authz for domain" $d |
c4d8fd83 | 2108 | |
2109 | if ! _send_signed_request "$API/acme/new-authz" "{\"resource\": \"new-authz\", \"identifier\": {\"type\": \"dns\", \"value\": \"$d\"}}" ; then | |
2110 | _err "Can not get domain token." | |
2111 | _clearup | |
b0070f03 | 2112 | _on_issue_err |
c4d8fd83 | 2113 | return 1 |
2114 | fi | |
2115 | ||
8663fb7e | 2116 | if [ ! -z "$code" ] && [ ! "$code" = '201' ] ; then |
4c3b3608 | 2117 | _err "new-authz error: $response" |
2118 | _clearup | |
b0070f03 | 2119 | _on_issue_err |
4c3b3608 | 2120 | return 1 |
2121 | fi | |
2122 | ||
fdcb6b72 | 2123 | entry="$(printf "%s\n" "$response" | _egrep_o '[^\{]*"type":"'$vtype'"[^\}]*')" |
4c3b3608 | 2124 | _debug entry "$entry" |
19539575 | 2125 | if [ -z "$entry" ] ; then |
2126 | _err "Error, can not get domain token $d" | |
2127 | _clearup | |
b0070f03 | 2128 | _on_issue_err |
19539575 | 2129 | return 1 |
2130 | fi | |
22ea4004 | 2131 | token="$(printf "%s\n" "$entry" | _egrep_o '"token":"[^"]*' | cut -d : -f 2 | tr -d '"')" |
4c3b3608 | 2132 | _debug token $token |
2133 | ||
22ea4004 | 2134 | uri="$(printf "%s\n" "$entry" | _egrep_o '"uri":"[^"]*'| cut -d : -f 2,3 | tr -d '"' )" |
4c3b3608 | 2135 | _debug uri $uri |
2136 | ||
2137 | keyauthorization="$token.$thumbprint" | |
2138 | _debug keyauthorization "$keyauthorization" | |
2139 | ||
d35bf517 | 2140 | |
2141 | if printf "$response" | grep '"status":"valid"' >/dev/null 2>&1 ; then | |
2142 | _info "$d is already verified, skip." | |
2143 | keyauthorization=$STATE_VERIFIED | |
2144 | _debug keyauthorization "$keyauthorization" | |
ec603bee | 2145 | fi |
2146 | ||
d35bf517 | 2147 | |
a63b05a9 | 2148 | dvlist="$d$sep$keyauthorization$sep$uri$sep$vtype$sep$_currentRoot" |
4c3b3608 | 2149 | _debug dvlist "$dvlist" |
2150 | ||
2151 | vlist="$vlist$dvlist," | |
2152 | ||
2153 | done | |
2154 | ||
2155 | #add entry | |
2156 | dnsadded="" | |
2157 | ventries=$(echo "$vlist" | tr ',' ' ' ) | |
2158 | for ventry in $ventries | |
2159 | do | |
2160 | d=$(echo $ventry | cut -d $sep -f 1) | |
2161 | keyauthorization=$(echo $ventry | cut -d $sep -f 2) | |
a63b05a9 | 2162 | vtype=$(echo $ventry | cut -d $sep -f 4) |
2163 | _currentRoot=$(echo $ventry | cut -d $sep -f 5) | |
ec603bee | 2164 | |
bd5e57d8 | 2165 | if [ "$keyauthorization" = "$STATE_VERIFIED" ] ; then |
ec603bee | 2166 | _info "$d is already verified, skip $vtype." |
2167 | continue | |
2168 | fi | |
2169 | ||
8663fb7e | 2170 | if [ "$vtype" = "$VTYPE_DNS" ] ; then |
4c3b3608 | 2171 | dnsadded='0' |
2172 | txtdomain="_acme-challenge.$d" | |
2173 | _debug txtdomain "$txtdomain" | |
22ea4004 | 2174 | txt="$(printf "%s" "$keyauthorization" | _digest "sha256" | _urlencode)" |
4c3b3608 | 2175 | _debug txt "$txt" |
2176 | #dns | |
2177 | #1. check use api | |
2178 | d_api="" | |
8663fb7e | 2179 | if [ -f "$LE_WORKING_DIR/$d/$_currentRoot" ] ; then |
a63b05a9 | 2180 | d_api="$LE_WORKING_DIR/$d/$_currentRoot" |
8663fb7e | 2181 | elif [ -f "$LE_WORKING_DIR/$d/$_currentRoot.sh" ] ; then |
a63b05a9 | 2182 | d_api="$LE_WORKING_DIR/$d/$_currentRoot.sh" |
8663fb7e | 2183 | elif [ -f "$LE_WORKING_DIR/$_currentRoot" ] ; then |
a63b05a9 | 2184 | d_api="$LE_WORKING_DIR/$_currentRoot" |
8663fb7e | 2185 | elif [ -f "$LE_WORKING_DIR/$_currentRoot.sh" ] ; then |
a63b05a9 | 2186 | d_api="$LE_WORKING_DIR/$_currentRoot.sh" |
8663fb7e | 2187 | elif [ -f "$LE_WORKING_DIR/dnsapi/$_currentRoot" ] ; then |
a63b05a9 | 2188 | d_api="$LE_WORKING_DIR/dnsapi/$_currentRoot" |
8663fb7e | 2189 | elif [ -f "$LE_WORKING_DIR/dnsapi/$_currentRoot.sh" ] ; then |
a63b05a9 | 2190 | d_api="$LE_WORKING_DIR/dnsapi/$_currentRoot.sh" |
4c3b3608 | 2191 | fi |
2192 | _debug d_api "$d_api" | |
2193 | ||
8663fb7e | 2194 | if [ "$d_api" ] ; then |
4c3b3608 | 2195 | _info "Found domain api file: $d_api" |
2196 | else | |
2197 | _err "Add the following TXT record:" | |
cbcd7e0f | 2198 | _err "Domain: '$(__green $txtdomain)'" |
2199 | _err "TXT value: '$(__green $txt)'" | |
4c3b3608 | 2200 | _err "Please be aware that you prepend _acme-challenge. before your domain" |
2201 | _err "so the resulting subdomain will be: $txtdomain" | |
2202 | continue | |
2203 | fi | |
4c3b3608 | 2204 | |
73b8b120 | 2205 | ( |
8663fb7e | 2206 | if ! . $d_api ; then |
73b8b120 | 2207 | _err "Load file $d_api error. Please check your api file and try again." |
2208 | return 1 | |
2209 | fi | |
2210 | ||
158f22f7 | 2211 | addcommand="${_currentRoot}_add" |
d53289d7 | 2212 | if ! _exists $addcommand ; then |
73b8b120 | 2213 | _err "It seems that your api file is not correct, it must have a function named: $addcommand" |
2214 | return 1 | |
2215 | fi | |
2216 | ||
2217 | if ! $addcommand $txtdomain $txt ; then | |
2218 | _err "Error add txt for domain:$txtdomain" | |
2219 | return 1 | |
2220 | fi | |
2221 | ) | |
4c3b3608 | 2222 | |
8663fb7e | 2223 | if [ "$?" != "0" ] ; then |
5ef501c5 | 2224 | _clearup |
b0070f03 | 2225 | _on_issue_err |
4c3b3608 | 2226 | return 1 |
2227 | fi | |
2228 | dnsadded='1' | |
2229 | fi | |
2230 | done | |
2231 | ||
8663fb7e | 2232 | if [ "$dnsadded" = '0' ] ; then |
4d2f38b0 | 2233 | _savedomainconf "Le_Vlist" "$vlist" |
4c3b3608 | 2234 | _debug "Dns record not added yet, so, save to $DOMAIN_CONF and exit." |
2235 | _err "Please add the TXT records to the domains, and retry again." | |
5ef501c5 | 2236 | _clearup |
b0070f03 | 2237 | _on_issue_err |
4c3b3608 | 2238 | return 1 |
2239 | fi | |
2240 | ||
2241 | fi | |
2242 | ||
8663fb7e | 2243 | if [ "$dnsadded" = '1' ] ; then |
0e38c60d | 2244 | if [ -z "$Le_DNSSleep" ] ; then |
4a4dacb5 | 2245 | Le_DNSSleep=$DEFAULT_DNS_SLEEP |
0e38c60d | 2246 | else |
2247 | _savedomainconf "Le_DNSSleep" "$Le_DNSSleep" | |
2248 | fi | |
2249 | ||
5fbc47eb | 2250 | _info "Sleep $(__green $Le_DNSSleep) seconds for the txt records to take effect" |
fdcb6b72 | 2251 | _sleep $Le_DNSSleep |
4c3b3608 | 2252 | fi |
2253 | ||
2254 | _debug "ok, let's start to verify" | |
a63b05a9 | 2255 | |
0463b5d6 | 2256 | _ncIndex=1 |
4c3b3608 | 2257 | ventries=$(echo "$vlist" | tr ',' ' ' ) |
2258 | for ventry in $ventries | |
2259 | do | |
2260 | d=$(echo $ventry | cut -d $sep -f 1) | |
2261 | keyauthorization=$(echo $ventry | cut -d $sep -f 2) | |
2262 | uri=$(echo $ventry | cut -d $sep -f 3) | |
a63b05a9 | 2263 | vtype=$(echo $ventry | cut -d $sep -f 4) |
2264 | _currentRoot=$(echo $ventry | cut -d $sep -f 5) | |
ec603bee | 2265 | |
bd5e57d8 | 2266 | if [ "$keyauthorization" = "$STATE_VERIFIED" ] ; then |
ec603bee | 2267 | _info "$d is already verified, skip $vtype." |
2268 | continue | |
2269 | fi | |
2270 | ||
4c3b3608 | 2271 | _info "Verifying:$d" |
2272 | _debug "d" "$d" | |
2273 | _debug "keyauthorization" "$keyauthorization" | |
2274 | _debug "uri" "$uri" | |
2275 | removelevel="" | |
e22bcf7c | 2276 | token="$(printf "%s" "$keyauthorization" | cut -d '.' -f 1)" |
a63b05a9 | 2277 | |
2278 | _debug "_currentRoot" "$_currentRoot" | |
2279 | ||
2280 | ||
8663fb7e | 2281 | if [ "$vtype" = "$VTYPE_HTTP" ] ; then |
3f4513b3 | 2282 | if [ "$_currentRoot" = "$NO_VALUE" ] ; then |
4c3b3608 | 2283 | _info "Standalone mode server" |
0463b5d6 | 2284 | _ncaddr="$(_getfield "$Le_LocalAddress" "$_ncIndex" )" |
2285 | _ncIndex="$(_math $_ncIndex + 1)" | |
2286 | _startserver "$keyauthorization" "$_ncaddr" & | |
8663fb7e | 2287 | if [ "$?" != "0" ] ; then |
5ef501c5 | 2288 | _clearup |
b0070f03 | 2289 | _on_issue_err |
6fc1447f | 2290 | return 1 |
2291 | fi | |
4c3b3608 | 2292 | serverproc="$!" |
2293 | sleep 2 | |
2294 | _debug serverproc $serverproc | |
6fc1447f | 2295 | |
4c3b3608 | 2296 | else |
8663fb7e | 2297 | if [ "$_currentRoot" = "apache" ] ; then |
6f930641 | 2298 | wellknown_path="$ACME_DIR" |
2299 | else | |
a63b05a9 | 2300 | wellknown_path="$_currentRoot/.well-known/acme-challenge" |
8663fb7e | 2301 | if [ ! -d "$_currentRoot/.well-known" ] ; then |
6f930641 | 2302 | removelevel='1' |
8663fb7e | 2303 | elif [ ! -d "$_currentRoot/.well-known/acme-challenge" ] ; then |
6f930641 | 2304 | removelevel='2' |
2305 | else | |
2306 | removelevel='3' | |
2307 | fi | |
4c3b3608 | 2308 | fi |
6f930641 | 2309 | |
4c3b3608 | 2310 | _debug wellknown_path "$wellknown_path" |
6f930641 | 2311 | |
4c3b3608 | 2312 | _debug "writing token:$token to $wellknown_path/$token" |
2313 | ||
2314 | mkdir -p "$wellknown_path" | |
7939b419 | 2315 | printf "%s" "$keyauthorization" > "$wellknown_path/$token" |
8663fb7e | 2316 | if [ ! "$usingApache" ] ; then |
32fdc196 TB |
2317 | if webroot_owner=$(_stat $_currentRoot) ; then |
2318 | _debug "Changing owner/group of .well-known to $webroot_owner" | |
2319 | chown -R $webroot_owner "$_currentRoot/.well-known" | |
2320 | else | |
2321 | _debug "not chaning owner/group of webroot"; | |
2322 | fi | |
df886ffa | 2323 | fi |
4c3b3608 | 2324 | |
2325 | fi | |
e22bcf7c | 2326 | |
2327 | elif [ "$vtype" = "$VTYPE_TLS" ] ; then | |
2328 | #create A | |
2329 | #_hash_A="$(printf "%s" $token | _digest "sha256" "hex" )" | |
2330 | #_debug2 _hash_A "$_hash_A" | |
2331 | #_x="$(echo $_hash_A | cut -c 1-32)" | |
2332 | #_debug2 _x "$_x" | |
2333 | #_y="$(echo $_hash_A | cut -c 33-64)" | |
2334 | #_debug2 _y "$_y" | |
2335 | #_SAN_A="$_x.$_y.token.acme.invalid" | |
2336 | #_debug2 _SAN_A "$_SAN_A" | |
2337 | ||
2338 | #create B | |
2339 | _hash_B="$(printf "%s" $keyauthorization | _digest "sha256" "hex" )" | |
2340 | _debug2 _hash_B "$_hash_B" | |
2341 | _x="$(echo $_hash_B | cut -c 1-32)" | |
2342 | _debug2 _x "$_x" | |
2343 | _y="$(echo $_hash_B | cut -c 33-64)" | |
2344 | _debug2 _y "$_y" | |
2345 | ||
2346 | #_SAN_B="$_x.$_y.ka.acme.invalid" | |
2347 | ||
2348 | _SAN_B="$_x.$_y.acme.invalid" | |
2349 | _debug2 _SAN_B "$_SAN_B" | |
2350 | ||
0463b5d6 | 2351 | _ncaddr="$(_getfield "$Le_LocalAddress" "$_ncIndex" )" |
2352 | _ncIndex="$(_math $_ncIndex + 1)" | |
2353 | if ! _starttlsserver "$_SAN_B" "$_SAN_A" "$Le_TLSPort" "$keyauthorization" "$_ncaddr"; then | |
e22bcf7c | 2354 | _err "Start tls server error." |
2355 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" | |
2356 | _clearup | |
b0070f03 | 2357 | _on_issue_err |
e22bcf7c | 2358 | return 1 |
2359 | fi | |
4c3b3608 | 2360 | fi |
2361 | ||
c4d8fd83 | 2362 | if ! _send_signed_request $uri "{\"resource\": \"challenge\", \"keyAuthorization\": \"$keyauthorization\"}" ; then |
2363 | _err "$d:Can not get challenge: $response" | |
2364 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" | |
2365 | _clearup | |
b0070f03 | 2366 | _on_issue_err |
c4d8fd83 | 2367 | return 1 |
2368 | fi | |
4c3b3608 | 2369 | |
8663fb7e | 2370 | if [ ! -z "$code" ] && [ ! "$code" = '202' ] ; then |
c60883ef | 2371 | _err "$d:Challenge error: $response" |
a63b05a9 | 2372 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" |
4c3b3608 | 2373 | _clearup |
b0070f03 | 2374 | _on_issue_err |
4c3b3608 | 2375 | return 1 |
2376 | fi | |
2377 | ||
6fc1447f | 2378 | waittimes=0 |
8663fb7e | 2379 | if [ -z "$MAX_RETRY_TIMES" ] ; then |
6fc1447f | 2380 | MAX_RETRY_TIMES=30 |
2381 | fi | |
2382 | ||
2ee5d873 | 2383 | while true ; do |
00a50605 | 2384 | waittimes=$(_math $waittimes + 1) |
8663fb7e | 2385 | if [ "$waittimes" -ge "$MAX_RETRY_TIMES" ] ; then |
6fc1447f | 2386 | _err "$d:Timeout" |
2387 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" | |
2388 | _clearup | |
b0070f03 | 2389 | _on_issue_err |
6fc1447f | 2390 | return 1 |
2391 | fi | |
2392 | ||
4c3b3608 | 2393 | _debug "sleep 5 secs to verify" |
2394 | sleep 5 | |
2395 | _debug "checking" | |
9aaf36cd | 2396 | response="$(_get $uri)" |
8663fb7e | 2397 | if [ "$?" != "0" ] ; then |
c60883ef | 2398 | _err "$d:Verify error:$response" |
a63b05a9 | 2399 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" |
4c3b3608 | 2400 | _clearup |
b0070f03 | 2401 | _on_issue_err |
4c3b3608 | 2402 | return 1 |
2403 | fi | |
9aaf36cd | 2404 | _debug2 original "$response" |
2405 | ||
2406 | response="$(echo "$response" | _normalizeJson )" | |
7012b91f | 2407 | _debug2 response "$response" |
4c3b3608 | 2408 | |
22ea4004 | 2409 | status=$(echo "$response" | _egrep_o '"status":"[^"]*' | cut -d : -f 2 | tr -d '"') |
8663fb7e | 2410 | if [ "$status" = "valid" ] ; then |
4c3b3608 | 2411 | _info "Success" |
2412 | _stopserver $serverproc | |
2413 | serverproc="" | |
a63b05a9 | 2414 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" |
4c3b3608 | 2415 | break; |
2416 | fi | |
2417 | ||
8663fb7e | 2418 | if [ "$status" = "invalid" ] ; then |
fdcb6b72 | 2419 | error="$(echo "$response" | _egrep_o '"error":\{[^\}]*\}')" |
b7ec6789 | 2420 | _debug2 error "$error" |
22ea4004 | 2421 | errordetail="$(echo $error | _egrep_o '"detail": *"[^"]*"' | cut -d '"' -f 4)" |
b7ec6789 | 2422 | _debug2 errordetail "$errordetail" |
2423 | if [ "$errordetail" ] ; then | |
2424 | _err "$d:Verify error:$errordetail" | |
2425 | else | |
2426 | _err "$d:Verify error:$error" | |
2427 | fi | |
dcf9cb58 | 2428 | if [ "$DEBUG" ] ; then |
2429 | if [ "$vtype" = "$VTYPE_HTTP" ] ; then | |
276b51d9 | 2430 | _debug "Debug: get token url." |
dcf9cb58 | 2431 | _get "http://$d/.well-known/acme-challenge/$token" |
2432 | fi | |
2433 | fi | |
a63b05a9 | 2434 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" |
4c3b3608 | 2435 | _clearup |
b0070f03 | 2436 | _on_issue_err |
4c3b3608 | 2437 | return 1; |
2438 | fi | |
2439 | ||
8663fb7e | 2440 | if [ "$status" = "pending" ] ; then |
4c3b3608 | 2441 | _info "Pending" |
2442 | else | |
2443 | _err "$d:Verify error:$response" | |
a63b05a9 | 2444 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" |
4c3b3608 | 2445 | _clearup |
b0070f03 | 2446 | _on_issue_err |
4c3b3608 | 2447 | return 1 |
2448 | fi | |
2449 | ||
2450 | done | |
2451 | ||
2452 | done | |
2453 | ||
2454 | _clearup | |
2455 | _info "Verify finished, start to sign." | |
fa8311dc | 2456 | der="$(_getfile "${CSR_PATH}" "${BEGIN_CSR}" "${END_CSR}" | tr -d "\r\n" | _urlencode)" |
c4d8fd83 | 2457 | |
2458 | if ! _send_signed_request "$API/acme/new-cert" "{\"resource\": \"new-cert\", \"csr\": \"$der\"}" "needbase64" ; then | |
2459 | _err "Sign failed." | |
b0070f03 | 2460 | _on_issue_err |
c4d8fd83 | 2461 | return 1 |
2462 | fi | |
4c3b3608 | 2463 | |
d404e92d | 2464 | _rcert="$response" |
ef858ef0 | 2465 | Le_LinkCert="$(grep -i '^Location.*$' $HTTP_HEADER | head -n 1 | tr -d "\r\n" | cut -d " " -f 2)" |
4d2f38b0 | 2466 | _savedomainconf "Le_LinkCert" "$Le_LinkCert" |
4c3b3608 | 2467 | |
8663fb7e | 2468 | if [ "$Le_LinkCert" ] ; then |
88fab7d6 | 2469 | echo "$BEGIN_CERT" > "$CERT_PATH" |
d404e92d | 2470 | |
2471 | if ! _get "$Le_LinkCert" | _base64 "multiline" >> "$CERT_PATH" ; then | |
2472 | _debug "Get cert failed. Let's try last response." | |
2473 | printf -- "%s" "$_rcert" | _dbase64 "multiline" | _base64 "multiline" >> "$CERT_PATH" | |
2474 | fi | |
2475 | ||
88fab7d6 | 2476 | echo "$END_CERT" >> "$CERT_PATH" |
43822d37 | 2477 | _info "$(__green "Cert success.")" |
4c3b3608 | 2478 | cat "$CERT_PATH" |
2479 | ||
66f08eb2 | 2480 | _info "Your cert is in $( __green " $CERT_PATH ")" |
5980ebc7 | 2481 | |
2482 | if [ -f "$CERT_KEY_PATH" ] ; then | |
2483 | _info "Your cert key is in $( __green " $CERT_KEY_PATH ")" | |
2484 | fi | |
2485 | ||
caf1fc10 | 2486 | cp "$CERT_PATH" "$CERT_FULLCHAIN_PATH" |
281aa349 | 2487 | |
8663fb7e | 2488 | if [ ! "$USER_PATH" ] || [ ! "$IN_CRON" ] ; then |
281aa349 | 2489 | USER_PATH="$PATH" |
2490 | _saveaccountconf "USER_PATH" "$USER_PATH" | |
2491 | fi | |
4c3b3608 | 2492 | fi |
2493 | ||
2494 | ||
8663fb7e | 2495 | if [ -z "$Le_LinkCert" ] ; then |
eae29099 | 2496 | response="$(echo $response | _dbase64 "multiline" | _normalizeJson )" |
22ea4004 | 2497 | _err "Sign failed: $(echo "$response" | _egrep_o '"detail":"[^"]*"')" |
b0070f03 | 2498 | _on_issue_err |
4c3b3608 | 2499 | return 1 |
2500 | fi | |
2501 | ||
4d2f38b0 | 2502 | _cleardomainconf "Le_Vlist" |
4c3b3608 | 2503 | |
ef858ef0 | 2504 | Le_LinkIssuer=$(grep -i '^Link' $HTTP_HEADER | head -n 1 | cut -d " " -f 2| cut -d ';' -f 1 | tr -d '<>' ) |
fac1e367 | 2505 | if ! _contains "$Le_LinkIssuer" ":" ; then |
2506 | Le_LinkIssuer="$API$Le_LinkIssuer" | |
2507 | fi | |
2508 | ||
4d2f38b0 | 2509 | _savedomainconf "Le_LinkIssuer" "$Le_LinkIssuer" |
4c3b3608 | 2510 | |
8663fb7e | 2511 | if [ "$Le_LinkIssuer" ] ; then |
88fab7d6 | 2512 | echo "$BEGIN_CERT" > "$CA_CERT_PATH" |
c60883ef | 2513 | _get "$Le_LinkIssuer" | _base64 "multiline" >> "$CA_CERT_PATH" |
88fab7d6 | 2514 | echo "$END_CERT" >> "$CA_CERT_PATH" |
66f08eb2 | 2515 | _info "The intermediate CA cert is in $( __green " $CA_CERT_PATH ")" |
caf1fc10 | 2516 | cat "$CA_CERT_PATH" >> "$CERT_FULLCHAIN_PATH" |
66f08eb2 | 2517 | _info "And the full chain certs is there: $( __green " $CERT_FULLCHAIN_PATH ")" |
4c3b3608 | 2518 | fi |
2519 | ||
3aae1ae3 | 2520 | Le_CertCreateTime=$(_time) |
4d2f38b0 | 2521 | _savedomainconf "Le_CertCreateTime" "$Le_CertCreateTime" |
4c3b3608 | 2522 | |
2523 | Le_CertCreateTimeStr=$(date -u ) | |
4d2f38b0 | 2524 | _savedomainconf "Le_CertCreateTimeStr" "$Le_CertCreateTimeStr" |
4c3b3608 | 2525 | |
523c7682 | 2526 | if [ -z "$Le_RenewalDays" ] || [ "$Le_RenewalDays" -lt "0" ] || [ "$Le_RenewalDays" -gt "$MAX_RENEW" ] ; then |
2527 | Le_RenewalDays=$MAX_RENEW | |
054cb72e | 2528 | else |
2529 | _savedomainconf "Le_RenewalDays" "$Le_RenewalDays" | |
13d7cae9 | 2530 | fi |
2531 | ||
78009539 PS |
2532 | if [ "$CA_BUNDLE" ] ; then |
2533 | _saveaccountconf CA_BUNDLE "$CA_BUNDLE" | |
2534 | else | |
2535 | _clearaccountconf "CA_BUNDLE" | |
2536 | fi | |
2537 | ||
fac1e367 | 2538 | if [ "$HTTPS_INSECURE" ] ; then |
2539 | _saveaccountconf HTTPS_INSECURE "$HTTPS_INSECURE" | |
2540 | else | |
2541 | _clearaccountconf "HTTPS_INSECURE" | |
13d7cae9 | 2542 | fi |
00a50605 | 2543 | |
50827188 | 2544 | if [ "$Le_Listen_V4" ] ; then |
2545 | _savedomainconf "Le_Listen_V4" "$Le_Listen_V4" | |
2546 | _cleardomainconf Le_Listen_V6 | |
2547 | elif [ "$Le_Listen_V6" ] ; then | |
2548 | _savedomainconf "Le_Listen_V6" "$Le_Listen_V6" | |
2549 | _cleardomainconf Le_Listen_V4 | |
2550 | fi | |
2551 | ||
00a50605 | 2552 | Le_NextRenewTime=$(_math $Le_CertCreateTime + $Le_RenewalDays \* 24 \* 60 \* 60) |
028e1747 | 2553 | |
4c3b3608 | 2554 | |
2555 | Le_NextRenewTimeStr=$( _time2str $Le_NextRenewTime ) | |
4d2f38b0 | 2556 | _savedomainconf "Le_NextRenewTimeStr" "$Le_NextRenewTimeStr" |
028e1747 | 2557 | |
2558 | Le_NextRenewTime=$(_math $Le_NextRenewTime - 86400) | |
2559 | _savedomainconf "Le_NextRenewTime" "$Le_NextRenewTime" | |
f6dcd989 | 2560 | |
028e1747 | 2561 | |
b0070f03 | 2562 | _on_issue_success |
4c3b3608 | 2563 | |
4c0d3f1b | 2564 | if [ "$Le_RealCertPath$Le_RealKeyPath$Le_RealCACertPath$Le_ReloadCmd$Le_RealFullChainPath" ] ; then |
43822d37 | 2565 | _installcert |
01f54558 | 2566 | fi |
4c0d3f1b | 2567 | |
4c3b3608 | 2568 | } |
2569 | ||
43822d37 | 2570 | #domain [isEcc] |
4c3b3608 | 2571 | renew() { |
2572 | Le_Domain="$1" | |
8663fb7e | 2573 | if [ -z "$Le_Domain" ] ; then |
43822d37 | 2574 | _usage "Usage: $PROJECT_ENTRY --renew -d domain.com [--ecc]" |
4c3b3608 | 2575 | return 1 |
2576 | fi | |
2577 | ||
43822d37 | 2578 | _isEcc="$2" |
2579 | ||
2580 | _initpath $Le_Domain "$_isEcc" | |
2581 | ||
e2053b22 | 2582 | _info "$(__green "Renew: '$Le_Domain'")" |
8663fb7e | 2583 | if [ ! -f "$DOMAIN_CONF" ] ; then |
43822d37 | 2584 | _info "'$Le_Domain' is not a issued domain, skip." |
4c3b3608 | 2585 | return 0; |
2586 | fi | |
2587 | ||
1e6b68f5 | 2588 | if [ "$Le_RenewalDays" ] ; then |
2589 | _savedomainconf Le_RenewalDays "$Le_RenewalDays" | |
2590 | fi | |
2591 | ||
8663fb7e | 2592 | . "$DOMAIN_CONF" |
5c48e139 | 2593 | |
2594 | if [ "$Le_API" ] ; then | |
2595 | API="$Le_API" | |
2596 | fi | |
2597 | ||
3aae1ae3 | 2598 | if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(_time)" -lt "$Le_NextRenewTime" ] ; then |
e2053b22 | 2599 | _info "Skip, Next renewal time is: $(__green "$Le_NextRenewTimeStr")" |
2600 | _info "Add '$(__red '--force')' to force to renew." | |
cc179731 | 2601 | return $RENEW_SKIP |
4c3b3608 | 2602 | fi |
2603 | ||
2604 | IS_RENEW="1" | |
0463b5d6 | 2605 | 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 | 2606 | res=$? |
4c3b3608 | 2607 | IS_RENEW="" |
2608 | ||
2609 | return $res | |
2610 | } | |
2611 | ||
cc179731 | 2612 | #renewAll [stopRenewOnError] |
4c3b3608 | 2613 | renewAll() { |
2614 | _initpath | |
cc179731 | 2615 | _stopRenewOnError="$1" |
2616 | _debug "_stopRenewOnError" "$_stopRenewOnError" | |
2617 | _ret="0" | |
43822d37 | 2618 | |
b2817897 | 2619 | for d in $(ls -F ${CERT_HOME}/ | grep [^.].*[.].*/$ ) ; do |
4c3b3608 | 2620 | d=$(echo $d | cut -d '/' -f 1) |
43822d37 | 2621 | ( |
2622 | if _endswith $d "$ECC_SUFFIX" ; then | |
2623 | _isEcc=$(echo $d | cut -d "$ECC_SEP" -f 2) | |
2624 | d=$(echo $d | cut -d "$ECC_SEP" -f 1) | |
2625 | fi | |
2626 | renew "$d" "$_isEcc" | |
4d2f38b0 | 2627 | ) |
cc179731 | 2628 | rc="$?" |
2629 | _debug "Return code: $rc" | |
2630 | if [ "$rc" != "0" ] ; then | |
2631 | if [ "$rc" = "$RENEW_SKIP" ] ; then | |
2632 | _info "Skipped $d" | |
2633 | elif [ "$_stopRenewOnError" ] ; then | |
2634 | _err "Error renew $d, stop now." | |
2635 | return $rc | |
2636 | else | |
2637 | _ret="$rc" | |
2638 | _err "Error renew $d, Go ahead to next one." | |
2639 | fi | |
2640 | fi | |
4c3b3608 | 2641 | done |
cc179731 | 2642 | return $_ret |
4c3b3608 | 2643 | } |
2644 | ||
dcf4f8f6 | 2645 | |
10afcaca | 2646 | #csr webroot |
2647 | signcsr(){ | |
2648 | _csrfile="$1" | |
2649 | _csrW="$2" | |
2650 | if [ -z "$_csrfile" ] || [ -z "$_csrW" ]; then | |
2651 | _usage "Usage: $PROJECT_ENTRY --signcsr --csr mycsr.csr -w /path/to/webroot/a.com/ " | |
2652 | return 1 | |
2653 | fi | |
2654 | ||
2655 | _initpath | |
2656 | ||
2657 | _csrsubj=$(_readSubjectFromCSR "$_csrfile") | |
ad752b31 | 2658 | if [ "$?" != "0" ] ; then |
10afcaca | 2659 | _err "Can not read subject from csr: $_csrfile" |
2660 | return 1 | |
2661 | fi | |
ad752b31 | 2662 | _debug _csrsubj "$_csrsubj" |
10afcaca | 2663 | |
2664 | _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile") | |
2665 | if [ "$?" != "0" ] ; then | |
2666 | _err "Can not read domain list from csr: $_csrfile" | |
2667 | return 1 | |
2668 | fi | |
2669 | _debug "_csrdomainlist" "$_csrdomainlist" | |
2670 | ||
ad752b31 | 2671 | |
2672 | if [ -z "$_csrsubj" ] ; then | |
2673 | _csrsubj="$(_getfield "$_csrdomainlist" 1)" | |
2674 | _debug _csrsubj "$_csrsubj" | |
2675 | _csrdomainlist="$(echo "$_csrdomainlist" | cut -d , -f 2-)" | |
2676 | _debug "_csrdomainlist" "$_csrdomainlist" | |
2677 | fi | |
2678 | ||
2679 | if [ -z "$_csrsubj" ] ; then | |
2680 | _err "Can not read subject from csr: $_csrfile" | |
2681 | return 1 | |
2682 | fi | |
2683 | ||
10afcaca | 2684 | _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile") |
2685 | if [ "$?" != "0" ] || [ -z "$_csrkeylength" ] ; then | |
2686 | _err "Can not read key length from csr: $_csrfile" | |
2687 | return 1 | |
2688 | fi | |
2689 | ||
2690 | _initpath "$_csrsubj" "$_csrkeylength" | |
2691 | mkdir -p "$DOMAIN_PATH" | |
2692 | ||
2693 | _info "Copy csr to: $CSR_PATH" | |
2694 | cp "$_csrfile" "$CSR_PATH" | |
2695 | ||
2696 | issue "$_csrW" "$_csrsubj" "$_csrdomainlist" "$_csrkeylength" | |
2697 | ||
2698 | } | |
2699 | ||
2700 | showcsr() { | |
2701 | _csrfile="$1" | |
2702 | _csrd="$2" | |
2703 | if [ -z "$_csrfile" ] && [ -z "$_csrd" ]; then | |
2704 | _usage "Usage: $PROJECT_ENTRY --showcsr --csr mycsr.csr" | |
2705 | return 1 | |
2706 | fi | |
2707 | ||
2708 | _initpath | |
2709 | ||
2710 | _csrsubj=$(_readSubjectFromCSR "$_csrfile") | |
2711 | if [ "$?" != "0" ] || [ -z "$_csrsubj" ] ; then | |
2712 | _err "Can not read subject from csr: $_csrfile" | |
2713 | return 1 | |
2714 | fi | |
2715 | ||
2716 | _info "Subject=$_csrsubj" | |
2717 | ||
2718 | _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile") | |
2719 | if [ "$?" != "0" ] ; then | |
2720 | _err "Can not read domain list from csr: $_csrfile" | |
2721 | return 1 | |
2722 | fi | |
2723 | _debug "_csrdomainlist" "$_csrdomainlist" | |
2724 | ||
2725 | _info "SubjectAltNames=$_csrdomainlist" | |
2726 | ||
2727 | ||
2728 | _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile") | |
2729 | if [ "$?" != "0" ] || [ -z "$_csrkeylength" ] ; then | |
2730 | _err "Can not read key length from csr: $_csrfile" | |
2731 | return 1 | |
2732 | fi | |
2733 | _info "KeyLength=$_csrkeylength" | |
2734 | } | |
2735 | ||
6d7eda3e | 2736 | list() { |
22ea4004 | 2737 | _raw="$1" |
6d7eda3e | 2738 | _initpath |
dcf4f8f6 | 2739 | |
2740 | _sep="|" | |
2741 | if [ "$_raw" ] ; then | |
43822d37 | 2742 | printf "Main_Domain${_sep}KeyLength${_sep}SAN_Domains${_sep}Created${_sep}Renew\n" |
dcf4f8f6 | 2743 | for d in $(ls -F ${CERT_HOME}/ | grep [^.].*[.].*/$ ) ; do |
2744 | d=$(echo $d | cut -d '/' -f 1) | |
2745 | ( | |
43822d37 | 2746 | if _endswith $d "$ECC_SUFFIX" ; then |
2747 | _isEcc=$(echo $d | cut -d "$ECC_SEP" -f 2) | |
2748 | d=$(echo $d | cut -d "$ECC_SEP" -f 1) | |
2749 | fi | |
2750 | _initpath $d "$_isEcc" | |
dcf4f8f6 | 2751 | if [ -f "$DOMAIN_CONF" ] ; then |
2752 | . "$DOMAIN_CONF" | |
43822d37 | 2753 | printf "$Le_Domain${_sep}\"$Le_Keylength\"${_sep}$Le_Alt${_sep}$Le_CertCreateTimeStr${_sep}$Le_NextRenewTimeStr\n" |
dcf4f8f6 | 2754 | fi |
2755 | ) | |
2756 | done | |
2757 | else | |
22ea4004 | 2758 | if _exists column ; then |
2759 | list "raw" | column -t -s "$_sep" | |
2760 | else | |
43822d37 | 2761 | list "raw" | tr "$_sep" '\t' |
22ea4004 | 2762 | fi |
dcf4f8f6 | 2763 | fi |
6d7eda3e | 2764 | |
2765 | ||
2766 | } | |
2767 | ||
4c3b3608 | 2768 | installcert() { |
2769 | Le_Domain="$1" | |
8663fb7e | 2770 | if [ -z "$Le_Domain" ] ; then |
43822d37 | 2771 | _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 | 2772 | return 1 |
2773 | fi | |
2774 | ||
2775 | Le_RealCertPath="$2" | |
2776 | Le_RealKeyPath="$3" | |
2777 | Le_RealCACertPath="$4" | |
2778 | Le_ReloadCmd="$5" | |
a63b05a9 | 2779 | Le_RealFullChainPath="$6" |
43822d37 | 2780 | _isEcc="$7" |
2781 | ||
2782 | _initpath $Le_Domain "$_isEcc" | |
2783 | if [ ! -d "$DOMAIN_PATH" ] ; then | |
2784 | _err "Domain is not valid:'$Le_Domain'" | |
2785 | return 1 | |
2786 | fi | |
2787 | ||
2788 | _installcert | |
2789 | } | |
4c3b3608 | 2790 | |
43822d37 | 2791 | |
2792 | _installcert() { | |
4c3b3608 | 2793 | |
4d2f38b0 | 2794 | _savedomainconf "Le_RealCertPath" "$Le_RealCertPath" |
2795 | _savedomainconf "Le_RealCACertPath" "$Le_RealCACertPath" | |
2796 | _savedomainconf "Le_RealKeyPath" "$Le_RealKeyPath" | |
2797 | _savedomainconf "Le_ReloadCmd" "$Le_ReloadCmd" | |
2798 | _savedomainconf "Le_RealFullChainPath" "$Le_RealFullChainPath" | |
4c3b3608 | 2799 | |
3f4513b3 | 2800 | if [ "$Le_RealCertPath" = "$NO_VALUE" ] ; then |
4d2f38b0 | 2801 | Le_RealCertPath="" |
2802 | fi | |
3f4513b3 | 2803 | if [ "$Le_RealKeyPath" = "$NO_VALUE" ] ; then |
4d2f38b0 | 2804 | Le_RealKeyPath="" |
2805 | fi | |
3f4513b3 | 2806 | if [ "$Le_RealCACertPath" = "$NO_VALUE" ] ; then |
4d2f38b0 | 2807 | Le_RealCACertPath="" |
2808 | fi | |
3f4513b3 | 2809 | if [ "$Le_ReloadCmd" = "$NO_VALUE" ] ; then |
4d2f38b0 | 2810 | Le_ReloadCmd="" |
2811 | fi | |
3f4513b3 | 2812 | if [ "$Le_RealFullChainPath" = "$NO_VALUE" ] ; then |
4d2f38b0 | 2813 | Le_RealFullChainPath="" |
2814 | fi | |
2815 | ||
2816 | _installed="0" | |
8663fb7e | 2817 | if [ "$Le_RealCertPath" ] ; then |
4d2f38b0 | 2818 | _installed=1 |
2819 | _info "Installing cert to:$Le_RealCertPath" | |
43822d37 | 2820 | if [ -f "$Le_RealCertPath" ] && [ ! "$IS_RENEW" ] ; then |
ff3bce32 | 2821 | cp "$Le_RealCertPath" "$Le_RealCertPath".bak |
4c3b3608 | 2822 | fi |
2823 | cat "$CERT_PATH" > "$Le_RealCertPath" | |
2824 | fi | |
2825 | ||
8663fb7e | 2826 | if [ "$Le_RealCACertPath" ] ; then |
4d2f38b0 | 2827 | _installed=1 |
2828 | _info "Installing CA to:$Le_RealCACertPath" | |
8663fb7e | 2829 | if [ "$Le_RealCACertPath" = "$Le_RealCertPath" ] ; then |
4c3b3608 | 2830 | echo "" >> "$Le_RealCACertPath" |
2831 | cat "$CA_CERT_PATH" >> "$Le_RealCACertPath" | |
2832 | else | |
43822d37 | 2833 | if [ -f "$Le_RealCACertPath" ] && [ ! "$IS_RENEW" ] ; then |
ff3bce32 | 2834 | cp "$Le_RealCACertPath" "$Le_RealCACertPath".bak |
78552b18 | 2835 | fi |
4c3b3608 | 2836 | cat "$CA_CERT_PATH" > "$Le_RealCACertPath" |
2837 | fi | |
2838 | fi | |
2839 | ||
2840 | ||
8663fb7e | 2841 | if [ "$Le_RealKeyPath" ] ; then |
4d2f38b0 | 2842 | _installed=1 |
2843 | _info "Installing key to:$Le_RealKeyPath" | |
43822d37 | 2844 | if [ -f "$Le_RealKeyPath" ] && [ ! "$IS_RENEW" ] ; then |
ff3bce32 | 2845 | cp "$Le_RealKeyPath" "$Le_RealKeyPath".bak |
4c3b3608 | 2846 | fi |
2847 | cat "$CERT_KEY_PATH" > "$Le_RealKeyPath" | |
2848 | fi | |
a63b05a9 | 2849 | |
8663fb7e | 2850 | if [ "$Le_RealFullChainPath" ] ; then |
4d2f38b0 | 2851 | _installed=1 |
2852 | _info "Installing full chain to:$Le_RealFullChainPath" | |
43822d37 | 2853 | if [ -f "$Le_RealFullChainPath" ] && [ ! "$IS_RENEW" ] ; then |
ff3bce32 | 2854 | cp "$Le_RealFullChainPath" "$Le_RealFullChainPath".bak |
a63b05a9 | 2855 | fi |
2856 | cat "$CERT_FULLCHAIN_PATH" > "$Le_RealFullChainPath" | |
2857 | fi | |
4c3b3608 | 2858 | |
8663fb7e | 2859 | if [ "$Le_ReloadCmd" ] ; then |
4d2f38b0 | 2860 | _installed=1 |
4c3b3608 | 2861 | _info "Run Le_ReloadCmd: $Le_ReloadCmd" |
4d2f38b0 | 2862 | if (cd "$DOMAIN_PATH" && eval "$Le_ReloadCmd") ; then |
43822d37 | 2863 | _info "$(__green "Reload success")" |
4d2f38b0 | 2864 | else |
2865 | _err "Reload error for :$Le_Domain" | |
2866 | fi | |
2867 | fi | |
2868 | ||
4c3b3608 | 2869 | |
2870 | } | |
2871 | ||
2872 | installcronjob() { | |
2873 | _initpath | |
77546ea5 | 2874 | if ! _exists "crontab" ; then |
2875 | _err "crontab doesn't exist, so, we can not install cron jobs." | |
2876 | _err "All your certs will not be renewed automatically." | |
a7b7355d | 2877 | _err "You must add your own cron job to call '$PROJECT_ENTRY --cron' everyday." |
77546ea5 | 2878 | return 1 |
2879 | fi | |
2880 | ||
4c3b3608 | 2881 | _info "Installing cron job" |
a7b7355d | 2882 | if ! crontab -l | grep "$PROJECT_ENTRY --cron" ; then |
8663fb7e | 2883 | if [ -f "$LE_WORKING_DIR/$PROJECT_ENTRY" ] ; then |
a7b7355d | 2884 | lesh="\"$LE_WORKING_DIR\"/$PROJECT_ENTRY" |
4c3b3608 | 2885 | else |
a7b7355d | 2886 | _err "Can not install cronjob, $PROJECT_ENTRY not found." |
4c3b3608 | 2887 | return 1 |
2888 | fi | |
22ea4004 | 2889 | if _exists uname && uname -a | grep solaris >/dev/null ; then |
2890 | crontab -l | { cat; echo "0 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null"; } | crontab -- | |
2891 | else | |
2892 | crontab -l | { cat; echo "0 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null"; } | crontab - | |
2893 | fi | |
4c3b3608 | 2894 | fi |
8663fb7e | 2895 | if [ "$?" != "0" ] ; then |
4c3b3608 | 2896 | _err "Install cron job failed. You need to manually renew your certs." |
2897 | _err "Or you can add cronjob by yourself:" | |
a7b7355d | 2898 | _err "$lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null" |
4c3b3608 | 2899 | return 1 |
2900 | fi | |
2901 | } | |
2902 | ||
2903 | uninstallcronjob() { | |
37db5b81 | 2904 | if ! _exists "crontab" ; then |
2905 | return | |
2906 | fi | |
4c3b3608 | 2907 | _info "Removing cron job" |
a7b7355d | 2908 | cr="$(crontab -l | grep "$PROJECT_ENTRY --cron")" |
8663fb7e | 2909 | if [ "$cr" ] ; then |
22ea4004 | 2910 | if _exists uname && uname -a | grep solaris >/dev/null ; then |
2911 | crontab -l | sed "/$PROJECT_ENTRY --cron/d" | crontab -- | |
2912 | else | |
2913 | crontab -l | sed "/$PROJECT_ENTRY --cron/d" | crontab - | |
2914 | fi | |
a7b7355d | 2915 | LE_WORKING_DIR="$(echo "$cr" | cut -d ' ' -f 9 | tr -d '"')" |
4c3b3608 | 2916 | _info LE_WORKING_DIR "$LE_WORKING_DIR" |
2917 | fi | |
2918 | _initpath | |
a7b7355d | 2919 | |
4c3b3608 | 2920 | } |
2921 | ||
6cb415f5 | 2922 | revoke() { |
2923 | Le_Domain="$1" | |
8663fb7e | 2924 | if [ -z "$Le_Domain" ] ; then |
43822d37 | 2925 | _usage "Usage: $PROJECT_ENTRY --revoke -d domain.com" |
6cb415f5 | 2926 | return 1 |
2927 | fi | |
2928 | ||
43822d37 | 2929 | _isEcc="$2" |
2930 | ||
2931 | _initpath $Le_Domain "$_isEcc" | |
8663fb7e | 2932 | if [ ! -f "$DOMAIN_CONF" ] ; then |
6cb415f5 | 2933 | _err "$Le_Domain is not a issued domain, skip." |
2934 | return 1; | |
2935 | fi | |
2936 | ||
8663fb7e | 2937 | if [ ! -f "$CERT_PATH" ] ; then |
6cb415f5 | 2938 | _err "Cert for $Le_Domain $CERT_PATH is not found, skip." |
2939 | return 1 | |
2940 | fi | |
2941 | ||
2942 | cert="$(_getfile "${CERT_PATH}" "${BEGIN_CERT}" "${END_CERT}"| tr -d "\r\n" | _urlencode)" | |
2943 | ||
8663fb7e | 2944 | if [ -z "$cert" ] ; then |
6cb415f5 | 2945 | _err "Cert for $Le_Domain is empty found, skip." |
2946 | return 1 | |
2947 | fi | |
2948 | ||
2949 | data="{\"resource\": \"revoke-cert\", \"certificate\": \"$cert\"}" | |
2950 | uri="$API/acme/revoke-cert" | |
2951 | ||
2952 | _info "Try domain key first." | |
2953 | if _send_signed_request $uri "$data" "" "$CERT_KEY_PATH"; then | |
8663fb7e | 2954 | if [ -z "$response" ] ; then |
6cb415f5 | 2955 | _info "Revoke success." |
2956 | rm -f $CERT_PATH | |
2957 | return 0 | |
2958 | else | |
2959 | _err "Revoke error by domain key." | |
c9c31c04 | 2960 | _err "$response" |
6cb415f5 | 2961 | fi |
2962 | fi | |
2963 | ||
2964 | _info "Then try account key." | |
2965 | ||
2966 | if _send_signed_request $uri "$data" "" "$ACCOUNT_KEY_PATH" ; then | |
8663fb7e | 2967 | if [ -z "$response" ] ; then |
6cb415f5 | 2968 | _info "Revoke success." |
2969 | rm -f $CERT_PATH | |
2970 | return 0 | |
2971 | else | |
2972 | _err "Revoke error." | |
c9c31c04 | 2973 | _debug "$response" |
6cb415f5 | 2974 | fi |
2975 | fi | |
2976 | return 1 | |
2977 | } | |
4c3b3608 | 2978 | |
0c00e870 | 2979 | |
2980 | #domain vtype | |
2981 | _deactivate() { | |
2982 | _d_domain="$1" | |
2983 | _d_type="$2" | |
2984 | _initpath | |
2985 | ||
2986 | _d_i=0 | |
2987 | _d_max_retry=9 | |
2988 | while [ "$_d_i" -lt "$_d_max_retry" ] ; | |
2989 | do | |
0407c4e0 | 2990 | _info "Deactivate: $_d_domain" |
0c00e870 | 2991 | _d_i="$(_math $_d_i + 1)" |
2992 | if ! _send_signed_request "$API/acme/new-authz" "{\"resource\": \"new-authz\", \"identifier\": {\"type\": \"dns\", \"value\": \"$_d_domain\"}}" ; then | |
2993 | _err "Can not get domain token." | |
2994 | return 1 | |
2995 | fi | |
2996 | ||
48f02fb6 | 2997 | authzUri="$(echo "$responseHeaders" | grep "^Location:" | cut -d ' ' -f 2 | tr -d "\r\n")" |
3f4513b3 | 2998 | _debug "authzUri" "$authzUri" |
0c00e870 | 2999 | |
3000 | if [ ! -z "$code" ] && [ ! "$code" = '201' ] ; then | |
3001 | _err "new-authz error: $response" | |
3002 | return 1 | |
3003 | fi | |
3004 | ||
fdcb6b72 | 3005 | entry="$(printf "%s\n" "$response" | _egrep_o '[^\{]*"status":"valid","uri"[^\}]*')" |
0c00e870 | 3006 | _debug entry "$entry" |
3007 | ||
3008 | if [ -z "$entry" ] ; then | |
fb2029e7 | 3009 | _info "No more valid entry found." |
0c00e870 | 3010 | break |
3011 | fi | |
3012 | ||
3013 | _vtype="$(printf "%s\n" "$entry" | _egrep_o '"type": *"[^"]*"' | cut -d : -f 2 | tr -d '"')" | |
3014 | _debug _vtype $_vtype | |
3015 | _info "Found $_vtype" | |
3016 | ||
3017 | ||
3018 | uri="$(printf "%s\n" "$entry" | _egrep_o '"uri":"[^"]*'| cut -d : -f 2,3 | tr -d '"' )" | |
3019 | _debug uri $uri | |
3020 | ||
3021 | if [ "$_d_type" ] && [ "$_d_type" != "$_vtype" ] ; then | |
3022 | _info "Skip $_vtype" | |
3023 | continue | |
3024 | fi | |
3025 | ||
3026 | _info "Deactivate: $_vtype" | |
3027 | ||
3028 | if ! _send_signed_request "$authzUri" "{\"resource\": \"authz\", \"status\":\"deactivated\"}" ; then | |
3029 | _err "Can not deactivate $_vtype." | |
3030 | return 1 | |
3031 | fi | |
3032 | ||
fb2029e7 | 3033 | _info "Deactivate: $_vtype success." |
3034 | ||
0c00e870 | 3035 | done |
3036 | _debug "$_d_i" | |
3037 | if [ "$_d_i" -lt "$_d_max_retry" ] ; then | |
3038 | _info "Deactivated success!" | |
3039 | else | |
3040 | _err "Deactivate failed." | |
3041 | fi | |
3042 | ||
3043 | } | |
3044 | ||
3045 | deactivate() { | |
3f4513b3 | 3046 | _d_domain_list="$1" |
0c00e870 | 3047 | _d_type="$2" |
3048 | _initpath | |
3f4513b3 | 3049 | _debug _d_domain_list "$_d_domain_list" |
3050 | if [ -z "$(echo $_d_domain_list | cut -d , -f 1 )" ] ; then | |
3051 | _usage "Usage: $PROJECT_ENTRY --deactivate -d domain.com [-d domain.com]" | |
0c00e870 | 3052 | return 1 |
3053 | fi | |
3f4513b3 | 3054 | for _d_dm in $(echo "$_d_domain_list" | tr ',' ' ' ) ; |
3055 | do | |
3056 | if [ -z "$_d_dm" ] || [ "$_d_dm" = "$NO_VALUE" ] ; then | |
3057 | continue | |
3058 | fi | |
86c017ec | 3059 | if ! _deactivate "$_d_dm" $_d_type ; then |
3060 | return 1 | |
3061 | fi | |
3f4513b3 | 3062 | done |
0c00e870 | 3063 | } |
3064 | ||
4c3b3608 | 3065 | # Detect profile file if not specified as environment variable |
3066 | _detect_profile() { | |
a63b05a9 | 3067 | if [ -n "$PROFILE" -a -f "$PROFILE" ] ; then |
4c3b3608 | 3068 | echo "$PROFILE" |
3069 | return | |
3070 | fi | |
3071 | ||
4c3b3608 | 3072 | DETECTED_PROFILE='' |
4c3b3608 | 3073 | SHELLTYPE="$(basename "/$SHELL")" |
3074 | ||
8663fb7e | 3075 | if [ "$SHELLTYPE" = "bash" ] ; then |
3076 | if [ -f "$HOME/.bashrc" ] ; then | |
4c3b3608 | 3077 | DETECTED_PROFILE="$HOME/.bashrc" |
8663fb7e | 3078 | elif [ -f "$HOME/.bash_profile" ] ; then |
4c3b3608 | 3079 | DETECTED_PROFILE="$HOME/.bash_profile" |
3080 | fi | |
8663fb7e | 3081 | elif [ "$SHELLTYPE" = "zsh" ] ; then |
4c3b3608 | 3082 | DETECTED_PROFILE="$HOME/.zshrc" |
3083 | fi | |
3084 | ||
8663fb7e | 3085 | if [ -z "$DETECTED_PROFILE" ] ; then |
3086 | if [ -f "$HOME/.profile" ] ; then | |
4c3b3608 | 3087 | DETECTED_PROFILE="$HOME/.profile" |
8663fb7e | 3088 | elif [ -f "$HOME/.bashrc" ] ; then |
4c3b3608 | 3089 | DETECTED_PROFILE="$HOME/.bashrc" |
8663fb7e | 3090 | elif [ -f "$HOME/.bash_profile" ] ; then |
4c3b3608 | 3091 | DETECTED_PROFILE="$HOME/.bash_profile" |
8663fb7e | 3092 | elif [ -f "$HOME/.zshrc" ] ; then |
4c3b3608 | 3093 | DETECTED_PROFILE="$HOME/.zshrc" |
3094 | fi | |
3095 | fi | |
3096 | ||
8663fb7e | 3097 | if [ ! -z "$DETECTED_PROFILE" ] ; then |
4c3b3608 | 3098 | echo "$DETECTED_PROFILE" |
3099 | fi | |
3100 | } | |
3101 | ||
3102 | _initconf() { | |
3103 | _initpath | |
8663fb7e | 3104 | if [ ! -f "$ACCOUNT_CONF_PATH" ] ; then |
d53289d7 | 3105 | echo "#ACCOUNT_CONF_PATH=xxxx |
3106 | ||
3107 | #Account configurations: | |
4c3b3608 | 3108 | #Here are the supported macros, uncomment them to make them take effect. |
d53289d7 | 3109 | |
4c3b3608 | 3110 | #ACCOUNT_EMAIL=aaa@aaa.com # the account email used to register account. |
5fd3f21b | 3111 | #ACCOUNT_KEY_PATH=\"/path/to/account.key\" |
b2817897 | 3112 | #CERT_HOME=\"/path/to/cert/home\" |
4c3b3608 | 3113 | |
d404e92d | 3114 | |
3115 | ||
d0871bda | 3116 | #LOG_FILE=\"$DEFAULT_LOG_FILE\" |
6b500036 | 3117 | #LOG_LEVEL=1 |
5ea6e9c9 | 3118 | |
251d1c5c | 3119 | #AUTO_UPGRADE=\"1\" |
89002ed2 | 3120 | |
4c3b3608 | 3121 | #STAGE=1 # Use the staging api |
3122 | #FORCE=1 # Force to issue cert | |
3123 | #DEBUG=1 # Debug mode | |
3124 | ||
166096dc | 3125 | |
8814a348 | 3126 | #USER_AGENT=\"$USER_AGENT\" |
281aa349 | 3127 | |
3128 | #USER_PATH="" | |
3129 | ||
4c3b3608 | 3130 | #dns api |
3131 | ####################### | |
3132 | #Cloudflare: | |
3133 | #api key | |
3d49985a | 3134 | #CF_Key=\"sdfsdfsdfljlbjkljlkjsdfoiwje\" |
4c3b3608 | 3135 | #account email |
3d49985a | 3136 | #CF_Email=\"xxxx@sss.com\" |
4c3b3608 | 3137 | |
3138 | ####################### | |
3139 | #Dnspod.cn: | |
3140 | #api key id | |
3d49985a | 3141 | #DP_Id=\"1234\" |
4c3b3608 | 3142 | #api key |
3d49985a | 3143 | #DP_Key=\"sADDsdasdgdsf\" |
4c3b3608 | 3144 | |
3145 | ####################### | |
3146 | #Cloudxns.com: | |
3d49985a | 3147 | #CX_Key=\"1234\" |
4c3b3608 | 3148 | # |
3d49985a | 3149 | #CX_Secret=\"sADDsdasdgdsf\" |
30de13b4 | 3150 | |
3151 | ####################### | |
3152 | #Godaddy.com: | |
3153 | #GD_Key=\"sdfdsgdgdfdasfds\" | |
3154 | # | |
3155 | #GD_Secret=\"sADDsdasdfsdfdssdgdsf\" | |
4c3b3608 | 3156 | |
3157 | " > $ACCOUNT_CONF_PATH | |
3158 | fi | |
3159 | } | |
3160 | ||
c8e9a31e | 3161 | # nocron |
c60883ef | 3162 | _precheck() { |
c8e9a31e | 3163 | _nocron="$1" |
3164 | ||
c60883ef | 3165 | if ! _exists "curl" && ! _exists "wget"; then |
3166 | _err "Please install curl or wget first, we need to access http resources." | |
4c3b3608 | 3167 | return 1 |
3168 | fi | |
3169 | ||
c8e9a31e | 3170 | if [ -z "$_nocron" ] ; then |
3171 | if ! _exists "crontab" ; then | |
3172 | _err "It is recommended to install crontab first. try to install 'cron, crontab, crontabs or vixie-cron'." | |
3173 | _err "We need to set cron job to renew the certs automatically." | |
3174 | _err "Otherwise, your certs will not be able to be renewed automatically." | |
3175 | if [ -z "$FORCE" ] ; then | |
3176 | _err "Please add '--force' and try install again to go without crontab." | |
3177 | _err "./$PROJECT_ENTRY --install --force" | |
3178 | return 1 | |
3179 | fi | |
77546ea5 | 3180 | fi |
4c3b3608 | 3181 | fi |
3182 | ||
c60883ef | 3183 | if ! _exists "openssl" ; then |
3184 | _err "Please install openssl first." | |
3185 | _err "We need openssl to generate keys." | |
4c3b3608 | 3186 | return 1 |
3187 | fi | |
3188 | ||
c60883ef | 3189 | if ! _exists "nc" ; then |
3190 | _err "It is recommended to install nc first, try to install 'nc' or 'netcat'." | |
3191 | _err "We use nc for standalone server if you use standalone mode." | |
3192 | _err "If you don't use standalone mode, just ignore this warning." | |
3193 | fi | |
3194 | ||
3195 | return 0 | |
3196 | } | |
3197 | ||
0a7c9364 | 3198 | _setShebang() { |
3199 | _file="$1" | |
3200 | _shebang="$2" | |
3201 | if [ -z "$_shebang" ] ; then | |
43822d37 | 3202 | _usage "Usage: file shebang" |
0a7c9364 | 3203 | return 1 |
3204 | fi | |
3205 | cp "$_file" "$_file.tmp" | |
3206 | echo "$_shebang" > "$_file" | |
3207 | sed -n 2,99999p "$_file.tmp" >> "$_file" | |
3208 | rm -f "$_file.tmp" | |
3209 | } | |
3210 | ||
94dc5f33 | 3211 | _installalias() { |
3212 | _initpath | |
3213 | ||
3214 | _envfile="$LE_WORKING_DIR/$PROJECT_ENTRY.env" | |
3215 | if [ "$_upgrading" ] && [ "$_upgrading" = "1" ] ; then | |
3216 | echo "$(cat $_envfile)" | sed "s|^LE_WORKING_DIR.*$||" > "$_envfile" | |
3217 | echo "$(cat $_envfile)" | sed "s|^alias le.*$||" > "$_envfile" | |
3218 | echo "$(cat $_envfile)" | sed "s|^alias le.sh.*$||" > "$_envfile" | |
3219 | fi | |
3220 | ||
1786a5e5 | 3221 | _setopt "$_envfile" "export LE_WORKING_DIR" "=" "\"$LE_WORKING_DIR\"" |
94dc5f33 | 3222 | _setopt "$_envfile" "alias $PROJECT_ENTRY" "=" "\"$LE_WORKING_DIR/$PROJECT_ENTRY\"" |
3223 | ||
3224 | _profile="$(_detect_profile)" | |
3225 | if [ "$_profile" ] ; then | |
3226 | _debug "Found profile: $_profile" | |
3227 | _setopt "$_profile" ". \"$_envfile\"" | |
3228 | _info "OK, Close and reopen your terminal to start using $PROJECT_NAME" | |
3229 | else | |
3230 | _info "No profile is found, you will need to go into $LE_WORKING_DIR to use $PROJECT_NAME" | |
3231 | fi | |
3232 | ||
3233 | ||
3234 | #for csh | |
3235 | _cshfile="$LE_WORKING_DIR/$PROJECT_ENTRY.csh" | |
94dc5f33 | 3236 | _csh_profile="$HOME/.cshrc" |
3237 | if [ -f "$_csh_profile" ] ; then | |
6626371d | 3238 | _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\"" |
3239 | _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY\"" | |
94dc5f33 | 3240 | _setopt "$_csh_profile" "source \"$_cshfile\"" |
3241 | fi | |
acafa585 | 3242 | |
3243 | #for tcsh | |
3244 | _tcsh_profile="$HOME/.tcshrc" | |
3245 | if [ -f "$_tcsh_profile" ] ; then | |
3246 | _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\"" | |
3247 | _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY\"" | |
3248 | _setopt "$_tcsh_profile" "source \"$_cshfile\"" | |
3249 | fi | |
94dc5f33 | 3250 | |
3251 | } | |
3252 | ||
c8e9a31e | 3253 | # nocron |
c60883ef | 3254 | install() { |
f3e4cea3 | 3255 | |
3256 | if [ -z "$LE_WORKING_DIR" ] ; then | |
3257 | LE_WORKING_DIR="$DEFAULT_INSTALL_HOME" | |
3258 | fi | |
3259 | ||
c8e9a31e | 3260 | _nocron="$1" |
c60883ef | 3261 | if ! _initpath ; then |
3262 | _err "Install failed." | |
4c3b3608 | 3263 | return 1 |
3264 | fi | |
52677b0a | 3265 | if [ "$_nocron" ] ; then |
3266 | _debug "Skip install cron job" | |
3267 | fi | |
3268 | ||
c8e9a31e | 3269 | if ! _precheck "$_nocron" ; then |
c60883ef | 3270 | _err "Pre-check failed, can not install." |
4c3b3608 | 3271 | return 1 |
3272 | fi | |
c60883ef | 3273 | |
6cc11ffb | 3274 | #convert from le |
8663fb7e | 3275 | if [ -d "$HOME/.le" ] ; then |
6cc11ffb | 3276 | for envfile in "le.env" "le.sh.env" |
3277 | do | |
8663fb7e | 3278 | if [ -f "$HOME/.le/$envfile" ] ; then |
6cc11ffb | 3279 | if grep "le.sh" "$HOME/.le/$envfile" >/dev/null ; then |
3280 | _upgrading="1" | |
3281 | _info "You are upgrading from le.sh" | |
3282 | _info "Renaming \"$HOME/.le\" to $LE_WORKING_DIR" | |
3283 | mv "$HOME/.le" "$LE_WORKING_DIR" | |
3284 | mv "$LE_WORKING_DIR/$envfile" "$LE_WORKING_DIR/$PROJECT_ENTRY.env" | |
3285 | break; | |
3286 | fi | |
3287 | fi | |
3288 | done | |
3289 | fi | |
3290 | ||
4c3b3608 | 3291 | _info "Installing to $LE_WORKING_DIR" |
635695ec | 3292 | |
4a0f23e2 | 3293 | if ! mkdir -p "$LE_WORKING_DIR" ; then |
90035252 | 3294 | _err "Can not create working dir: $LE_WORKING_DIR" |
4a0f23e2 | 3295 | return 1 |
3296 | fi | |
3297 | ||
762978f8 | 3298 | chmod 700 "$LE_WORKING_DIR" |
3299 | ||
a7b7355d | 3300 | cp $PROJECT_ENTRY "$LE_WORKING_DIR/" && chmod +x "$LE_WORKING_DIR/$PROJECT_ENTRY" |
4c3b3608 | 3301 | |
8663fb7e | 3302 | if [ "$?" != "0" ] ; then |
a7b7355d | 3303 | _err "Install failed, can not copy $PROJECT_ENTRY" |
4c3b3608 | 3304 | return 1 |
3305 | fi | |
3306 | ||
a7b7355d | 3307 | _info "Installed to $LE_WORKING_DIR/$PROJECT_ENTRY" |
4c3b3608 | 3308 | |
94dc5f33 | 3309 | _installalias |
4c3b3608 | 3310 | |
8663fb7e | 3311 | if [ -d "dnsapi" ] ; then |
6ed1c718 | 3312 | mkdir -p $LE_WORKING_DIR/dnsapi |
3313 | cp dnsapi/* $LE_WORKING_DIR/dnsapi/ | |
3314 | fi | |
d53289d7 | 3315 | |
8663fb7e | 3316 | if [ ! -f "$ACCOUNT_CONF_PATH" ] ; then |
4c3b3608 | 3317 | _initconf |
3318 | fi | |
6cc11ffb | 3319 | |
8663fb7e | 3320 | if [ "$_DEFAULT_ACCOUNT_CONF_PATH" != "$ACCOUNT_CONF_PATH" ] ; then |
635695ec | 3321 | _setopt "$_DEFAULT_ACCOUNT_CONF_PATH" "ACCOUNT_CONF_PATH" "=" "\"$ACCOUNT_CONF_PATH\"" |
6cc11ffb | 3322 | fi |
3323 | ||
8663fb7e | 3324 | if [ "$_DEFAULT_CERT_HOME" != "$CERT_HOME" ] ; then |
b2817897 | 3325 | _saveaccountconf "CERT_HOME" "$CERT_HOME" |
3326 | fi | |
3327 | ||
8663fb7e | 3328 | if [ "$_DEFAULT_ACCOUNT_KEY_PATH" != "$ACCOUNT_KEY_PATH" ] ; then |
b2817897 | 3329 | _saveaccountconf "ACCOUNT_KEY_PATH" "$ACCOUNT_KEY_PATH" |
3330 | fi | |
3331 | ||
c8e9a31e | 3332 | if [ -z "$_nocron" ] ; then |
3333 | installcronjob | |
3334 | fi | |
0a7c9364 | 3335 | |
641989fd | 3336 | if [ -z "$NO_DETECT_SH" ] ; then |
3337 | #Modify shebang | |
3338 | if _exists bash ; then | |
3339 | _info "Good, bash is installed, change the shebang to use bash as prefered." | |
3340 | _shebang='#!/usr/bin/env bash' | |
3341 | _setShebang "$LE_WORKING_DIR/$PROJECT_ENTRY" "$_shebang" | |
3342 | if [ -d "$LE_WORKING_DIR/dnsapi" ] ; then | |
3343 | for _apifile in $(ls "$LE_WORKING_DIR/dnsapi/"*.sh) ; do | |
3344 | _setShebang "$_apifile" "$_shebang" | |
3345 | done | |
3346 | fi | |
0a7c9364 | 3347 | fi |
3348 | fi | |
3349 | ||
4c3b3608 | 3350 | _info OK |
3351 | } | |
3352 | ||
52677b0a | 3353 | # nocron |
4c3b3608 | 3354 | uninstall() { |
52677b0a | 3355 | _nocron="$1" |
3356 | if [ -z "$_nocron" ] ; then | |
3357 | uninstallcronjob | |
3358 | fi | |
4c3b3608 | 3359 | _initpath |
3360 | ||
3361 | _profile="$(_detect_profile)" | |
8663fb7e | 3362 | if [ "$_profile" ] ; then |
7203a1c1 | 3363 | text="$(cat $_profile)" |
94dc5f33 | 3364 | echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.env\"$||" > "$_profile" |
4c3b3608 | 3365 | fi |
3366 | ||
94dc5f33 | 3367 | _csh_profile="$HOME/.cshrc" |
3368 | if [ -f "$_csh_profile" ] ; then | |
3369 | text="$(cat $_csh_profile)" | |
3370 | echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" > "$_csh_profile" | |
3371 | fi | |
3372 | ||
acafa585 | 3373 | _tcsh_profile="$HOME/.tcshrc" |
3374 | if [ -f "$_tcsh_profile" ] ; then | |
3375 | text="$(cat $_tcsh_profile)" | |
3376 | echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" > "$_tcsh_profile" | |
3377 | fi | |
3378 | ||
a7b7355d | 3379 | rm -f $LE_WORKING_DIR/$PROJECT_ENTRY |
4c3b3608 | 3380 | _info "The keys and certs are in $LE_WORKING_DIR, you can remove them by yourself." |
3381 | ||
3382 | } | |
3383 | ||
3384 | cron() { | |
281aa349 | 3385 | IN_CRON=1 |
89002ed2 | 3386 | _initpath |
6bf281f9 | 3387 | if [ "$AUTO_UPGRADE" = "1" ] ; then |
89002ed2 | 3388 | export LE_WORKING_DIR |
3389 | ( | |
89002ed2 | 3390 | if ! upgrade ; then |
3391 | _err "Cron:Upgrade failed!" | |
3392 | return 1 | |
3393 | fi | |
3394 | ) | |
3395 | . $LE_WORKING_DIR/$PROJECT_ENTRY >/dev/null | |
1ab63043 | 3396 | |
3397 | if [ -t 1 ] ; then | |
3398 | __INTERACTIVE="1" | |
3399 | fi | |
3400 | ||
89002ed2 | 3401 | _info "Auto upgraded to: $VER" |
3402 | fi | |
4c3b3608 | 3403 | renewAll |
cc179731 | 3404 | _ret="$?" |
281aa349 | 3405 | IN_CRON="" |
0ba95a3d | 3406 | exit $_ret |
4c3b3608 | 3407 | } |
3408 | ||
3409 | version() { | |
a63b05a9 | 3410 | echo "$PROJECT" |
3411 | echo "v$VER" | |
4c3b3608 | 3412 | } |
3413 | ||
3414 | showhelp() { | |
d0871bda | 3415 | _initpath |
4c3b3608 | 3416 | version |
a7b7355d | 3417 | echo "Usage: $PROJECT_ENTRY command ...[parameters].... |
a63b05a9 | 3418 | Commands: |
3419 | --help, -h Show this help message. | |
3420 | --version, -v Show version info. | |
a7b7355d | 3421 | --install Install $PROJECT_NAME to your system. |
3422 | --uninstall Uninstall $PROJECT_NAME, and uninstall the cron job. | |
10afcaca | 3423 | --upgrade Upgrade $PROJECT_NAME to the latest code from $PROJECT . |
a63b05a9 | 3424 | --issue Issue a cert. |
10afcaca | 3425 | --signcsr Issue a cert from an existing csr. |
a63b05a9 | 3426 | --installcert Install the issued cert to apache/nginx or any other server. |
3427 | --renew, -r Renew a cert. | |
10afcaca | 3428 | --renewAll Renew all the certs. |
a63b05a9 | 3429 | --revoke Revoke a cert. |
10afcaca | 3430 | --list List all the certs. |
3431 | --showcsr Show the content of a csr. | |
a63b05a9 | 3432 | --installcronjob Install the cron job to renew certs, you don't need to call this. The 'install' command can automatically install the cron job. |
3433 | --uninstallcronjob Uninstall the cron job. The 'uninstall' command can do this automatically. | |
3434 | --cron Run cron job to renew all the certs. | |
3435 | --toPkcs Export the certificate and key to a pfx file. | |
eb59817e | 3436 | --updateaccount Update account info. |
3437 | --registeraccount Register account key. | |
a63b05a9 | 3438 | --createAccountKey, -cak Create an account private key, professional use. |
3439 | --createDomainKey, -cdk Create an domain private key, professional use. | |
3440 | --createCSR, -ccsr Create CSR , professional use. | |
0c00e870 | 3441 | --deactivate Deactivate the domain authz, professional use. |
a63b05a9 | 3442 | |
3443 | Parameters: | |
3444 | --domain, -d domain.tld Specifies a domain, used to issue, renew or revoke etc. | |
3445 | --force, -f Used to force to install or force to renew a cert immediately. | |
3446 | --staging, --test Use staging server, just for test. | |
3447 | --debug Output debug info. | |
3448 | ||
3449 | --webroot, -w /path/to/webroot Specifies the web root folder for web root mode. | |
3450 | --standalone Use standalone mode. | |
e22bcf7c | 3451 | --tls Use standalone tls mode. |
a63b05a9 | 3452 | --apache Use apache mode. |
eccec5f6 | 3453 | --dns [dns_cf|dns_dp|dns_cx|/path/to/api/file] Use dns mode or dns api. |
4a4dacb5 | 3454 | --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 | 3455 | |
3456 | --keylength, -k [2048] Specifies the domain key length: 2048, 3072, 4096, 8192 or ec-256, ec-384. | |
3457 | --accountkeylength, -ak [2048] Specifies the account key length. | |
d0871bda | 3458 | --log [/path/to/logfile] Specifies the log file. The default is: \"$DEFAULT_LOG_FILE\" if you don't give a file path here. |
a73c5b33 | 3459 | --log-level 1|2 Specifies the log level, default is 1. |
a63b05a9 | 3460 | |
3461 | These parameters are to install the cert to nginx/apache or anyother server after issue/renew a cert: | |
3462 | ||
3463 | --certpath /path/to/real/cert/file After issue/renew, the cert will be copied to this path. | |
3464 | --keypath /path/to/real/key/file After issue/renew, the key will be copied to this path. | |
3465 | --capath /path/to/real/ca/file After issue/renew, the intermediate cert will be copied to this path. | |
3466 | --fullchainpath /path/to/fullchain/file After issue/renew, the fullchain cert will be copied to this path. | |
3467 | ||
3468 | --reloadcmd \"service nginx reload\" After issue/renew, it's used to reload the server. | |
3469 | ||
3470 | --accountconf Specifies a customized account config file. | |
635695ec | 3471 | --home Specifies the home dir for $PROJECT_NAME . |
39c8f79f | 3472 | --certhome Specifies the home dir to save all the certs, only valid for '--install' command. |
635695ec | 3473 | --useragent Specifies the user agent string. it will be saved for future use too. |
b5eb4b90 | 3474 | --accountemail Specifies the account email for registering, Only valid for the '--install' command. |
06625071 | 3475 | --accountkey Specifies the account key path, Only valid for the '--install' command. |
523c7682 | 3476 | --days Specifies the days to renew the cert when using '--issue' command. The max value is $MAX_RENEW days. |
39c8f79f | 3477 | --httpport Specifies the standalone listening port. Only valid if the server is behind a reverse proxy or load balancer. |
e22bcf7c | 3478 | --tlsport Specifies the standalone tls listening port. Only valid if the server is behind a reverse proxy or load balancer. |
6ae0f7f5 | 3479 | --local-address Specifies the standalone/tls server listening address, in case you have multiple ip addresses. |
dcf4f8f6 | 3480 | --listraw Only used for '--list' command, list the certs in raw format. |
c8e9a31e | 3481 | --stopRenewOnError, -se Only valid for '--renewall' command. Stop if one cert has error in renewal. |
13d7cae9 | 3482 | --insecure Do not check the server certificate, in some devices, the api server's certificate may not be trusted. |
78009539 | 3483 | --ca-bundle Specifices the path to the CA certificate bundle to verify api server's certificate. |
bc96082f | 3484 | --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 | 3485 | --ecc Specifies to use the ECC cert. Valid for '--installcert', '--renew', '--revoke', '--toPkcs' and '--createCSR' |
10afcaca | 3486 | --csr Specifies the input csr. |
b0070f03 | 3487 | --pre-hook Command to be run before obtaining any certificates. |
3488 | --post-hook Command to be run after attempting to obtain/renew certificates. No matter the obain/renew is success or failed. | |
3489 | --renew-hook Command to be run once for each successfully renewed certificate. | |
0c9546cc | 3490 | --ocsp-must-staple, --ocsp Generate ocsp must Staple extension. |
6bf281f9 | 3491 | --auto-upgrade [0|1] Valid for '--upgrade' command, indicating whether to upgrade automatically in future. |
6ae0f7f5 | 3492 | --listen-v4 Force standalone/tls server to listen at ipv4. |
3493 | --listen-v6 Force standalone/tls server to listen at ipv6. | |
4c3b3608 | 3494 | " |
3495 | } | |
3496 | ||
52677b0a | 3497 | # nocron |
4a0f23e2 | 3498 | _installOnline() { |
3499 | _info "Installing from online archive." | |
52677b0a | 3500 | _nocron="$1" |
8663fb7e | 3501 | if [ ! "$BRANCH" ] ; then |
4a0f23e2 | 3502 | BRANCH="master" |
3503 | fi | |
a8df88ab | 3504 | |
4a0f23e2 | 3505 | target="$PROJECT/archive/$BRANCH.tar.gz" |
3506 | _info "Downloading $target" | |
3507 | localname="$BRANCH.tar.gz" | |
3508 | if ! _get "$target" > $localname ; then | |
df9547ae | 3509 | _err "Download error." |
4a0f23e2 | 3510 | return 1 |
3511 | fi | |
0bbe6eef | 3512 | ( |
4a0f23e2 | 3513 | _info "Extracting $localname" |
3514 | tar xzf $localname | |
0bbe6eef | 3515 | |
6cc11ffb | 3516 | cd "$PROJECT_NAME-$BRANCH" |
a7b7355d | 3517 | chmod +x $PROJECT_ENTRY |
52677b0a | 3518 | if ./$PROJECT_ENTRY install "$_nocron" ; then |
4a0f23e2 | 3519 | _info "Install success!" |
3520 | fi | |
3521 | ||
3522 | cd .. | |
0bbe6eef | 3523 | |
6cc11ffb | 3524 | rm -rf "$PROJECT_NAME-$BRANCH" |
4a0f23e2 | 3525 | rm -f "$localname" |
0bbe6eef | 3526 | ) |
4a0f23e2 | 3527 | } |
3528 | ||
52677b0a | 3529 | upgrade() { |
3530 | if ( | |
267f283a | 3531 | _initpath |
3532 | export LE_WORKING_DIR | |
d0b748a4 | 3533 | cd "$LE_WORKING_DIR" |
52677b0a | 3534 | _installOnline "nocron" |
3535 | ) ; then | |
3536 | _info "Upgrade success!" | |
096d8992 | 3537 | exit 0 |
52677b0a | 3538 | else |
3539 | _err "Upgrade failed!" | |
096d8992 | 3540 | exit 1 |
52677b0a | 3541 | fi |
3542 | } | |
a63b05a9 | 3543 | |
5ea6e9c9 | 3544 | _processAccountConf() { |
3545 | if [ "$_useragent" ] ; then | |
3546 | _saveaccountconf "USER_AGENT" "$_useragent" | |
fbd2038f | 3547 | elif [ "$USER_AGENT" ] && [ "$USER_AGENT" != "$DEFAULT_USER_AGENT" ] ; then |
d0871bda | 3548 | _saveaccountconf "USER_AGENT" "$USER_AGENT" |
5ea6e9c9 | 3549 | fi |
3550 | ||
3551 | if [ "$_accountemail" ] ; then | |
3552 | _saveaccountconf "ACCOUNT_EMAIL" "$_accountemail" | |
fbd2038f | 3553 | elif [ "$ACCOUNT_EMAIL" ] && [ "$ACCOUNT_EMAIL" != "$DEFAULT_ACCOUNT_EMAIL" ] ; then |
d0871bda | 3554 | _saveaccountconf "ACCOUNT_EMAIL" "$ACCOUNT_EMAIL" |
5ea6e9c9 | 3555 | fi |
3556 | ||
6bf281f9 | 3557 | if [ "$_auto_upgrade" ] ; then |
3558 | _saveaccountconf "AUTO_UPGRADE" "$_auto_upgrade" | |
3559 | elif [ "$AUTO_UPGRADE" ] ; then | |
3560 | _saveaccountconf "AUTO_UPGRADE" "$AUTO_UPGRADE" | |
3561 | fi | |
3562 | ||
5ea6e9c9 | 3563 | } |
3564 | ||
a63b05a9 | 3565 | _process() { |
3566 | _CMD="" | |
3567 | _domain="" | |
3f4513b3 | 3568 | _altdomains="$NO_VALUE" |
a63b05a9 | 3569 | _webroot="" |
bdbf323f | 3570 | _keylength="" |
3571 | _accountkeylength="" | |
3572 | _certpath="" | |
3573 | _keypath="" | |
3574 | _capath="" | |
3575 | _fullchainpath="" | |
4d2f38b0 | 3576 | _reloadcmd="" |
a63b05a9 | 3577 | _password="" |
635695ec | 3578 | _accountconf="" |
3579 | _useragent="" | |
b5eb4b90 | 3580 | _accountemail="" |
3581 | _accountkey="" | |
b2817897 | 3582 | _certhome="" |
39c8f79f | 3583 | _httpport="" |
e22bcf7c | 3584 | _tlsport="" |
0e38c60d | 3585 | _dnssleep="" |
dcf4f8f6 | 3586 | _listraw="" |
cc179731 | 3587 | _stopRenewOnError="" |
13d7cae9 | 3588 | _insecure="" |
78009539 | 3589 | _ca_bundle="" |
c8e9a31e | 3590 | _nocron="" |
43822d37 | 3591 | _ecc="" |
10afcaca | 3592 | _csr="" |
b0070f03 | 3593 | _pre_hook="" |
3594 | _post_hook="" | |
3595 | _renew_hook="" | |
5ea6e9c9 | 3596 | _logfile="" |
d0871bda | 3597 | _log="" |
0463b5d6 | 3598 | _local_address="" |
a73c5b33 | 3599 | _log_level="" |
6bf281f9 | 3600 | _auto_upgrade="" |
6ae0f7f5 | 3601 | _listen_v4="" |
3602 | _listen_v6="" | |
8663fb7e | 3603 | while [ ${#} -gt 0 ] ; do |
a63b05a9 | 3604 | case "${1}" in |
3605 | ||
3606 | --help|-h) | |
3607 | showhelp | |
3608 | return | |
3609 | ;; | |
3610 | --version|-v) | |
3611 | version | |
3612 | return | |
3613 | ;; | |
3614 | --install) | |
3615 | _CMD="install" | |
3616 | ;; | |
3617 | --uninstall) | |
3618 | _CMD="uninstall" | |
3619 | ;; | |
52677b0a | 3620 | --upgrade) |
3621 | _CMD="upgrade" | |
3622 | ;; | |
a63b05a9 | 3623 | --issue) |
3624 | _CMD="issue" | |
3625 | ;; | |
10afcaca | 3626 | --signcsr) |
3627 | _CMD="signcsr" | |
3628 | ;; | |
3629 | --showcsr) | |
3630 | _CMD="showcsr" | |
3631 | ;; | |
a63b05a9 | 3632 | --installcert|-i) |
3633 | _CMD="installcert" | |
3634 | ;; | |
3635 | --renew|-r) | |
3636 | _CMD="renew" | |
3637 | ;; | |
4d2f38b0 | 3638 | --renewAll|--renewall) |
a63b05a9 | 3639 | _CMD="renewAll" |
3640 | ;; | |
3641 | --revoke) | |
3642 | _CMD="revoke" | |
3643 | ;; | |
6d7eda3e | 3644 | --list) |
3645 | _CMD="list" | |
3646 | ;; | |
a63b05a9 | 3647 | --installcronjob) |
3648 | _CMD="installcronjob" | |
3649 | ;; | |
3650 | --uninstallcronjob) | |
3651 | _CMD="uninstallcronjob" | |
3652 | ;; | |
3653 | --cron) | |
3654 | _CMD="cron" | |
3655 | ;; | |
3656 | --toPkcs) | |
3657 | _CMD="toPkcs" | |
3658 | ;; | |
3659 | --createAccountKey|--createaccountkey|-cak) | |
3660 | _CMD="createAccountKey" | |
3661 | ;; | |
3662 | --createDomainKey|--createdomainkey|-cdk) | |
3663 | _CMD="createDomainKey" | |
3664 | ;; | |
3665 | --createCSR|--createcsr|-ccr) | |
3666 | _CMD="createCSR" | |
3667 | ;; | |
0c00e870 | 3668 | --deactivate) |
3669 | _CMD="deactivate" | |
3670 | ;; | |
eb59817e | 3671 | --updateaccount) |
3672 | _CMD="updateaccount" | |
3673 | ;; | |
3674 | --registeraccount) | |
3675 | _CMD="registeraccount" | |
3676 | ;; | |
a63b05a9 | 3677 | --domain|-d) |
3678 | _dvalue="$2" | |
3679 | ||
ee1737a5 | 3680 | if [ "$_dvalue" ] ; then |
3681 | if _startswith "$_dvalue" "-" ; then | |
3682 | _err "'$_dvalue' is not a valid domain for parameter '$1'" | |
3683 | return 1 | |
3684 | fi | |
3685 | ||
3686 | if [ -z "$_domain" ] ; then | |
3687 | _domain="$_dvalue" | |
a63b05a9 | 3688 | else |
3f4513b3 | 3689 | if [ "$_altdomains" = "$NO_VALUE" ] ; then |
ee1737a5 | 3690 | _altdomains="$_dvalue" |
3691 | else | |
3692 | _altdomains="$_altdomains,$_dvalue" | |
3693 | fi | |
a63b05a9 | 3694 | fi |
3695 | fi | |
ee1737a5 | 3696 | |
a63b05a9 | 3697 | shift |
3698 | ;; | |
3699 | ||
3700 | --force|-f) | |
3701 | FORCE="1" | |
3702 | ;; | |
3703 | --staging|--test) | |
3704 | STAGE="1" | |
3705 | ;; | |
3706 | --debug) | |
8663fb7e | 3707 | if [ -z "$2" ] || _startswith "$2" "-" ; then |
a63b05a9 | 3708 | DEBUG="1" |
3709 | else | |
3710 | DEBUG="$2" | |
3711 | shift | |
6fc1447f | 3712 | fi |
a63b05a9 | 3713 | ;; |
a63b05a9 | 3714 | --webroot|-w) |
3715 | wvalue="$2" | |
8663fb7e | 3716 | if [ -z "$_webroot" ] ; then |
a63b05a9 | 3717 | _webroot="$wvalue" |
3718 | else | |
3719 | _webroot="$_webroot,$wvalue" | |
3720 | fi | |
3721 | shift | |
3722 | ;; | |
3723 | --standalone) | |
3f4513b3 | 3724 | wvalue="$NO_VALUE" |
8663fb7e | 3725 | if [ -z "$_webroot" ] ; then |
a63b05a9 | 3726 | _webroot="$wvalue" |
3727 | else | |
3728 | _webroot="$_webroot,$wvalue" | |
3729 | fi | |
3730 | ;; | |
0463b5d6 | 3731 | --local-address) |
3732 | lvalue="$2" | |
3733 | _local_address="$_local_address$lvalue," | |
3734 | shift | |
3735 | ;; | |
a63b05a9 | 3736 | --apache) |
3737 | wvalue="apache" | |
8663fb7e | 3738 | if [ -z "$_webroot" ] ; then |
a63b05a9 | 3739 | _webroot="$wvalue" |
3740 | else | |
3741 | _webroot="$_webroot,$wvalue" | |
3742 | fi | |
3743 | ;; | |
e22bcf7c | 3744 | --tls) |
3745 | wvalue="$W_TLS" | |
3746 | if [ -z "$_webroot" ] ; then | |
3747 | _webroot="$wvalue" | |
3748 | else | |
3749 | _webroot="$_webroot,$wvalue" | |
3750 | fi | |
3751 | ;; | |
a63b05a9 | 3752 | --dns) |
3753 | wvalue="dns" | |
dceb3aca | 3754 | if ! _startswith "$2" "-" ; then |
a63b05a9 | 3755 | wvalue="$2" |
3756 | shift | |
3757 | fi | |
8663fb7e | 3758 | if [ -z "$_webroot" ] ; then |
a63b05a9 | 3759 | _webroot="$wvalue" |
3760 | else | |
3761 | _webroot="$_webroot,$wvalue" | |
3762 | fi | |
3763 | ;; | |
0e38c60d | 3764 | --dnssleep) |
3765 | _dnssleep="$2" | |
3766 | Le_DNSSleep="$_dnssleep" | |
3767 | shift | |
3768 | ;; | |
3769 | ||
a63b05a9 | 3770 | --keylength|-k) |
3771 | _keylength="$2" | |
3f4513b3 | 3772 | if [ "$_accountkeylength" = "$NO_VALUE" ] ; then |
2ce87fe2 | 3773 | _accountkeylength="$2" |
3774 | fi | |
a63b05a9 | 3775 | shift |
3776 | ;; | |
3777 | --accountkeylength|-ak) | |
2ce87fe2 | 3778 | _accountkeylength="$2" |
a63b05a9 | 3779 | shift |
3780 | ;; | |
3781 | ||
3782 | --certpath) | |
3783 | _certpath="$2" | |
3784 | shift | |
3785 | ;; | |
3786 | --keypath) | |
3787 | _keypath="$2" | |
3788 | shift | |
3789 | ;; | |
3790 | --capath) | |
3791 | _capath="$2" | |
3792 | shift | |
3793 | ;; | |
3794 | --fullchainpath) | |
3795 | _fullchainpath="$2" | |
3796 | shift | |
3797 | ;; | |
635695ec | 3798 | --reloadcmd|--reloadCmd) |
a63b05a9 | 3799 | _reloadcmd="$2" |
3800 | shift | |
3801 | ;; | |
3802 | --password) | |
3803 | _password="$2" | |
3804 | shift | |
3805 | ;; | |
3806 | --accountconf) | |
635695ec | 3807 | _accountconf="$2" |
3808 | ACCOUNT_CONF_PATH="$_accountconf" | |
a7b7355d | 3809 | shift |
a63b05a9 | 3810 | ;; |
a7b7355d | 3811 | --home) |
a63b05a9 | 3812 | LE_WORKING_DIR="$2" |
a7b7355d | 3813 | shift |
a63b05a9 | 3814 | ;; |
b2817897 | 3815 | --certhome) |
3816 | _certhome="$2" | |
3817 | CERT_HOME="$_certhome" | |
3818 | shift | |
3819 | ;; | |
635695ec | 3820 | --useragent) |
3821 | _useragent="$2" | |
3822 | USER_AGENT="$_useragent" | |
3823 | shift | |
3824 | ;; | |
b5eb4b90 | 3825 | --accountemail ) |
3826 | _accountemail="$2" | |
3827 | ACCOUNT_EMAIL="$_accountemail" | |
3828 | shift | |
3829 | ;; | |
3830 | --accountkey ) | |
3831 | _accountkey="$2" | |
3832 | ACCOUNT_KEY_PATH="$_accountkey" | |
3833 | shift | |
3834 | ;; | |
06625071 | 3835 | --days ) |
3836 | _days="$2" | |
3837 | Le_RenewalDays="$_days" | |
3838 | shift | |
3839 | ;; | |
39c8f79f | 3840 | --httpport ) |
3841 | _httpport="$2" | |
3842 | Le_HTTPPort="$_httpport" | |
3843 | shift | |
3844 | ;; | |
e22bcf7c | 3845 | --tlsport ) |
3846 | _tlsport="$2" | |
3847 | Le_TLSPort="$_tlsport" | |
3848 | shift | |
3849 | ;; | |
3850 | ||
dcf4f8f6 | 3851 | --listraw ) |
3852 | _listraw="raw" | |
3853 | ;; | |
cc179731 | 3854 | --stopRenewOnError|--stoprenewonerror|-se ) |
3855 | _stopRenewOnError="1" | |
3856 | ;; | |
13d7cae9 | 3857 | --insecure) |
3858 | _insecure="1" | |
fac1e367 | 3859 | HTTPS_INSECURE="1" |
13d7cae9 | 3860 | ;; |
78009539 | 3861 | --ca-bundle) |
775bd1ab | 3862 | _ca_bundle="$(readlink -f $2)" |
78009539 PS |
3863 | CA_BUNDLE="$_ca_bundle" |
3864 | shift | |
3865 | ;; | |
c8e9a31e | 3866 | --nocron) |
3867 | _nocron="1" | |
3868 | ;; | |
43822d37 | 3869 | --ecc) |
3870 | _ecc="isEcc" | |
3871 | ;; | |
10afcaca | 3872 | --csr) |
3873 | _csr="$2" | |
3874 | shift | |
3875 | ;; | |
b0070f03 | 3876 | --pre-hook) |
3877 | _pre_hook="$2" | |
3878 | shift | |
3879 | ;; | |
3880 | --post-hook) | |
3881 | _post_hook="$2" | |
3882 | shift | |
3883 | ;; | |
3884 | --renew-hook) | |
3885 | _renew_hook="$2" | |
3886 | shift | |
3887 | ;; | |
0c9546cc | 3888 | --ocsp-must-staple|--ocsp) |
3889 | Le_OCSP_Stable="1" | |
3890 | ;; | |
d0871bda | 3891 | --log|--logfile) |
3892 | _log="1" | |
5ea6e9c9 | 3893 | _logfile="$2" |
6bf281f9 | 3894 | if _startswith "$_logfile" '-' ; then |
d0871bda | 3895 | _logfile="" |
3896 | else | |
3897 | shift | |
3898 | fi | |
5ea6e9c9 | 3899 | LOG_FILE="$_logfile" |
a73c5b33 | 3900 | if [ -z "$LOG_LEVEL" ] ; then |
3901 | LOG_LEVEL="$DEFAULT_LOG_LEVEL" | |
3902 | fi | |
3903 | ;; | |
3904 | --log-level) | |
30bfc2ce | 3905 | _log_level="$2" |
a73c5b33 | 3906 | LOG_LEVEL="$_log_level" |
3907 | shift | |
5ea6e9c9 | 3908 | ;; |
6bf281f9 | 3909 | --auto-upgrade) |
3910 | _auto_upgrade="$2" | |
3911 | if [ -z "$_auto_upgrade" ] || _startswith "$_auto_upgrade" '-' ; then | |
3912 | _auto_upgrade="1" | |
3913 | else | |
3914 | shift | |
3915 | fi | |
3916 | AUTO_UPGRADE="$_auto_upgrade" | |
3917 | ;; | |
6ae0f7f5 | 3918 | --listen-v4) |
3919 | _listen_v4="1" | |
3920 | Le_Listen_V4="$_listen_v4" | |
3921 | ;; | |
3922 | --listen-v6) | |
3923 | _listen_v6="1" | |
3924 | Le_Listen_V6="$_listen_v6" | |
3925 | ;; | |
6bf281f9 | 3926 | |
a63b05a9 | 3927 | *) |
3928 | _err "Unknown parameter : $1" | |
3929 | return 1 | |
3930 | ;; | |
3931 | esac | |
3932 | ||
3933 | shift 1 | |
3934 | done | |
3935 | ||
5ea6e9c9 | 3936 | if [ "${_CMD}" != "install" ] ; then |
3937 | __initHome | |
d0871bda | 3938 | if [ "$_log" ] && [ -z "$_logfile" ] ; then |
3939 | _logfile="$DEFAULT_LOG_FILE" | |
3940 | fi | |
5ea6e9c9 | 3941 | if [ "$_logfile" ] ; then |
3942 | _saveaccountconf "LOG_FILE" "$_logfile" | |
3943 | fi | |
d0871bda | 3944 | LOG_FILE="$_logfile" |
a73c5b33 | 3945 | |
3946 | if [ "$_log_level" ] ; then | |
3947 | _saveaccountconf "LOG_LEVEL" "$_log_level" | |
3948 | LOG_LEVEL="$_log_level" | |
3949 | fi | |
3950 | ||
5ea6e9c9 | 3951 | _processAccountConf |
3952 | fi | |
3953 | ||
dcf9cb58 | 3954 | if [ "$DEBUG" ] ; then |
3955 | version | |
3956 | fi | |
a63b05a9 | 3957 | |
3958 | case "${_CMD}" in | |
c8e9a31e | 3959 | install) install "$_nocron" ;; |
bc96082f | 3960 | uninstall) uninstall "$_nocron" ;; |
52677b0a | 3961 | upgrade) upgrade ;; |
a63b05a9 | 3962 | issue) |
0463b5d6 | 3963 | issue "$_webroot" "$_domain" "$_altdomains" "$_keylength" "$_certpath" "$_keypath" "$_capath" "$_reloadcmd" "$_fullchainpath" "$_pre_hook" "$_post_hook" "$_renew_hook" "$_local_address" |
a63b05a9 | 3964 | ;; |
10afcaca | 3965 | signcsr) |
3966 | signcsr "$_csr" "$_webroot" | |
3967 | ;; | |
3968 | showcsr) | |
3969 | showcsr "$_csr" "$_domain" | |
3970 | ;; | |
a63b05a9 | 3971 | installcert) |
43822d37 | 3972 | installcert "$_domain" "$_certpath" "$_keypath" "$_capath" "$_reloadcmd" "$_fullchainpath" "$_ecc" |
a63b05a9 | 3973 | ;; |
3974 | renew) | |
43822d37 | 3975 | renew "$_domain" "$_ecc" |
a63b05a9 | 3976 | ;; |
3977 | renewAll) | |
cc179731 | 3978 | renewAll "$_stopRenewOnError" |
a63b05a9 | 3979 | ;; |
3980 | revoke) | |
43822d37 | 3981 | revoke "$_domain" "$_ecc" |
a63b05a9 | 3982 | ;; |
0c00e870 | 3983 | deactivate) |
3f4513b3 | 3984 | deactivate "$_domain,$_altdomains" |
eb59817e | 3985 | ;; |
3986 | registeraccount) | |
3987 | registeraccount | |
3988 | ;; | |
3989 | updateaccount) | |
3990 | updateaccount | |
3991 | ;; | |
6d7eda3e | 3992 | list) |
dcf4f8f6 | 3993 | list "$_listraw" |
6d7eda3e | 3994 | ;; |
a63b05a9 | 3995 | installcronjob) installcronjob ;; |
3996 | uninstallcronjob) uninstallcronjob ;; | |
3997 | cron) cron ;; | |
3998 | toPkcs) | |
43822d37 | 3999 | toPkcs "$_domain" "$_password" "$_ecc" |
a63b05a9 | 4000 | ;; |
4001 | createAccountKey) | |
5fbc47eb | 4002 | createAccountKey "$_accountkeylength" |
a63b05a9 | 4003 | ;; |
4004 | createDomainKey) | |
4005 | createDomainKey "$_domain" "$_keylength" | |
4006 | ;; | |
4007 | createCSR) | |
43822d37 | 4008 | createCSR "$_domain" "$_altdomains" "$_ecc" |
a63b05a9 | 4009 | ;; |
4010 | ||
4011 | *) | |
4012 | _err "Invalid command: $_CMD" | |
4013 | showhelp; | |
4014 | return 1 | |
4015 | ;; | |
4016 | esac | |
d3595686 | 4017 | _ret="$?" |
4018 | if [ "$_ret" != "0" ] ; then | |
4019 | return $_ret | |
4020 | fi | |
a63b05a9 | 4021 | |
5ea6e9c9 | 4022 | if [ "${_CMD}" = "install" ] ; then |
d0871bda | 4023 | if [ "$_log" ] ; then |
4024 | if [ -z "$LOG_FILE" ] ; then | |
4025 | LOG_FILE="$DEFAULT_LOG_FILE" | |
4026 | fi | |
4027 | _saveaccountconf "LOG_FILE" "$LOG_FILE" | |
5ea6e9c9 | 4028 | fi |
a73c5b33 | 4029 | |
4030 | if [ "$_log_level" ] ; then | |
4031 | _saveaccountconf "LOG_LEVEL" "$_log_level" | |
4032 | fi | |
5ea6e9c9 | 4033 | _processAccountConf |
b5eb4b90 | 4034 | fi |
635695ec | 4035 | |
a63b05a9 | 4036 | } |
4037 | ||
4038 | ||
8663fb7e | 4039 | if [ "$INSTALLONLINE" ] ; then |
d1f97fc8 | 4040 | INSTALLONLINE="" |
4a0f23e2 | 4041 | _installOnline $BRANCH |
4042 | exit | |
4043 | fi | |
4c3b3608 | 4044 | |
a63b05a9 | 4045 | |
276b51d9 | 4046 | |
4047 | ||
a63b05a9 | 4048 | |
319e0ae3 | 4049 | main() { |
4050 | [ -z "$1" ] && showhelp && return | |
4051 | if _startswith "$1" '-' ; then _process "$@"; else "$@";fi | |
4052 | } | |
e69a7c38 | 4053 | |
319e0ae3 | 4054 | main "$@" |