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