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