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