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