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