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