]> git.proxmox.com Git - mirror_acme.sh.git/blame - acme.sh
Merge pull request #1752 from Neilpang/badnonce
[mirror_acme.sh.git] / acme.sh
CommitLineData
0a7c9364 1#!/usr/bin/env sh
bfdf1f48 2
b9b70323 3VER=2.8.0
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() {
cb115809
JM
1377 #BSD
1378 if date -u -r "$1" 2>/dev/null; then
4c3b3608 1379 return
1380 fi
4c2a3841 1381
cb115809
JM
1382 #Linux
1383 if date -u -d@"$1" 2>/dev/null; then
4c3b3608 1384 return
1385 fi
4c2a3841 1386
cb115809 1387 #Solaris
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
9cecd525 1610# body url [needbase64] [POST|PUT|DELETE] [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
cc2d5946 1798 __request_conent_type="$CONTENT_TYPE_JSON"
1799
11927a76 1800 payload64=$(printf "%s" "$payload" | _base64 | _url_replace)
f9a6988e 1801 _debug3 payload64 "$payload64"
4c2a3841 1802
709a3fb0 1803 MAX_REQUEST_RETRY_TIMES=20
1804 _sleep_retry_sec=1
0bc745f6 1805 _request_retry_times=0
1806 while [ "${_request_retry_times}" -lt "$MAX_REQUEST_RETRY_TIMES" ]; do
aad309ee 1807 _request_retry_times=$(_math "$_request_retry_times" + 1)
b7924ce5 1808 _debug3 _request_retry_times "$_request_retry_times"
0bc745f6 1809 if [ -z "$_CACHED_NONCE" ]; then
8f01919f 1810 _headers=""
cae50e16 1811 if [ "$ACME_NEW_NONCE" ]; then
1812 _debug2 "Get nonce. ACME_NEW_NONCE" "$ACME_NEW_NONCE"
1813 nonceurl="$ACME_NEW_NONCE"
6a66ba8a 1814 if _post "" "$nonceurl" "" "HEAD" "$__request_conent_type"; then
cae50e16 1815 _headers="$(cat "$HTTP_HEADER")"
1816 fi
1817 fi
1818 if [ -z "$_headers" ]; then
1819 _debug2 "Get nonce. ACME_DIRECTORY" "$ACME_DIRECTORY"
1820 nonceurl="$ACME_DIRECTORY"
1821 _headers="$(_get "$nonceurl" "onlyheader")"
1822 fi
0bc745f6 1823
1824 if [ "$?" != "0" ]; then
1825 _err "Can not connect to $nonceurl to get nonce."
1826 return 1
1827 fi
4c2a3841 1828
0bc745f6 1829 _debug2 _headers "$_headers"
4c2a3841 1830
0bc745f6 1831 _CACHED_NONCE="$(echo "$_headers" | grep "Replay-Nonce:" | _head_n 1 | tr -d "\r\n " | cut -d ':' -f 2)"
1832 _debug2 _CACHED_NONCE "$_CACHED_NONCE"
1833 else
1834 _debug2 "Use _CACHED_NONCE" "$_CACHED_NONCE"
1835 fi
1836 nonce="$_CACHED_NONCE"
1837 _debug2 nonce "$nonce"
aad309ee 1838 if [ -z "$nonce" ]; then
1839 _info "Could not get nonce, let's try again."
1840 _sleep 2
1841 continue
1842 fi
c1151b0d 1843 if [ "$ACME_VERSION" = "2" ]; then
d2cde379 1844 if [ "$url" = "$ACME_NEW_ACCOUNT" ] || [ "$url" = "$ACME_REVOKE_CERT" ]; then
c1151b0d 1845 protected="$JWK_HEADERPLACE_PART1$nonce\", \"url\": \"${url}$JWK_HEADERPLACE_PART2, \"jwk\": $jwk"'}'
1846 else
6b798b01 1847 protected="$JWK_HEADERPLACE_PART1$nonce\", \"url\": \"${url}$JWK_HEADERPLACE_PART2, \"kid\": \"${ACCOUNT_URL}\""'}'
c1151b0d 1848 fi
1849 else
1850 protected="$JWK_HEADERPLACE_PART1$nonce\", \"url\": \"${url}$JWK_HEADERPLACE_PART2, \"jwk\": $jwk"'}'
1851 fi
0bc745f6 1852 _debug3 protected "$protected"
a272ee4f 1853
0bc745f6 1854 protected64="$(printf "%s" "$protected" | _base64 | _url_replace)"
1855 _debug3 protected64 "$protected64"
4c2a3841 1856
0bc745f6 1857 if ! _sig_t="$(printf "%s" "$protected64.$payload64" | _sign "$keyfile" "sha256")"; then
1858 _err "Sign request failed."
1859 return 1
1860 fi
1861 _debug3 _sig_t "$_sig_t"
166096dc 1862
0bc745f6 1863 sig="$(printf "%s" "$_sig_t" | _url_replace)"
1864 _debug3 sig "$sig"
4c2a3841 1865
c1151b0d 1866 if [ "$ACME_VERSION" = "2" ]; then
1867 body="{\"protected\": \"$protected64\", \"payload\": \"$payload64\", \"signature\": \"$sig\"}"
1868 else
1869 body="{\"header\": $JWK_HEADER, \"protected\": \"$protected64\", \"payload\": \"$payload64\", \"signature\": \"$sig\"}"
1870 fi
0bc745f6 1871 _debug3 body "$body"
4c2a3841 1872
6a66ba8a 1873 response="$(_post "$body" "$url" "$needbase64" "POST" "$__request_conent_type")"
0bc745f6 1874 _CACHED_NONCE=""
bbbdcb09 1875
0bc745f6 1876 if [ "$?" != "0" ]; then
1877 _err "Can not post to $url"
1878 return 1
1879 fi
1880 _debug2 original "$response"
1881 response="$(echo "$response" | _normalizeJson)"
4c3b3608 1882
64802502 1883 responseHeaders="$(cat "$HTTP_HEADER")"
4c3b3608 1884
0bc745f6 1885 _debug2 responseHeaders "$responseHeaders"
1886 _debug2 response "$response"
1887 code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\r\n")"
1888 _debug code "$code"
4c2a3841 1889
0bc745f6 1890 _CACHED_NONCE="$(echo "$responseHeaders" | grep "Replay-Nonce:" | _head_n 1 | tr -d "\r\n " | cut -d ':' -f 2)"
4c3b3608 1891
8148bfea 1892 _body="$response"
1893 if [ "$needbase64" ]; then
e1db5db8 1894 _body="$(echo "$_body" | _dbase64 | tr -d '\0')"
6d6b2efd 1895 _debug3 _body "$_body"
8148bfea 1896 fi
36309e6d 1897
9cecd525 1898 if _contains "$_body" "JWS has invalid anti-replay nonce" || _contains "$_body" "JWS has an invalid anti-replay nonce"; then
709a3fb0 1899 _info "It seems the CA server is busy now, let's wait and retry. Sleeping $_sleep_retry_sec seconds."
411b342a 1900 _CACHED_NONCE=""
709a3fb0 1901 _sleep $_sleep_retry_sec
0bc745f6 1902 continue
1903 fi
b7924ce5 1904 break
0bc745f6 1905 done
4c3b3608 1906
4c3b3608 1907}
4c3b3608 1908
1909#setopt "file" "opt" "=" "value" [";"]
1910_setopt() {
1911 __conf="$1"
1912 __opt="$2"
1913 __sep="$3"
1914 __val="$4"
1915 __end="$5"
4c2a3841 1916 if [ -z "$__opt" ]; then
1917 _usage usage: _setopt '"file" "opt" "=" "value" [";"]'
4c3b3608 1918 return
1919 fi
4c2a3841 1920 if [ ! -f "$__conf" ]; then
4c3b3608 1921 touch "$__conf"
1922 fi
1923
4c2a3841 1924 if grep -n "^$__opt$__sep" "$__conf" >/dev/null; then
22ea4004 1925 _debug3 OK
4c2a3841 1926 if _contains "$__val" "&"; then
79a267ab 1927 __val="$(echo "$__val" | sed 's/&/\\&/g')"
4c3b3608 1928 fi
79a267ab 1929 text="$(cat "$__conf")"
52f8b787 1930 printf -- "%s\n" "$text" | sed "s|^$__opt$__sep.*$|$__opt$__sep$__val$__end|" >"$__conf"
4c3b3608 1931
4c2a3841 1932 elif grep -n "^#$__opt$__sep" "$__conf" >/dev/null; then
1933 if _contains "$__val" "&"; then
79a267ab 1934 __val="$(echo "$__val" | sed 's/&/\\&/g')"
4c3b3608 1935 fi
79a267ab 1936 text="$(cat "$__conf")"
52f8b787 1937 printf -- "%s\n" "$text" | sed "s|^#$__opt$__sep.*$|$__opt$__sep$__val$__end|" >"$__conf"
4c3b3608 1938
1939 else
22ea4004 1940 _debug3 APP
4c2a3841 1941 echo "$__opt$__sep$__val$__end" >>"$__conf"
4c3b3608 1942 fi
1efb2085 1943 _debug3 "$(grep -n "^$__opt$__sep" "$__conf")"
4c3b3608 1944}
1945
8a29fbc8 1946#_save_conf file key value
1947#save to conf
1948_save_conf() {
1949 _s_c_f="$1"
1950 _sdkey="$2"
1951 _sdvalue="$3"
4c2a3841 1952 if [ "$_s_c_f" ]; then
8a29fbc8 1953 _setopt "$_s_c_f" "$_sdkey" "=" "'$_sdvalue'"
4d2f38b0 1954 else
8a29fbc8 1955 _err "config file is empty, can not save $_sdkey=$_sdvalue"
4d2f38b0 1956 fi
1957}
1958
8a29fbc8 1959#_clear_conf file key
1960_clear_conf() {
1961 _c_c_f="$1"
1962 _sdkey="$2"
4c2a3841 1963 if [ "$_c_c_f" ]; then
20ea8591 1964 _conf_data="$(cat "$_c_c_f")"
fa574fe8 1965 echo "$_conf_data" | sed "s/^$_sdkey *=.*$//" >"$_c_c_f"
4c3b3608 1966 else
8a29fbc8 1967 _err "config file is empty, can not clear"
4c3b3608 1968 fi
1969}
1970
8a29fbc8 1971#_read_conf file key
1972_read_conf() {
1973 _r_c_f="$1"
1974 _sdkey="$2"
4c2a3841 1975 if [ -f "$_r_c_f" ]; then
1976 (
79a267ab 1977 eval "$(grep "^$_sdkey *=" "$_r_c_f")"
4c2a3841 1978 eval "printf \"%s\" \"\$$_sdkey\""
1979 )
61623d22 1980 else
57e58ce7 1981 _debug "config file is empty, can not read $_sdkey"
4c3b3608 1982 fi
4c3b3608 1983}
1984
1985#_savedomainconf key value
1986#save to domain.conf
1987_savedomainconf() {
8a29fbc8 1988 _save_conf "$DOMAIN_CONF" "$1" "$2"
4d2f38b0 1989}
1990
1991#_cleardomainconf key
1992_cleardomainconf() {
8a29fbc8 1993 _clear_conf "$DOMAIN_CONF" "$1"
4c3b3608 1994}
1995
61623d22 1996#_readdomainconf key
1997_readdomainconf() {
8a29fbc8 1998 _read_conf "$DOMAIN_CONF" "$1"
61623d22 1999}
2000
4c3b3608 2001#_saveaccountconf key value
2002_saveaccountconf() {
8a29fbc8 2003 _save_conf "$ACCOUNT_CONF_PATH" "$1" "$2"
4c3b3608 2004}
2005
fcdf41ba 2006#key value
2007_saveaccountconf_mutable() {
2008 _save_conf "$ACCOUNT_CONF_PATH" "SAVED_$1" "$2"
2009 #remove later
2010 _clearaccountconf "$1"
2011}
2012
2013#key
2014_readaccountconf() {
2015 _read_conf "$ACCOUNT_CONF_PATH" "$1"
2016}
2017
2018#key
2019_readaccountconf_mutable() {
2020 _rac_key="$1"
2021 _readaccountconf "SAVED_$_rac_key"
2022}
2023
fac1e367 2024#_clearaccountconf key
2025_clearaccountconf() {
8a29fbc8 2026 _clear_conf "$ACCOUNT_CONF_PATH" "$1"
2027}
2028
2029#_savecaconf key value
2030_savecaconf() {
2031 _save_conf "$CA_CONF" "$1" "$2"
2032}
2033
2034#_readcaconf key
2035_readcaconf() {
2036 _read_conf "$CA_CONF" "$1"
2037}
2038
2039#_clearaccountconf key
2040_clearcaconf() {
2041 _clear_conf "$CA_CONF" "$1"
fac1e367 2042}
2043
0463b5d6 2044# content localaddress
4c3b3608 2045_startserver() {
2046 content="$1"
0463b5d6 2047 ncaddr="$2"
2048 _debug "ncaddr" "$ncaddr"
2049
6fc1447f 2050 _debug "startserver: $$"
4c2a3841 2051
39c8f79f 2052 _debug Le_HTTPPort "$Le_HTTPPort"
6ae0f7f5 2053 _debug Le_Listen_V4 "$Le_Listen_V4"
2054 _debug Le_Listen_V6 "$Le_Listen_V6"
4c2a3841 2055
3794b5cb 2056 _NC="socat"
4c2a3841 2057 if [ "$Le_Listen_V4" ]; then
6ae0f7f5 2058 _NC="$_NC -4"
4c2a3841 2059 elif [ "$Le_Listen_V6" ]; then
6ae0f7f5 2060 _NC="$_NC -6"
2061 fi
4c2a3841 2062
9ad7ac63 2063 if [ "$DEBUG" ] && [ "$DEBUG" -gt "1" ]; then
5c568d69 2064 _NC="$_NC -d -d -v"
2065 fi
2066
9134b6ea
HC
2067 SOCAT_OPTIONS=TCP-LISTEN:$Le_HTTPPort,crlf,reuseaddr,fork
2068
2069 #Adding bind to local-address
d84665cb 2070 if [ "$ncaddr" ]; then
9ad7ac63 2071 SOCAT_OPTIONS="$SOCAT_OPTIONS,bind=${ncaddr}"
9134b6ea
HC
2072 fi
2073
5c568d69 2074 _debug "_NC" "$_NC $SOCAT_OPTIONS"
bae50da7 2075 $_NC $SOCAT_OPTIONS SYSTEM:"sleep 1; echo HTTP/1.0 200 OK; echo ; echo $content; echo;" &
3794b5cb 2076 serverproc="$!"
4c3b3608 2077}
2078
4c2a3841 2079_stopserver() {
4c3b3608 2080 pid="$1"
6fc1447f 2081 _debug "pid" "$pid"
4c2a3841 2082 if [ -z "$pid" ]; then
6fc1447f 2083 return
2084 fi
e22bcf7c 2085
3794b5cb 2086 kill $pid
2087
4c3b3608 2088}
2089
fdcb6b72 2090# sleep sec
2091_sleep() {
2092 _sleep_sec="$1"
4c2a3841 2093 if [ "$__INTERACTIVE" ]; then
fdcb6b72 2094 _sleep_c="$_sleep_sec"
4c2a3841 2095 while [ "$_sleep_c" -ge "0" ]; do
c583d6bb 2096 printf "\r \r"
fdcb6b72 2097 __green "$_sleep_c"
79a267ab 2098 _sleep_c="$(_math "$_sleep_c" - 1)"
fdcb6b72 2099 sleep 1
2100 done
c583d6bb 2101 printf "\r"
fdcb6b72 2102 else
2103 sleep "$_sleep_sec"
2104 fi
2105}
e22bcf7c 2106
6ae0f7f5 2107# _starttlsserver san_a san_b port content _ncaddr
e22bcf7c 2108_starttlsserver() {
2109 _info "Starting tls server."
2110 san_a="$1"
2111 san_b="$2"
2112 port="$3"
2113 content="$4"
6ae0f7f5 2114 opaddr="$5"
4c2a3841 2115
e22bcf7c 2116 _debug san_a "$san_a"
2117 _debug san_b "$san_b"
2118 _debug port "$port"
4c2a3841 2119
e22bcf7c 2120 #create key TLS_KEY
4c2a3841 2121 if ! _createkey "2048" "$TLS_KEY"; then
e22bcf7c 2122 _err "Create tls validation key error."
2123 return 1
2124 fi
4c2a3841 2125
e22bcf7c 2126 #create csr
2127 alt="$san_a"
4c2a3841 2128 if [ "$san_b" ]; then
e22bcf7c 2129 alt="$alt,$san_b"
2130 fi
4c2a3841 2131 if ! _createcsr "tls.acme.sh" "$alt" "$TLS_KEY" "$TLS_CSR" "$TLS_CONF"; then
e22bcf7c 2132 _err "Create tls validation csr error."
2133 return 1
2134 fi
4c2a3841 2135
e22bcf7c 2136 #self signed
4c2a3841 2137 if ! _signcsr "$TLS_KEY" "$TLS_CSR" "$TLS_CONF" "$TLS_CERT"; then
e22bcf7c 2138 _err "Create tls validation cert error."
2139 return 1
2140 fi
4c2a3841 2141
5f6e3da7 2142 __S_OPENSSL="${ACME_OPENSSL_BIN:-openssl} s_server -www -cert $TLS_CERT -key $TLS_KEY "
2143 if [ "$opaddr" ]; then
2144 __S_OPENSSL="$__S_OPENSSL -accept $opaddr:$port"
2145 else
2146 __S_OPENSSL="$__S_OPENSSL -accept $port"
2147 fi
6ae0f7f5 2148
2149 _debug Le_Listen_V4 "$Le_Listen_V4"
2150 _debug Le_Listen_V6 "$Le_Listen_V6"
4c2a3841 2151 if [ "$Le_Listen_V4" ]; then
6ae0f7f5 2152 __S_OPENSSL="$__S_OPENSSL -4"
4c2a3841 2153 elif [ "$Le_Listen_V6" ]; then
6ae0f7f5 2154 __S_OPENSSL="$__S_OPENSSL -6"
2155 fi
4c2a3841 2156
6ae0f7f5 2157 _debug "$__S_OPENSSL"
5f6e3da7 2158 if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
2159 $__S_OPENSSL -tlsextdebug &
2160 else
2161 $__S_OPENSSL >/dev/null 2>&1 &
2162 fi
331c4bb6 2163
e22bcf7c 2164 serverproc="$!"
5dbf664a 2165 sleep 1
d5ec5f80 2166 _debug serverproc "$serverproc"
e22bcf7c 2167}
2168
18e46962 2169#file
2170_readlink() {
2171 _rf="$1"
2172 if ! readlink -f "$_rf" 2>/dev/null; then
6c4cc357 2173 if _startswith "$_rf" "/"; then
2174 echo "$_rf"
7da50703 2175 return 0
2176 fi
6c4cc357 2177 echo "$(pwd)/$_rf" | _conapath
18e46962 2178 fi
2179}
2180
6c4cc357 2181_conapath() {
2182 sed "s#/\./#/#g"
2183}
2184
5ea6e9c9 2185__initHome() {
4c2a3841 2186 if [ -z "$_SCRIPT_HOME" ]; then
2187 if _exists readlink && _exists dirname; then
66990cf8 2188 _debug "Lets find script dir."
f3e4cea3 2189 _debug "_SCRIPT_" "$_SCRIPT_"
18e46962 2190 _script="$(_readlink "$_SCRIPT_")"
f3e4cea3 2191 _debug "_script" "$_script"
2192 _script_home="$(dirname "$_script")"
2193 _debug "_script_home" "$_script_home"
4c2a3841 2194 if [ -d "$_script_home" ]; then
f3e4cea3 2195 _SCRIPT_HOME="$_script_home"
2196 else
2197 _err "It seems the script home is not correct:$_script_home"
2198 fi
2199 fi
2200 fi
2201
219e9115 2202 # if [ -z "$LE_WORKING_DIR" ]; then
2203 # if [ -f "$DEFAULT_INSTALL_HOME/account.conf" ]; then
2204 # _debug "It seems that $PROJECT_NAME is already installed in $DEFAULT_INSTALL_HOME"
2205 # LE_WORKING_DIR="$DEFAULT_INSTALL_HOME"
2206 # else
2207 # LE_WORKING_DIR="$_SCRIPT_HOME"
2208 # fi
2209 # fi
4c2a3841 2210
2211 if [ -z "$LE_WORKING_DIR" ]; then
f3e4cea3 2212 _debug "Using default home:$DEFAULT_INSTALL_HOME"
2213 LE_WORKING_DIR="$DEFAULT_INSTALL_HOME"
2214 fi
7da50703 2215 export LE_WORKING_DIR
f3e4cea3 2216
f5b546b3 2217 if [ -z "$LE_CONFIG_HOME" ]; then
2218 LE_CONFIG_HOME="$LE_WORKING_DIR"
27dbe77f 2219 fi
f5b546b3 2220 _debug "Using config home:$LE_CONFIG_HOME"
2221 export LE_CONFIG_HOME
27dbe77f 2222
f5b546b3 2223 _DEFAULT_ACCOUNT_CONF_PATH="$LE_CONFIG_HOME/account.conf"
d53289d7 2224
4c2a3841 2225 if [ -z "$ACCOUNT_CONF_PATH" ]; then
2226 if [ -f "$_DEFAULT_ACCOUNT_CONF_PATH" ]; then
8663fb7e 2227 . "$_DEFAULT_ACCOUNT_CONF_PATH"
635695ec 2228 fi
d53289d7 2229 fi
4c2a3841 2230
2231 if [ -z "$ACCOUNT_CONF_PATH" ]; then
d53289d7 2232 ACCOUNT_CONF_PATH="$_DEFAULT_ACCOUNT_CONF_PATH"
4c3b3608 2233 fi
4c2a3841 2234
f5b546b3 2235 DEFAULT_LOG_FILE="$LE_CONFIG_HOME/$PROJECT_NAME.log"
4c2a3841 2236
f5b546b3 2237 DEFAULT_CA_HOME="$LE_CONFIG_HOME/ca"
4c2a3841 2238
2239 if [ -z "$LE_TEMP_DIR" ]; then
f5b546b3 2240 LE_TEMP_DIR="$LE_CONFIG_HOME/tmp"
610e0f21 2241 fi
5ea6e9c9 2242}
2243
48d9a8c1 2244#server
2245_initAPI() {
2246 _api_server="${1:-$ACME_DIRECTORY}"
2247 _debug "_init api for server: $_api_server"
4cee14f3 2248
cae50e16 2249 if [ -z "$ACME_NEW_ACCOUNT" ]; then
48d9a8c1 2250 response=$(_get "$_api_server")
2251 if [ "$?" != "0" ]; then
2252 _debug2 "response" "$response"
2253 _err "Can not init api."
2254 return 1
2255 fi
2256 _debug2 "response" "$response"
2257
2258 ACME_KEY_CHANGE=$(echo "$response" | _egrep_o 'key-change" *: *"[^"]*"' | cut -d '"' -f 3)
c1151b0d 2259 if [ -z "$ACME_KEY_CHANGE" ]; then
2260 ACME_KEY_CHANGE=$(echo "$response" | _egrep_o 'keyChange" *: *"[^"]*"' | cut -d '"' -f 3)
2261 fi
48d9a8c1 2262 export ACME_KEY_CHANGE
2263
2264 ACME_NEW_AUTHZ=$(echo "$response" | _egrep_o 'new-authz" *: *"[^"]*"' | cut -d '"' -f 3)
c1151b0d 2265 if [ -z "$ACME_NEW_AUTHZ" ]; then
2266 ACME_NEW_AUTHZ=$(echo "$response" | _egrep_o 'newAuthz" *: *"[^"]*"' | cut -d '"' -f 3)
2267 fi
48d9a8c1 2268 export ACME_NEW_AUTHZ
2269
cae50e16 2270 ACME_NEW_ORDER=$(echo "$response" | _egrep_o 'new-cert" *: *"[^"]*"' | cut -d '"' -f 3)
a71eba07 2271 ACME_NEW_ORDER_RES="new-cert"
cae50e16 2272 if [ -z "$ACME_NEW_ORDER" ]; then
2273 ACME_NEW_ORDER=$(echo "$response" | _egrep_o 'new-order" *: *"[^"]*"' | cut -d '"' -f 3)
a71eba07 2274 ACME_NEW_ORDER_RES="new-order"
c1151b0d 2275 if [ -z "$ACME_NEW_ORDER" ]; then
2276 ACME_NEW_ORDER=$(echo "$response" | _egrep_o 'newOrder" *: *"[^"]*"' | cut -d '"' -f 3)
2277 fi
cae50e16 2278 fi
2279 export ACME_NEW_ORDER
a71eba07 2280 export ACME_NEW_ORDER_RES
48d9a8c1 2281
cae50e16 2282 ACME_NEW_ACCOUNT=$(echo "$response" | _egrep_o 'new-reg" *: *"[^"]*"' | cut -d '"' -f 3)
a71eba07 2283 ACME_NEW_ACCOUNT_RES="new-reg"
cae50e16 2284 if [ -z "$ACME_NEW_ACCOUNT" ]; then
2285 ACME_NEW_ACCOUNT=$(echo "$response" | _egrep_o 'new-account" *: *"[^"]*"' | cut -d '"' -f 3)
a71eba07 2286 ACME_NEW_ACCOUNT_RES="new-account"
c1151b0d 2287 if [ -z "$ACME_NEW_ACCOUNT" ]; then
2288 ACME_NEW_ACCOUNT=$(echo "$response" | _egrep_o 'newAccount" *: *"[^"]*"' | cut -d '"' -f 3)
2289 if [ "$ACME_NEW_ACCOUNT" ]; then
2290 export ACME_VERSION=2
2291 fi
2292 fi
cae50e16 2293 fi
2294 export ACME_NEW_ACCOUNT
a71eba07 2295 export ACME_NEW_ACCOUNT_RES
48d9a8c1 2296
2297 ACME_REVOKE_CERT=$(echo "$response" | _egrep_o 'revoke-cert" *: *"[^"]*"' | cut -d '"' -f 3)
c1151b0d 2298 if [ -z "$ACME_REVOKE_CERT" ]; then
2299 ACME_REVOKE_CERT=$(echo "$response" | _egrep_o 'revokeCert" *: *"[^"]*"' | cut -d '"' -f 3)
2300 fi
48d9a8c1 2301 export ACME_REVOKE_CERT
2302
cae50e16 2303 ACME_NEW_NONCE=$(echo "$response" | _egrep_o 'new-nonce" *: *"[^"]*"' | cut -d '"' -f 3)
c1151b0d 2304 if [ -z "$ACME_NEW_NONCE" ]; then
2305 ACME_NEW_NONCE=$(echo "$response" | _egrep_o 'newNonce" *: *"[^"]*"' | cut -d '"' -f 3)
2306 fi
cae50e16 2307 export ACME_NEW_NONCE
4249e13e 2308
f87890cb 2309 ACME_AGREEMENT=$(echo "$response" | _egrep_o 'terms-of-service" *: *"[^"]*"' | cut -d '"' -f 3)
c1151b0d 2310 if [ -z "$ACME_AGREEMENT" ]; then
2311 ACME_AGREEMENT=$(echo "$response" | _egrep_o 'termsOfService" *: *"[^"]*"' | cut -d '"' -f 3)
2312 fi
f87890cb 2313 export ACME_AGREEMENT
cae50e16 2314
f87890cb 2315 _debug "ACME_KEY_CHANGE" "$ACME_KEY_CHANGE"
2316 _debug "ACME_NEW_AUTHZ" "$ACME_NEW_AUTHZ"
2317 _debug "ACME_NEW_ORDER" "$ACME_NEW_ORDER"
2318 _debug "ACME_NEW_ACCOUNT" "$ACME_NEW_ACCOUNT"
2319 _debug "ACME_REVOKE_CERT" "$ACME_REVOKE_CERT"
2320 _debug "ACME_AGREEMENT" "$ACME_AGREEMENT"
c1151b0d 2321 _debug "ACME_NEW_NONCE" "$ACME_NEW_NONCE"
2322 _debug "ACME_VERSION" "$ACME_VERSION"
48d9a8c1 2323
f87890cb 2324 fi
48d9a8c1 2325}
2326
3281043e 2327#[domain] [keylength or isEcc flag]
5ea6e9c9 2328_initpath() {
cd9fb3b6 2329 domain="$1"
2330 _ilength="$2"
5ea6e9c9 2331
2332 __initHome
2333
4c2a3841 2334 if [ -f "$ACCOUNT_CONF_PATH" ]; then
8663fb7e 2335 . "$ACCOUNT_CONF_PATH"
4c3b3608 2336 fi
2337
4c2a3841 2338 if [ "$IN_CRON" ]; then
2339 if [ ! "$_USER_PATH_EXPORTED" ]; then
281aa349 2340 _USER_PATH_EXPORTED=1
2341 export PATH="$USER_PATH:$PATH"
2342 fi
2343 fi
4c2a3841 2344
2345 if [ -z "$CA_HOME" ]; then
5c48e139 2346 CA_HOME="$DEFAULT_CA_HOME"
2347 fi
281aa349 2348
72f54ca6 2349 if [ "$ACME_VERSION" = "2" ]; then
2350 DEFAULT_CA="$LETSENCRYPT_CA_V2"
2351 DEFAULT_STAGING_CA="$LETSENCRYPT_STAGING_CA_V2"
2352 fi
2353
48d9a8c1 2354 if [ -z "$ACME_DIRECTORY" ]; then
4c2a3841 2355 if [ -z "$STAGE" ]; then
48d9a8c1 2356 ACME_DIRECTORY="$DEFAULT_CA"
4c3b3608 2357 else
c1151b0d 2358 ACME_DIRECTORY="$DEFAULT_STAGING_CA"
48d9a8c1 2359 _info "Using stage ACME_DIRECTORY: $ACME_DIRECTORY"
4c2a3841 2360 fi
4c3b3608 2361 fi
4c2a3841 2362
66444663 2363 _debug ACME_DIRECTORY "$ACME_DIRECTORY"
98394f99 2364 _ACME_SERVER_HOST="$(echo "$ACME_DIRECTORY" | cut -d : -f 2 | tr -s / | cut -d / -f 2)"
48d9a8c1 2365 _debug2 "_ACME_SERVER_HOST" "$_ACME_SERVER_HOST"
2366
2367 CA_DIR="$CA_HOME/$_ACME_SERVER_HOST"
4c2a3841 2368
5c48e139 2369 _DEFAULT_CA_CONF="$CA_DIR/ca.conf"
4c2a3841 2370
2371 if [ -z "$CA_CONF" ]; then
5c48e139 2372 CA_CONF="$_DEFAULT_CA_CONF"
2373 fi
c4236e58 2374 _debug3 CA_CONF "$CA_CONF"
4c2a3841 2375
2376 if [ -f "$CA_CONF" ]; then
5c48e139 2377 . "$CA_CONF"
2378 fi
2379
4c2a3841 2380 if [ -z "$ACME_DIR" ]; then
4c3b3608 2381 ACME_DIR="/home/.acme"
2382 fi
4c2a3841 2383
2384 if [ -z "$APACHE_CONF_BACKUP_DIR" ]; then
f5b546b3 2385 APACHE_CONF_BACKUP_DIR="$LE_CONFIG_HOME"
4c3b3608 2386 fi
4c2a3841 2387
2388 if [ -z "$USER_AGENT" ]; then
bbbdcb09 2389 USER_AGENT="$DEFAULT_USER_AGENT"
2390 fi
4c2a3841 2391
2392 if [ -z "$HTTP_HEADER" ]; then
f5b546b3 2393 HTTP_HEADER="$LE_CONFIG_HOME/http.header"
933c169d 2394 fi
b2817897 2395
5c48e139 2396 _OLD_ACCOUNT_KEY="$LE_WORKING_DIR/account.key"
2397 _OLD_ACCOUNT_JSON="$LE_WORKING_DIR/account.json"
4c2a3841 2398
5c48e139 2399 _DEFAULT_ACCOUNT_KEY_PATH="$CA_DIR/account.key"
2400 _DEFAULT_ACCOUNT_JSON_PATH="$CA_DIR/account.json"
4c2a3841 2401 if [ -z "$ACCOUNT_KEY_PATH" ]; then
b2817897 2402 ACCOUNT_KEY_PATH="$_DEFAULT_ACCOUNT_KEY_PATH"
4c3b3608 2403 fi
4c2a3841 2404
2405 if [ -z "$ACCOUNT_JSON_PATH" ]; then
5c48e139 2406 ACCOUNT_JSON_PATH="$_DEFAULT_ACCOUNT_JSON_PATH"
2407 fi
4c2a3841 2408
f5b546b3 2409 _DEFAULT_CERT_HOME="$LE_CONFIG_HOME"
4c2a3841 2410 if [ -z "$CERT_HOME" ]; then
a79b26af
RD
2411 CERT_HOME="$_DEFAULT_CERT_HOME"
2412 fi
2413
77f1ea40 2414 if [ -z "$ACME_OPENSSL_BIN" ] || [ ! -f "$ACME_OPENSSL_BIN" ] || [ ! -x "$ACME_OPENSSL_BIN" ]; then
851fedf7 2415 ACME_OPENSSL_BIN="$DEFAULT_OPENSSL_BIN"
a746139c 2416 fi
2417
cd9fb3b6 2418 if [ -z "$domain" ]; then
4c3b3608 2419 return 0
2420 fi
4c2a3841 2421
4c2a3841 2422 if [ -z "$DOMAIN_PATH" ]; then
43822d37 2423 domainhome="$CERT_HOME/$domain"
2424 domainhomeecc="$CERT_HOME/$domain$ECC_SUFFIX"
4c2a3841 2425
4c3b3608 2426 DOMAIN_PATH="$domainhome"
4c2a3841 2427
2428 if _isEccKey "$_ilength"; then
43822d37 2429 DOMAIN_PATH="$domainhomeecc"
2430 else
4c2a3841 2431 if [ ! -d "$domainhome" ] && [ -d "$domainhomeecc" ]; then
6d4e903b 2432 _info "The domain '$domain' seems to have a ECC cert already, please add '$(__red "--ecc")' parameter if you want to use that cert."
43822d37 2433 fi
2434 fi
2435 _debug DOMAIN_PATH "$DOMAIN_PATH"
4c3b3608 2436 fi
4c2a3841 2437
fd72cced 2438 if [ -z "$DOMAIN_BACKUP_PATH" ]; then
d88f8e86 2439 DOMAIN_BACKUP_PATH="$DOMAIN_PATH/backup"
fd72cced 2440 fi
2441
4c2a3841 2442 if [ -z "$DOMAIN_CONF" ]; then
43822d37 2443 DOMAIN_CONF="$DOMAIN_PATH/$domain.conf"
4c3b3608 2444 fi
4c2a3841 2445
2446 if [ -z "$DOMAIN_SSL_CONF" ]; then
0c9546cc 2447 DOMAIN_SSL_CONF="$DOMAIN_PATH/$domain.csr.conf"
4c3b3608 2448 fi
4c2a3841 2449
2450 if [ -z "$CSR_PATH" ]; then
43822d37 2451 CSR_PATH="$DOMAIN_PATH/$domain.csr"
4c3b3608 2452 fi
4c2a3841 2453 if [ -z "$CERT_KEY_PATH" ]; then
43822d37 2454 CERT_KEY_PATH="$DOMAIN_PATH/$domain.key"
4c3b3608 2455 fi
4c2a3841 2456 if [ -z "$CERT_PATH" ]; then
43822d37 2457 CERT_PATH="$DOMAIN_PATH/$domain.cer"
4c3b3608 2458 fi
4c2a3841 2459 if [ -z "$CA_CERT_PATH" ]; then
43822d37 2460 CA_CERT_PATH="$DOMAIN_PATH/ca.cer"
4c3b3608 2461 fi
4c2a3841 2462 if [ -z "$CERT_FULLCHAIN_PATH" ]; then
43822d37 2463 CERT_FULLCHAIN_PATH="$DOMAIN_PATH/fullchain.cer"
caf1fc10 2464 fi
4c2a3841 2465 if [ -z "$CERT_PFX_PATH" ]; then
43822d37 2466 CERT_PFX_PATH="$DOMAIN_PATH/$domain.pfx"
ac2d5123 2467 fi
4410226d 2468 if [ -z "$CERT_PKCS8_PATH" ]; then
2469 CERT_PKCS8_PATH="$DOMAIN_PATH/$domain.pkcs8"
2470 fi
4c2a3841 2471
2472 if [ -z "$TLS_CONF" ]; then
f94433e5 2473 TLS_CONF="$DOMAIN_PATH/tls.validation.conf"
e22bcf7c 2474 fi
4c2a3841 2475 if [ -z "$TLS_CERT" ]; then
f94433e5 2476 TLS_CERT="$DOMAIN_PATH/tls.validation.cert"
e22bcf7c 2477 fi
4c2a3841 2478 if [ -z "$TLS_KEY" ]; then
f94433e5 2479 TLS_KEY="$DOMAIN_PATH/tls.validation.key"
e22bcf7c 2480 fi
4c2a3841 2481 if [ -z "$TLS_CSR" ]; then
f94433e5 2482 TLS_CSR="$DOMAIN_PATH/tls.validation.csr"
e22bcf7c 2483 fi
4c2a3841 2484
4c3b3608 2485}
2486
610e0f21 2487_exec() {
4c2a3841 2488 if [ -z "$_EXEC_TEMP_ERR" ]; then
610e0f21 2489 _EXEC_TEMP_ERR="$(_mktemp)"
2490 fi
2491
4c2a3841 2492 if [ "$_EXEC_TEMP_ERR" ]; then
3e5b1024 2493 eval "$@ 2>>$_EXEC_TEMP_ERR"
610e0f21 2494 else
3e5b1024 2495 eval "$@"
610e0f21 2496 fi
2497}
2498
2499_exec_err() {
3e5b1024 2500 [ "$_EXEC_TEMP_ERR" ] && _err "$(cat "$_EXEC_TEMP_ERR")" && echo "" >"$_EXEC_TEMP_ERR"
610e0f21 2501}
4c3b3608 2502
2503_apachePath() {
c3dd3ef0 2504 _APACHECTL="apachectl"
4c2a3841 2505 if ! _exists apachectl; then
2506 if _exists apache2ctl; then
2507 _APACHECTL="apache2ctl"
e4a19585 2508 else
bc96082f 2509 _err "'apachectl not found. It seems that apache is not installed, or you are not root user.'"
e4a19585 2510 _err "Please use webroot mode to try again."
2511 return 1
2512 fi
80a0a7b5 2513 fi
4c2a3841 2514
2515 if ! _exec $_APACHECTL -V >/dev/null; then
610e0f21 2516 _exec_err
2517 return 1
2518 fi
4c2a3841 2519
2520 if [ "$APACHE_HTTPD_CONF" ]; then
5be1449d 2521 _saveaccountconf APACHE_HTTPD_CONF "$APACHE_HTTPD_CONF"
2522 httpdconf="$APACHE_HTTPD_CONF"
79a267ab 2523 httpdconfname="$(basename "$httpdconfname")"
d62ee940 2524 else
4c2a3841 2525 httpdconfname="$($_APACHECTL -V | grep SERVER_CONFIG_FILE= | cut -d = -f 2 | tr -d '"')"
5be1449d 2526 _debug httpdconfname "$httpdconfname"
4c2a3841 2527
2528 if [ -z "$httpdconfname" ]; then
5be1449d 2529 _err "Can not read apache config file."
2530 return 1
2531 fi
4c2a3841 2532
2533 if _startswith "$httpdconfname" '/'; then
5be1449d 2534 httpdconf="$httpdconfname"
79a267ab 2535 httpdconfname="$(basename "$httpdconfname")"
5be1449d 2536 else
4c2a3841 2537 httpdroot="$($_APACHECTL -V | grep HTTPD_ROOT= | cut -d = -f 2 | tr -d '"')"
5be1449d 2538 _debug httpdroot "$httpdroot"
2539 httpdconf="$httpdroot/$httpdconfname"
79a267ab 2540 httpdconfname="$(basename "$httpdconfname")"
5be1449d 2541 fi
d62ee940 2542 fi
78768e98 2543 _debug httpdconf "$httpdconf"
8f63baf7 2544 _debug httpdconfname "$httpdconfname"
4c2a3841 2545 if [ ! -f "$httpdconf" ]; then
78768e98 2546 _err "Apache Config file not found" "$httpdconf"
4c3b3608 2547 return 1
2548 fi
2549 return 0
2550}
2551
2552_restoreApache() {
4c2a3841 2553 if [ -z "$usingApache" ]; then
4c3b3608 2554 return 0
2555 fi
2556 _initpath
4c2a3841 2557 if ! _apachePath; then
4c3b3608 2558 return 1
2559 fi
4c2a3841 2560
2561 if [ ! -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname" ]; then
4c3b3608 2562 _debug "No config file to restore."
2563 return 0
2564 fi
4c2a3841 2565
2566 cat "$APACHE_CONF_BACKUP_DIR/$httpdconfname" >"$httpdconf"
5ef501c5 2567 _debug "Restored: $httpdconf."
4c2a3841 2568 if ! _exec $_APACHECTL -t; then
610e0f21 2569 _exec_err
4c3b3608 2570 _err "Sorry, restore apache config error, please contact me."
4c2a3841 2571 return 1
4c3b3608 2572 fi
5ef501c5 2573 _debug "Restored successfully."
4c3b3608 2574 rm -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname"
4c2a3841 2575 return 0
4c3b3608 2576}
2577
2578_setApache() {
2579 _initpath
4c2a3841 2580 if ! _apachePath; then
4c3b3608 2581 return 1
2582 fi
2583
5fc5016d 2584 #test the conf first
869578ce 2585 _info "Checking if there is an error in the apache config file before starting."
4c2a3841 2586
44edb2bd 2587 if ! _exec "$_APACHECTL" -t >/dev/null; then
610e0f21 2588 _exec_err
2589 _err "The apache config file has error, please fix it first, then try again."
869578ce 2590 _err "Don't worry, there is nothing changed to your system."
4c2a3841 2591 return 1
5fc5016d 2592 else
2593 _info "OK"
2594 fi
4c2a3841 2595
4c3b3608 2596 #backup the conf
5778811a 2597 _debug "Backup apache config file" "$httpdconf"
4c2a3841 2598 if ! cp "$httpdconf" "$APACHE_CONF_BACKUP_DIR/"; then
869578ce 2599 _err "Can not backup apache config file, so abort. Don't worry, the apache config is not changed."
61a48a5b 2600 _err "This might be a bug of $PROJECT_NAME , please report issue: $PROJECT"
8f63baf7 2601 return 1
2602 fi
4c3b3608 2603 _info "JFYI, Config file $httpdconf is backuped to $APACHE_CONF_BACKUP_DIR/$httpdconfname"
2604 _info "In case there is an error that can not be restored automatically, you may try restore it yourself."
329174b6 2605 _info "The backup file will be deleted on success, just forget it."
4c2a3841 2606
4c3b3608 2607 #add alias
4c2a3841 2608
2609 apacheVer="$($_APACHECTL -V | grep "Server version:" | cut -d : -f 2 | cut -d " " -f 2 | cut -d '/' -f 2)"
b09d597c 2610 _debug "apacheVer" "$apacheVer"
2611 apacheMajer="$(echo "$apacheVer" | cut -d . -f 1)"
2612 apacheMinor="$(echo "$apacheVer" | cut -d . -f 2)"
2613
4c2a3841 2614 if [ "$apacheVer" ] && [ "$apacheMajer$apacheMinor" -ge "24" ]; then
b09d597c 2615 echo "
4c3b3608 2616Alias /.well-known/acme-challenge $ACME_DIR
2617
2618<Directory $ACME_DIR >
2619Require all granted
b09d597c 2620</Directory>
4c2a3841 2621 " >>"$httpdconf"
b09d597c 2622 else
2623 echo "
2624Alias /.well-known/acme-challenge $ACME_DIR
2625
2626<Directory $ACME_DIR >
2627Order allow,deny
2628Allow from all
4c3b3608 2629</Directory>
4c2a3841 2630 " >>"$httpdconf"
b09d597c 2631 fi
2632
4c2a3841 2633 _msg="$($_APACHECTL -t 2>&1)"
2634 if [ "$?" != "0" ]; then
5fc5016d 2635 _err "Sorry, apache config error"
4c2a3841 2636 if _restoreApache; then
869578ce 2637 _err "The apache config file is restored."
5fc5016d 2638 else
869578ce 2639 _err "Sorry, The apache config file can not be restored, please report bug."
5fc5016d 2640 fi
4c2a3841 2641 return 1
4c3b3608 2642 fi
4c2a3841 2643
2644 if [ ! -d "$ACME_DIR" ]; then
4c3b3608 2645 mkdir -p "$ACME_DIR"
2646 chmod 755 "$ACME_DIR"
2647 fi
4c2a3841 2648
44edb2bd 2649 if ! _exec "$_APACHECTL" graceful; then
4c2a3841 2650 _exec_err
610e0f21 2651 _err "$_APACHECTL graceful error, please contact me."
4c3b3608 2652 _restoreApache
4c2a3841 2653 return 1
4c3b3608 2654 fi
2655 usingApache="1"
2656 return 0
2657}
2658
9d725af6 2659#find the real nginx conf file
2660#backup
2661#set the nginx conf
2662#returns the real nginx conf file
2663_setNginx() {
2664 _d="$1"
2665 _croot="$2"
2666 _thumbpt="$3"
37f39c08 2667
9d725af6 2668 FOUND_REAL_NGINX_CONF=""
9f90618a 2669 FOUND_REAL_NGINX_CONF_LN=""
9d725af6 2670 BACKUP_NGINX_CONF=""
2671 _debug _croot "$_croot"
2672 _start_f="$(echo "$_croot" | cut -d : -f 2)"
2673 _debug _start_f "$_start_f"
2674 if [ -z "$_start_f" ]; then
2675 _debug "find start conf from nginx command"
2676 if [ -z "$NGINX_CONF" ]; then
37f39c08 2677 if ! _exists "nginx"; then
2678 _err "nginx command is not found."
2679 return 1
2680 fi
9d725af6 2681 NGINX_CONF="$(nginx -V 2>&1 | _egrep_o "--conf-path=[^ ]* " | tr -d " ")"
2682 _debug NGINX_CONF "$NGINX_CONF"
2683 NGINX_CONF="$(echo "$NGINX_CONF" | cut -d = -f 2)"
2684 _debug NGINX_CONF "$NGINX_CONF"
2685 if [ ! -f "$NGINX_CONF" ]; then
2686 _err "'$NGINX_CONF' doesn't exist."
2687 NGINX_CONF=""
2688 return 1
2689 fi
2690 _debug "Found nginx conf file:$NGINX_CONF"
2691 fi
2692 _start_f="$NGINX_CONF"
2693 fi
03f8d6e9 2694 _debug "Start detect nginx conf for $_d from:$_start_f"
9d725af6 2695 if ! _checkConf "$_d" "$_start_f"; then
5378d9ca 2696 _err "Can not find conf file for domain $d"
9d725af6 2697 return 1
2698 fi
2699 _info "Found conf file: $FOUND_REAL_NGINX_CONF"
2700
9f90618a 2701 _ln=$FOUND_REAL_NGINX_CONF_LN
03f8d6e9 2702 _debug "_ln" "$_ln"
2703
2704 _lnn=$(_math $_ln + 1)
2705 _debug _lnn "$_lnn"
2706 _start_tag="$(sed -n "$_lnn,${_lnn}p" "$FOUND_REAL_NGINX_CONF")"
2707 _debug "_start_tag" "$_start_tag"
2708 if [ "$_start_tag" = "$NGINX_START" ]; then
2709 _info "The domain $_d is already configured, skip"
2710 FOUND_REAL_NGINX_CONF=""
2711 return 0
2712 fi
2713
9d725af6 2714 mkdir -p "$DOMAIN_BACKUP_PATH"
2715 _backup_conf="$DOMAIN_BACKUP_PATH/$_d.nginx.conf"
2716 _debug _backup_conf "$_backup_conf"
2717 BACKUP_NGINX_CONF="$_backup_conf"
2718 _info "Backup $FOUND_REAL_NGINX_CONF to $_backup_conf"
2719 if ! cp "$FOUND_REAL_NGINX_CONF" "$_backup_conf"; then
2720 _err "backup error."
2721 FOUND_REAL_NGINX_CONF=""
2722 return 1
2723 fi
2724
37f39c08 2725 if ! _exists "nginx"; then
2726 _err "nginx command is not found."
2727 return 1
2728 fi
9d725af6 2729 _info "Check the nginx conf before setting up."
2730 if ! _exec "nginx -t" >/dev/null; then
2731 _exec_err
2732 return 1
2733 fi
2734
2735 _info "OK, Set up nginx config file"
9d725af6 2736
302c41ed 2737 if ! sed -n "1,${_ln}p" "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"; then
03f8d6e9 2738 cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"
9d725af6 2739 _err "write nginx conf error, but don't worry, the file is restored to the original version."
2740 return 1
2741 fi
2742
03f8d6e9 2743 echo "$NGINX_START
9d725af6 2744location ~ \"^/\.well-known/acme-challenge/([-_a-zA-Z0-9]+)\$\" {
2745 default_type text/plain;
2746 return 200 \"\$1.$_thumbpt\";
3c07f57a 2747}
03f8d6e9 2748#NGINX_START
2749" >>"$FOUND_REAL_NGINX_CONF"
9d725af6 2750
03f8d6e9 2751 if ! sed -n "${_lnn},99999p" "$_backup_conf" >>"$FOUND_REAL_NGINX_CONF"; then
2752 cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"
9d725af6 2753 _err "write nginx conf error, but don't worry, the file is restored."
2754 return 1
2755 fi
df711b0e 2756 _debug3 "Modified config:$(cat $FOUND_REAL_NGINX_CONF)"
9d725af6 2757 _info "nginx conf is done, let's check it again."
2758 if ! _exec "nginx -t" >/dev/null; then
2759 _exec_err
2760 _err "It seems that nginx conf was broken, let's restore."
302c41ed 2761 cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"
9d725af6 2762 return 1
2763 fi
2764
2765 _info "Reload nginx"
2766 if ! _exec "nginx -s reload" >/dev/null; then
2767 _exec_err
2768 _err "It seems that nginx reload error, let's restore."
302c41ed 2769 cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"
9d725af6 2770 return 1
2771 fi
2772
2773 return 0
2774}
2775
2776#d , conf
2777_checkConf() {
2778 _d="$1"
2779 _c_file="$2"
2780 _debug "Start _checkConf from:$_c_file"
2781 if [ ! -f "$2" ] && ! echo "$2" | grep '*$' >/dev/null && echo "$2" | grep '*' >/dev/null; then
2782 _debug "wildcard"
2783 for _w_f in $2; do
7f618e7e 2784 if [ -f "$_w_f" ] && _checkConf "$1" "$_w_f"; then
9d725af6 2785 return 0
2786 fi
2787 done
2788 #not found
2789 return 1
2790 elif [ -f "$2" ]; then
2791 _debug "single"
2792 if _isRealNginxConf "$1" "$2"; then
2793 _debug "$2 is found."
2794 FOUND_REAL_NGINX_CONF="$2"
2795 return 0
2796 fi
f08a79d3 2797 if cat "$2" | tr "\t" " " | grep "^ *include *.*;" >/dev/null; then
9d725af6 2798 _debug "Try include files"
f08a79d3 2799 for included in $(cat "$2" | tr "\t" " " | grep "^ *include *.*;" | sed "s/include //" | tr -d " ;"); do
9d725af6 2800 _debug "check included $included"
2801 if _checkConf "$1" "$included"; then
2802 return 0
2803 fi
2804 done
2805 fi
2806 return 1
2807 else
2808 _debug "$2 not found."
2809 return 1
2810 fi
2811 return 1
2812}
2813
2814#d , conf
2815_isRealNginxConf() {
2816 _debug "_isRealNginxConf $1 $2"
302c41ed 2817 if [ -f "$2" ]; then
3f1a76d9 2818 for _fln in $(tr "\t" ' ' <"$2" | grep -n "^ *server_name.* $1" | cut -d : -f 1); do
302c41ed 2819 _debug _fln "$_fln"
2820 if [ "$_fln" ]; then
04a609b5 2821 _start=$(tr "\t" ' ' <"$2" | _head_n "$_fln" | grep -n "^ *server *" | grep -v server_name | _tail_n 1)
9f90618a 2822 _debug "_start" "$_start"
2823 _start_n=$(echo "$_start" | cut -d : -f 1)
2824 _start_nn=$(_math $_start_n + 1)
2825 _debug "_start_n" "$_start_n"
2826 _debug "_start_nn" "$_start_nn"
2827
2828 _left="$(sed -n "${_start_nn},99999p" "$2")"
2829 _debug2 _left "$_left"
012dd698 2830 _end="$(echo "$_left" | tr "\t" ' ' | grep -n "^ *server *" | grep -v server_name | _head_n 1)"
2831 _debug "_end" "$_end"
2832 if [ "$_end" ]; then
9f90618a 2833 _end_n=$(echo "$_end" | cut -d : -f 1)
2834 _debug "_end_n" "$_end_n"
2835 _seg_n=$(echo "$_left" | sed -n "1,${_end_n}p")
2836 else
2837 _seg_n="$_left"
2838 fi
2839
2840 _debug "_seg_n" "$_seg_n"
2841
04a609b5 2842 _skip_ssl=1
d1067c60 2843 for _listen_i in $(echo "$_seg_n" | tr "\t" ' ' | grep "^ *listen" | tr -d " "); do
04a609b5 2844 if [ "$_listen_i" ]; then
2845 if [ "$(echo "$_listen_i" | _egrep_o "listen.*ssl[ |;]")" ]; then
2846 _debug2 "$_listen_i is ssl"
2847 else
2848 _debug2 "$_listen_i is plain text"
2849 _skip_ssl=""
c05eb0b1 2850 break
2851 fi
04a609b5 2852 fi
2853 done
2854
2855 if [ "$_skip_ssl" = "1" ]; then
9f90618a 2856 _debug "ssl on, skip"
241cfc43 2857 else
2858 FOUND_REAL_NGINX_CONF_LN=$_fln
2859 _debug3 "found FOUND_REAL_NGINX_CONF_LN" "$FOUND_REAL_NGINX_CONF_LN"
2860 return 0
450efea1 2861 fi
302c41ed 2862 fi
2863 done
9d725af6 2864 fi
302c41ed 2865 return 1
9d725af6 2866}
2867
2868#restore all the nginx conf
2869_restoreNginx() {
5d943a35 2870 if [ -z "$NGINX_RESTORE_VLIST" ]; then
9d725af6 2871 _debug "No need to restore nginx, skip."
2872 return
2873 fi
2874 _debug "_restoreNginx"
5d943a35 2875 _debug "NGINX_RESTORE_VLIST" "$NGINX_RESTORE_VLIST"
9d725af6 2876
5d943a35 2877 for ng_entry in $(echo "$NGINX_RESTORE_VLIST" | tr "$dvsep" ' '); do
9d725af6 2878 _debug "ng_entry" "$ng_entry"
2879 _nd=$(echo "$ng_entry" | cut -d "$sep" -f 1)
2880 _ngconf=$(echo "$ng_entry" | cut -d "$sep" -f 2)
2881 _ngbackupconf=$(echo "$ng_entry" | cut -d "$sep" -f 3)
2882 _info "Restoring from $_ngbackupconf to $_ngconf"
302c41ed 2883 cat "$_ngbackupconf" >"$_ngconf"
9d725af6 2884 done
2885
2886 _info "Reload nginx"
2887 if ! _exec "nginx -s reload" >/dev/null; then
2888 _exec_err
2889 _err "It seems that nginx reload error, please report bug."
2890 return 1
2891 fi
2892 return 0
2893}
2894
5ef501c5 2895_clearup() {
44edb2bd 2896 _stopserver "$serverproc"
4c3b3608 2897 serverproc=""
2898 _restoreApache
9d725af6 2899 _restoreNginx
800e3f45 2900 _clearupdns
4c2a3841 2901 if [ -z "$DEBUG" ]; then
e22bcf7c 2902 rm -f "$TLS_CONF"
2903 rm -f "$TLS_CERT"
2904 rm -f "$TLS_KEY"
2905 rm -f "$TLS_CSR"
2906 fi
4c3b3608 2907}
2908
800e3f45 2909_clearupdns() {
2910 _debug "_clearupdns"
4c2a3841 2911 if [ "$dnsadded" != 1 ] || [ -z "$vlist" ]; then
65b22b49 2912 _debug "skip dns."
800e3f45 2913 return
2914 fi
875625b1 2915 _info "Removing DNS records."
4c2a3841 2916 ventries=$(echo "$vlist" | tr ',' ' ')
875625b1 2917 _alias_index=1
4c2a3841 2918 for ventry in $ventries; do
0c538f75 2919 d=$(echo "$ventry" | cut -d "$sep" -f 1)
2920 keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2)
2921 vtype=$(echo "$ventry" | cut -d "$sep" -f 4)
2922 _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5)
11927a76 2923 txt="$(printf "%s" "$keyauthorization" | _digest "sha256" | _url_replace)"
21f201e3 2924 _debug txt "$txt"
4c2a3841 2925 if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
69212114 2926 _debug "$d is already verified, skip $vtype."
800e3f45 2927 continue
2928 fi
2929
4c2a3841 2930 if [ "$vtype" != "$VTYPE_DNS" ]; then
875625b1 2931 _debug "Skip $d for $vtype"
800e3f45 2932 continue
2933 fi
4c2a3841 2934
0c538f75 2935 d_api="$(_findHook "$d" dnsapi "$_currentRoot")"
800e3f45 2936 _debug d_api "$d_api"
4c2a3841 2937
2938 if [ -z "$d_api" ]; then
800e3f45 2939 _info "Not Found domain api file: $d_api"
2940 continue
2941 fi
4c2a3841 2942
800e3f45 2943 (
d5ec5f80 2944 if ! . "$d_api"; then
800e3f45 2945 _err "Load file $d_api error. Please check your api file and try again."
2946 return 1
2947 fi
4c2a3841 2948
800e3f45 2949 rmcommand="${_currentRoot}_rm"
d5ec5f80 2950 if ! _exists "$rmcommand"; then
800e3f45 2951 _err "It seems that your api file doesn't define $rmcommand"
2952 return 1
2953 fi
4c2a3841 2954
72f54ca6 2955 _dns_root_d="$d"
2956 if _startswith "$_dns_root_d" "*."; then
2957 _dns_root_d="$(echo "$_dns_root_d" | sed 's/*.//')"
2958 fi
875625b1 2959
2960 _d_alias="$(_getfield "$_challenge_alias" "$_alias_index")"
2961 _alias_index="$(_math "$_alias_index" + 1)"
2962 _debug "_d_alias" "$_d_alias"
2963 if [ "$_d_alias" ]; then
64821ad4 2964 if _startswith "$_d_alias" "$DNS_ALIAS_PREFIX"; then
2965 txtdomain="$(echo "$_d_alias" | sed "s/$DNS_ALIAS_PREFIX//")"
2966 else
2967 txtdomain="_acme-challenge.$_d_alias"
2968 fi
875625b1 2969 else
2970 txtdomain="_acme-challenge.$_dns_root_d"
2971 fi
4c2a3841 2972
21f201e3 2973 if ! $rmcommand "$txtdomain" "$txt"; then
800e3f45 2974 _err "Error removing txt for domain:$txtdomain"
2975 return 1
2976 fi
2977 )
4c2a3841 2978
800e3f45 2979 done
2980}
2981
4c3b3608 2982# webroot removelevel tokenfile
2983_clearupwebbroot() {
2984 __webroot="$1"
4c2a3841 2985 if [ -z "$__webroot" ]; then
4c3b3608 2986 _debug "no webroot specified, skip"
2987 return 0
2988 fi
4c2a3841 2989
dcf9cb58 2990 _rmpath=""
4c2a3841 2991 if [ "$2" = '1' ]; then
dcf9cb58 2992 _rmpath="$__webroot/.well-known"
4c2a3841 2993 elif [ "$2" = '2' ]; then
dcf9cb58 2994 _rmpath="$__webroot/.well-known/acme-challenge"
4c2a3841 2995 elif [ "$2" = '3' ]; then
dcf9cb58 2996 _rmpath="$__webroot/.well-known/acme-challenge/$3"
4c3b3608 2997 else
cc179731 2998 _debug "Skip for removelevel:$2"
4c3b3608 2999 fi
4c2a3841 3000
3001 if [ "$_rmpath" ]; then
3002 if [ "$DEBUG" ]; then
dcf9cb58 3003 _debug "Debugging, skip removing: $_rmpath"
3004 else
3005 rm -rf "$_rmpath"
3006 fi
3007 fi
4c2a3841 3008
4c3b3608 3009 return 0
3010
3011}
3012
b0070f03 3013_on_before_issue() {
af1cc3b3 3014 _chk_web_roots="$1"
02140ce7 3015 _chk_main_domain="$2"
3016 _chk_alt_domains="$3"
85e1f4ea 3017 _chk_pre_hook="$4"
3018 _chk_local_addr="$5"
30c2d84c 3019 _debug _on_before_issue
38f1b4d2 3020 _debug _chk_main_domain "$_chk_main_domain"
3021 _debug _chk_alt_domains "$_chk_alt_domains"
d0f7c309 3022 #run pre hook
85e1f4ea 3023 if [ "$_chk_pre_hook" ]; then
3024 _info "Run pre hook:'$_chk_pre_hook'"
d0f7c309 3025 if ! (
85e1f4ea 3026 cd "$DOMAIN_PATH" && eval "$_chk_pre_hook"
d0f7c309 3027 ); then
3028 _err "Error when run pre hook."
3029 return 1
3030 fi
3031 fi
3032
af1cc3b3 3033 if _hasfield "$_chk_web_roots" "$NO_VALUE"; then
3794b5cb 3034 if ! _exists "socat"; then
3035 _err "Please install socat tools first."
0463b5d6 3036 return 1
3037 fi
0463b5d6 3038 fi
3039
85e1f4ea 3040 _debug Le_LocalAddress "$_chk_local_addr"
4c2a3841 3041
0463b5d6 3042 _index=1
3043 _currentRoot=""
3044 _addrIndex=1
38f1b4d2 3045 _w_index=1
931d19ee 3046 while true; do
931d19ee 3047 d="$(echo "$_chk_main_domain,$_chk_alt_domains," | cut -d , -f "$_w_index")"
3048 _w_index="$(_math "$_w_index" + 1)"
3049 _debug d "$d"
3050 if [ -z "$d" ]; then
3051 break
3052 fi
d5ec5f80 3053 _debug "Check for domain" "$d"
af1cc3b3 3054 _currentRoot="$(_getfield "$_chk_web_roots" $_index)"
0463b5d6 3055 _debug "_currentRoot" "$_currentRoot"
3056 _index=$(_math $_index + 1)
3057 _checkport=""
4c2a3841 3058 if [ "$_currentRoot" = "$NO_VALUE" ]; then
0463b5d6 3059 _info "Standalone mode."
4c2a3841 3060 if [ -z "$Le_HTTPPort" ]; then
0463b5d6 3061 Le_HTTPPort=80
3062 else
4c2a3841 3063 _savedomainconf "Le_HTTPPort" "$Le_HTTPPort"
0463b5d6 3064 fi
3065 _checkport="$Le_HTTPPort"
4c2a3841 3066 elif [ "$_currentRoot" = "$W_TLS" ]; then
0463b5d6 3067 _info "Standalone tls mode."
4c2a3841 3068 if [ -z "$Le_TLSPort" ]; then
0463b5d6 3069 Le_TLSPort=443
3070 else
4c2a3841 3071 _savedomainconf "Le_TLSPort" "$Le_TLSPort"
0463b5d6 3072 fi
3073 _checkport="$Le_TLSPort"
3074 fi
4c2a3841 3075
3076 if [ "$_checkport" ]; then
0463b5d6 3077 _debug _checkport "$_checkport"
85e1f4ea 3078 _checkaddr="$(_getfield "$_chk_local_addr" $_addrIndex)"
0463b5d6 3079 _debug _checkaddr "$_checkaddr"
4c2a3841 3080
0463b5d6 3081 _addrIndex="$(_math $_addrIndex + 1)"
4c2a3841 3082
0463b5d6 3083 _netprc="$(_ss "$_checkport" | grep "$_checkport")"
3084 netprc="$(echo "$_netprc" | grep "$_checkaddr")"
4c2a3841 3085 if [ -z "$netprc" ]; then
0463b5d6 3086 netprc="$(echo "$_netprc" | grep "$LOCAL_ANY_ADDRESS")"
3087 fi
4c2a3841 3088 if [ "$netprc" ]; then
0463b5d6 3089 _err "$netprc"
4c2a3841 3090 _err "tcp port $_checkport is already used by $(echo "$netprc" | cut -d : -f 4)"
0463b5d6 3091 _err "Please stop it first"
3092 return 1
3093 fi
3094 fi
3095 done
3096
af1cc3b3 3097 if _hasfield "$_chk_web_roots" "apache"; then
4c2a3841 3098 if ! _setApache; then
0463b5d6 3099 _err "set up apache error. Report error to me."
3100 return 1
3101 fi
3102 else
3103 usingApache=""
3104 fi
3105
b0070f03 3106}
3107
3108_on_issue_err() {
85e1f4ea 3109 _chk_post_hook="$1"
58e4d337 3110 _chk_vlist="$2"
30c2d84c 3111 _debug _on_issue_err
cd8fc359 3112
4c2a3841 3113 if [ "$LOG_FILE" ]; then
a73c5b33 3114 _err "Please check log file for more details: $LOG_FILE"
3115 else
54ae008d 3116 _err "Please add '--debug' or '--log' to check more details."
a73c5b33 3117 _err "See: $_DEBUG_WIKI"
3118 fi
4c2a3841 3119
b0070f03 3120 #run the post hook
85e1f4ea 3121 if [ "$_chk_post_hook" ]; then
3122 _info "Run post hook:'$_chk_post_hook'"
b0070f03 3123 if ! (
85e1f4ea 3124 cd "$DOMAIN_PATH" && eval "$_chk_post_hook"
4c2a3841 3125 ); then
b0070f03 3126 _err "Error when run post hook."
3127 return 1
3128 fi
3129 fi
58e4d337 3130
3131 #trigger the validation to flush the pending authz
ea722da3 3132 _debug2 "_chk_vlist" "$_chk_vlist"
58e4d337 3133 if [ "$_chk_vlist" ]; then
3134 (
c719a61e 3135 _debug2 "start to deactivate authz"
3136 ventries=$(echo "$_chk_vlist" | tr "$dvsep" ' ')
3137 for ventry in $ventries; do
3138 d=$(echo "$ventry" | cut -d "$sep" -f 1)
3139 keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2)
3140 uri=$(echo "$ventry" | cut -d "$sep" -f 3)
3141 vtype=$(echo "$ventry" | cut -d "$sep" -f 4)
3142 _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5)
f94433e5 3143 __trigger_validation "$uri" "$keyauthorization"
c719a61e 3144 done
58e4d337 3145 )
3146 fi
3147
3881f221 3148 if [ "$IS_RENEW" = "1" ] && _hasfield "$Le_Webroot" "$W_DNS"; then
309bec47 3149 _err "$_DNS_MANUAL_ERR"
3150 fi
3151
58e4d337 3152 if [ "$DEBUG" ] && [ "$DEBUG" -gt "0" ]; then
3153 _debug "$(_dlg_versions)"
3154 fi
3155
b0070f03 3156}
3157
3158_on_issue_success() {
85e1f4ea 3159 _chk_post_hook="$1"
3160 _chk_renew_hook="$2"
30c2d84c 3161 _debug _on_issue_success
b0070f03 3162 #run the post hook
85e1f4ea 3163 if [ "$_chk_post_hook" ]; then
3164 _info "Run post hook:'$_chk_post_hook'"
b0070f03 3165 if ! (
85e1f4ea 3166 cd "$DOMAIN_PATH" && eval "$_chk_post_hook"
4c2a3841 3167 ); then
b0070f03 3168 _err "Error when run post hook."
3169 return 1
3170 fi
3171 fi
4c2a3841 3172
b0070f03 3173 #run renew hook
85e1f4ea 3174 if [ "$IS_RENEW" ] && [ "$_chk_renew_hook" ]; then
3175 _info "Run renew hook:'$_chk_renew_hook'"
b0070f03 3176 if ! (
85e1f4ea 3177 cd "$DOMAIN_PATH" && eval "$_chk_renew_hook"
4c2a3841 3178 ); then
b0070f03 3179 _err "Error when run renew hook."
3180 return 1
3181 fi
4c2a3841 3182 fi
3183
3881f221 3184 if _hasfield "$Le_Webroot" "$W_DNS"; then
309bec47 3185 _err "$_DNS_MANUAL_WARN"
3186 fi
3187
b0070f03 3188}
3189
eb59817e 3190updateaccount() {
3191 _initpath
3192 _regAccount
3193}
b0070f03 3194
eb59817e 3195registeraccount() {
57e58ce7 3196 _reg_length="$1"
eb59817e 3197 _initpath
57e58ce7 3198 _regAccount "$_reg_length"
eb59817e 3199}
d404e92d 3200
8a29fbc8 3201__calcAccountKeyHash() {
ca7202eb 3202 [ -f "$ACCOUNT_KEY_PATH" ] && _digest sha256 <"$ACCOUNT_KEY_PATH"
8a29fbc8 3203}
3204
339a8ad6 3205__calc_account_thumbprint() {
3206 printf "%s" "$jwk" | tr -d ' ' | _digest "sha256" | _url_replace
3207}
3208
57e58ce7 3209#keylength
d404e92d 3210_regAccount() {
3211 _initpath
57e58ce7 3212 _reg_length="$1"
f87890cb 3213 _debug3 _regAccount "$_regAccount"
c1151b0d 3214 _initAPI
3215
1bbc33a0 3216 mkdir -p "$CA_DIR"
5c48e139 3217 if [ ! -f "$ACCOUNT_KEY_PATH" ] && [ -f "$_OLD_ACCOUNT_KEY" ]; then
3218 _info "mv $_OLD_ACCOUNT_KEY to $ACCOUNT_KEY_PATH"
3219 mv "$_OLD_ACCOUNT_KEY" "$ACCOUNT_KEY_PATH"
3220 fi
4c2a3841 3221
5c48e139 3222 if [ ! -f "$ACCOUNT_JSON_PATH" ] && [ -f "$_OLD_ACCOUNT_JSON" ]; then
3223 _info "mv $_OLD_ACCOUNT_JSON to $ACCOUNT_JSON_PATH"
3224 mv "$_OLD_ACCOUNT_JSON" "$ACCOUNT_JSON_PATH"
3225 fi
4c2a3841 3226
3227 if [ ! -f "$ACCOUNT_KEY_PATH" ]; then
3228 if ! _create_account_key "$_reg_length"; then
d404e92d 3229 _err "Create account key error."
3230 return 1
3231 fi
3232 fi
4c2a3841 3233
3234 if ! _calcjwk "$ACCOUNT_KEY_PATH"; then
d404e92d 3235 return 1
3236 fi
f8d22c48 3237
c1151b0d 3238 if [ "$ACME_VERSION" = "2" ]; then
3239 regjson='{"termsOfServiceAgreed": true}'
3240 if [ "$ACCOUNT_EMAIL" ]; then
3241 regjson='{"contact": ["mailto: '$ACCOUNT_EMAIL'"], "termsOfServiceAgreed": true}'
3242 fi
3243 else
3244 _reg_res="$ACME_NEW_ACCOUNT_RES"
3245 regjson='{"resource": "'$_reg_res'", "terms-of-service-agreed": true, "agreement": "'$ACME_AGREEMENT'"}'
3246 if [ "$ACCOUNT_EMAIL" ]; then
3247 regjson='{"resource": "'$_reg_res'", "contact": ["mailto: '$ACCOUNT_EMAIL'"], "terms-of-service-agreed": true, "agreement": "'$ACME_AGREEMENT'"}'
3248 fi
f87890cb 3249 fi
4c2a3841 3250
ca7ebd93 3251 _info "Registering account"
d404e92d 3252
f87890cb 3253 if ! _send_signed_request "${ACME_NEW_ACCOUNT}" "$regjson"; then
3254 _err "Register account Error: $response"
3255 return 1
3256 fi
d404e92d 3257
f87890cb 3258 if [ "$code" = "" ] || [ "$code" = '201' ]; then
3259 echo "$response" >"$ACCOUNT_JSON_PATH"
3260 _info "Registered"
7df20e50 3261 elif [ "$code" = '409' ] || [ "$code" = '200' ]; then
f87890cb 3262 _info "Already registered"
3263 else
3264 _err "Register account Error: $response"
3265 return 1
3266 fi
d404e92d 3267
5d8d217a 3268 _debug2 responseHeaders "$responseHeaders"
f87890cb 3269 _accUri="$(echo "$responseHeaders" | grep "^Location:" | _head_n 1 | cut -d ' ' -f 2 | tr -d "\r\n")"
3270 _debug "_accUri" "$_accUri"
7e0b334b 3271 if [ -z "$_accUri" ]; then
3272 _err "Can not find account id url."
3273 _err "$responseHeaders"
3274 return 1
3275 fi
f87890cb 3276 _savecaconf "ACCOUNT_URL" "$_accUri"
7e0b334b 3277 export ACCOUNT_URL="$_accUri"
d404e92d 3278
f87890cb 3279 CA_KEY_HASH="$(__calcAccountKeyHash)"
3280 _debug "Calc CA_KEY_HASH" "$CA_KEY_HASH"
3281 _savecaconf CA_KEY_HASH "$CA_KEY_HASH"
d404e92d 3282
f87890cb 3283 if [ "$code" = '403' ]; then
3284 _err "It seems that the account key is already deactivated, please use a new account key."
3285 return 1
3286 fi
4c2a3841 3287
f87890cb 3288 ACCOUNT_THUMBPRINT="$(__calc_account_thumbprint)"
3289 _info "ACCOUNT_THUMBPRINT" "$ACCOUNT_THUMBPRINT"
d404e92d 3290}
3291
422dd1fa 3292#Implement deactivate account
3293deactivateaccount() {
3294 _initpath
3295
3296 if [ ! -f "$ACCOUNT_KEY_PATH" ] && [ -f "$_OLD_ACCOUNT_KEY" ]; then
422dd1fa 3297 _info "mv $_OLD_ACCOUNT_KEY to $ACCOUNT_KEY_PATH"
3298 mv "$_OLD_ACCOUNT_KEY" "$ACCOUNT_KEY_PATH"
3299 fi
3300
3301 if [ ! -f "$ACCOUNT_JSON_PATH" ] && [ -f "$_OLD_ACCOUNT_JSON" ]; then
422dd1fa 3302 _info "mv $_OLD_ACCOUNT_JSON to $ACCOUNT_JSON_PATH"
3303 mv "$_OLD_ACCOUNT_JSON" "$ACCOUNT_JSON_PATH"
3304 fi
3305
3306 if [ ! -f "$ACCOUNT_KEY_PATH" ]; then
3307 _err "Account key is not found at: $ACCOUNT_KEY_PATH"
3308 return 1
3309 fi
3310
3311 _accUri=$(_readcaconf "ACCOUNT_URL")
3312 _debug _accUri "$_accUri"
3313
3314 if [ -z "$_accUri" ]; then
3315 _err "The account url is empty, please run '--update-account' first to update the account info first,"
3316 _err "Then try again."
3317 return 1
3318 fi
3319
3320 if ! _calcjwk "$ACCOUNT_KEY_PATH"; then
3321 return 1
3322 fi
3323 _initAPI
3324
d2cde379 3325 if [ "$ACME_VERSION" = "2" ]; then
3326 _djson="{\"status\":\"deactivated\"}"
3327 else
3328 _djson="{\"resource\": \"reg\", \"status\":\"deactivated\"}"
3329 fi
3330 if _send_signed_request "$_accUri" "$_djson" && _contains "$response" '"deactivated"'; then
422dd1fa 3331 _info "Deactivate account success for $_accUri."
3332 _accid=$(echo "$response" | _egrep_o "\"id\" *: *[^,]*," | cut -d : -f 2 | tr -d ' ,')
3333 elif [ "$code" = "403" ]; then
3334 _info "The account is already deactivated."
3335 _accid=$(_getfield "$_accUri" "999" "/")
3336 else
3337 _err "Deactivate: account failed for $_accUri."
3338 return 1
3339 fi
3340
3341 _debug "Account id: $_accid"
3342 if [ "$_accid" ]; then
3343 _deactivated_account_path="$CA_DIR/deactivated/$_accid"
3344 _debug _deactivated_account_path "$_deactivated_account_path"
3345 if mkdir -p "$_deactivated_account_path"; then
3346 _info "Moving deactivated account info to $_deactivated_account_path/"
3347 mv "$CA_CONF" "$_deactivated_account_path/"
3348 mv "$ACCOUNT_JSON_PATH" "$_deactivated_account_path/"
3349 mv "$ACCOUNT_KEY_PATH" "$_deactivated_account_path/"
3350 else
3351 _err "Can not create dir: $_deactivated_account_path, try to remove the deactivated account key."
3352 rm -f "$CA_CONF"
3353 rm -f "$ACCOUNT_JSON_PATH"
3354 rm -f "$ACCOUNT_KEY_PATH"
3355 fi
3356 fi
3357}
3358
a61fe418 3359# domain folder file
3360_findHook() {
3361 _hookdomain="$1"
3362 _hookcat="$2"
3363 _hookname="$3"
3364
c7b16249 3365 if [ -f "$_SCRIPT_HOME/$_hookcat/$_hookname" ]; then
3366 d_api="$_SCRIPT_HOME/$_hookcat/$_hookname"
3367 elif [ -f "$_SCRIPT_HOME/$_hookcat/$_hookname.sh" ]; then
3368 d_api="$_SCRIPT_HOME/$_hookcat/$_hookname.sh"
4c2a3841 3369 elif [ -f "$LE_WORKING_DIR/$_hookdomain/$_hookname" ]; then
a61fe418 3370 d_api="$LE_WORKING_DIR/$_hookdomain/$_hookname"
4c2a3841 3371 elif [ -f "$LE_WORKING_DIR/$_hookdomain/$_hookname.sh" ]; then
a61fe418 3372 d_api="$LE_WORKING_DIR/$_hookdomain/$_hookname.sh"
4c2a3841 3373 elif [ -f "$LE_WORKING_DIR/$_hookname" ]; then
a61fe418 3374 d_api="$LE_WORKING_DIR/$_hookname"
4c2a3841 3375 elif [ -f "$LE_WORKING_DIR/$_hookname.sh" ]; then
a61fe418 3376 d_api="$LE_WORKING_DIR/$_hookname.sh"
4c2a3841 3377 elif [ -f "$LE_WORKING_DIR/$_hookcat/$_hookname" ]; then
a61fe418 3378 d_api="$LE_WORKING_DIR/$_hookcat/$_hookname"
4c2a3841 3379 elif [ -f "$LE_WORKING_DIR/$_hookcat/$_hookname.sh" ]; then
a61fe418 3380 d_api="$LE_WORKING_DIR/$_hookcat/$_hookname.sh"
3381 fi
3382
3383 printf "%s" "$d_api"
3384}
3385
f940b2a5 3386#domain
3387__get_domain_new_authz() {
3388 _gdnd="$1"
3389 _info "Getting new-authz for domain" "$_gdnd"
40ef86f4 3390 _initAPI
f940b2a5 3391 _Max_new_authz_retry_times=5
3392 _authz_i=0
4c2a3841 3393 while [ "$_authz_i" -lt "$_Max_new_authz_retry_times" ]; do
efd96153 3394 _debug "Try new-authz for the $_authz_i time."
48d9a8c1 3395 if ! _send_signed_request "${ACME_NEW_AUTHZ}" "{\"resource\": \"new-authz\", \"identifier\": {\"type\": \"dns\", \"value\": \"$(_idn "$_gdnd")\"}}"; then
f940b2a5 3396 _err "Can not get domain new authz."
3397 return 1
3398 fi
5413bf87 3399 if _contains "$response" "No registration exists matching provided key"; then
3400 _err "It seems there is an error, but it's recovered now, please try again."
3401 _err "If you see this message for a second time, please report bug: $(__green "$PROJECT")"
3402 _clearcaconf "CA_KEY_HASH"
3403 break
3404 fi
4c2a3841 3405 if ! _contains "$response" "An error occurred while processing your request"; then
f940b2a5 3406 _info "The new-authz request is ok."
3407 break
3408 fi
3409 _authz_i="$(_math "$_authz_i" + 1)"
9e45ac93 3410 _info "The server is busy, Sleep $_authz_i to retry."
f940b2a5 3411 _sleep "$_authz_i"
4c2a3841 3412 done
f940b2a5 3413
4c2a3841 3414 if [ "$_authz_i" = "$_Max_new_authz_retry_times" ]; then
efd96153 3415 _err "new-authz retry reach the max $_Max_new_authz_retry_times times."
f940b2a5 3416 fi
4c2a3841 3417
78915896 3418 if [ "$code" ] && [ "$code" != '201' ]; then
f940b2a5 3419 _err "new-authz error: $response"
3420 return 1
3421 fi
3422
3423}
3424
58e4d337 3425#uri keyAuthorization
f94433e5 3426__trigger_validation() {
58e4d337 3427 _debug2 "tigger domain validation."
3428 _t_url="$1"
3429 _debug2 _t_url "$_t_url"
3430 _t_key_authz="$2"
3431 _debug2 _t_key_authz "$_t_key_authz"
c1151b0d 3432 if [ "$ACME_VERSION" = "2" ]; then
3433 _send_signed_request "$_t_url" "{\"keyAuthorization\": \"$_t_key_authz\"}"
3434 else
3435 _send_signed_request "$_t_url" "{\"resource\": \"challenge\", \"keyAuthorization\": \"$_t_key_authz\"}"
3436 fi
58e4d337 3437}
3438
3c07f57a 3439#webroot, domain domainlist keylength
4c3b3608 3440issue() {
4c2a3841 3441 if [ -z "$2" ]; then
43822d37 3442 _usage "Usage: $PROJECT_ENTRY --issue -d a.com -w /path/to/webroot/a.com/ "
4c3b3608 3443 return 1
3444 fi
49d75a0c 3445 if [ -z "$1" ]; then
3446 _usage "Please specify at least one validation method: '--webroot', '--standalone', '--apache', '--nginx' or '--dns' etc."
3447 return 1
3448 fi
af1cc3b3 3449 _web_roots="$1"
3450 _main_domain="$2"
02140ce7 3451 _alt_domains="$3"
d2cde379 3452
af1cc3b3 3453 if _contains "$_main_domain" ","; then
3454 _main_domain=$(echo "$2,$3" | cut -d , -f 1)
02140ce7 3455 _alt_domains=$(echo "$2,$3" | cut -d , -f 2- | sed "s/,${NO_VALUE}$//")
2aff36e7 3456 fi
674b5088 3457 _debug _main_domain "$_main_domain"
3458 _debug _alt_domains "$_alt_domains"
3459
d9c9114b 3460 _key_length="$4"
85e1f4ea 3461 _real_cert="$5"
3462 _real_key="$6"
3463 _real_ca="$7"
3464 _reload_cmd="$8"
3465 _real_fullchain="$9"
3466 _pre_hook="${10}"
3467 _post_hook="${11}"
3468 _renew_hook="${12}"
3469 _local_addr="${13}"
875625b1 3470 _challenge_alias="${14}"
eccec5f6 3471 #remove these later.
af1cc3b3 3472 if [ "$_web_roots" = "dns-cf" ]; then
3473 _web_roots="dns_cf"
eccec5f6 3474 fi
af1cc3b3 3475 if [ "$_web_roots" = "dns-dp" ]; then
3476 _web_roots="dns_dp"
eccec5f6 3477 fi
af1cc3b3 3478 if [ "$_web_roots" = "dns-cx" ]; then
3479 _web_roots="dns_cx"
eccec5f6 3480 fi
4c2a3841 3481
3482 if [ ! "$IS_RENEW" ]; then
d9c9114b 3483 _initpath "$_main_domain" "$_key_length"
43822d37 3484 mkdir -p "$DOMAIN_PATH"
3485 fi
eccec5f6 3486
a0923622 3487 if _hasfield "$_web_roots" "$W_DNS" && [ -z "$FORCE_DNS_MANUAL" ]; then
3488 _err "$_DNS_MANUAL_ERROR"
3489 return 1
3490 fi
3491
48d9a8c1 3492 _debug "Using ACME_DIRECTORY: $ACME_DIRECTORY"
3493
3494 _initAPI
3495
4c2a3841 3496 if [ -f "$DOMAIN_CONF" ]; then
61623d22 3497 Le_NextRenewTime=$(_readdomainconf Le_NextRenewTime)
a4270efa 3498 _debug Le_NextRenewTime "$Le_NextRenewTime"
95e06de5 3499 if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(_time)" -lt "$Le_NextRenewTime" ]; then
bb25febd 3500 _saved_domain=$(_readdomainconf Le_Domain)
3501 _debug _saved_domain "$_saved_domain"
3502 _saved_alt=$(_readdomainconf Le_Alt)
3503 _debug _saved_alt "$_saved_alt"
02140ce7 3504 if [ "$_saved_domain,$_saved_alt" = "$_main_domain,$_alt_domains" ]; then
bb25febd 3505 _info "Domains not changed."
3506 _info "Skip, Next renewal time is: $(__green "$(_readdomainconf Le_NextRenewTimeStr)")"
4c2a3841 3507 _info "Add '$(__red '--force')' to force to renew."
bb25febd 3508 return $RENEW_SKIP
3509 else
3510 _info "Domains have changed."
3511 fi
4c3b3608 3512 fi
3513 fi
96a46cfc 3514
af1cc3b3 3515 _savedomainconf "Le_Domain" "$_main_domain"
02140ce7 3516 _savedomainconf "Le_Alt" "$_alt_domains"
af1cc3b3 3517 _savedomainconf "Le_Webroot" "$_web_roots"
4c2a3841 3518
85e1f4ea 3519 _savedomainconf "Le_PreHook" "$_pre_hook"
3520 _savedomainconf "Le_PostHook" "$_post_hook"
3521 _savedomainconf "Le_RenewHook" "$_renew_hook"
4c2a3841 3522
85e1f4ea 3523 if [ "$_local_addr" ]; then
3524 _savedomainconf "Le_LocalAddress" "$_local_addr"
72518d48 3525 else
3526 _cleardomainconf "Le_LocalAddress"
3527 fi
875625b1 3528 if [ "$_challenge_alias" ]; then
3529 _savedomainconf "Le_ChallengeAlias" "$_challenge_alias"
3530 else
3531 _cleardomainconf "Le_ChallengeAlias"
3532 fi
6ae0f7f5 3533
48d9a8c1 3534 Le_API="$ACME_DIRECTORY"
f6dcd989 3535 _savedomainconf "Le_API" "$Le_API"
4c2a3841 3536
02140ce7 3537 if [ "$_alt_domains" = "$NO_VALUE" ]; then
3538 _alt_domains=""
4c3b3608 3539 fi
4c2a3841 3540
d9c9114b 3541 if [ "$_key_length" = "$NO_VALUE" ]; then
3542 _key_length=""
d404e92d 3543 fi
4c2a3841 3544
85e1f4ea 3545 if ! _on_before_issue "$_web_roots" "$_main_domain" "$_alt_domains" "$_pre_hook" "$_local_addr"; then
0463b5d6 3546 _err "_on_before_issue."
3547 return 1
4c3b3608 3548 fi
0463b5d6 3549
8a29fbc8 3550 _saved_account_key_hash="$(_readcaconf "CA_KEY_HASH")"
3551 _debug2 _saved_account_key_hash "$_saved_account_key_hash"
4c2a3841 3552
e8b54a50 3553 if [ -z "$ACCOUNT_URL" ] || [ -z "$_saved_account_key_hash" ] || [ "$_saved_account_key_hash" != "$(__calcAccountKeyHash)" ]; then
57e58ce7 3554 if ! _regAccount "$_accountkeylength"; then
85e1f4ea 3555 _on_issue_err "$_post_hook"
8a29fbc8 3556 return 1
3557 fi
57e58ce7 3558 else
3559 _debug "_saved_account_key_hash is not changed, skip register account."
166096dc 3560 fi
166096dc 3561
4c2a3841 3562 if [ -f "$CSR_PATH" ] && [ ! -f "$CERT_KEY_PATH" ]; then
10afcaca 3563 _info "Signing from existing CSR."
3564 else
3565 _key=$(_readdomainconf Le_Keylength)
3566 _debug "Read key length:$_key"
c4b2e582 3567 if [ ! -f "$CERT_KEY_PATH" ] || [ "$_key_length" != "$_key" ] || [ "$Le_ForceNewDomainKey" = "1" ]; then
d9c9114b 3568 if ! createDomainKey "$_main_domain" "$_key_length"; then
10afcaca 3569 _err "Create domain key error."
3570 _clearup
85e1f4ea 3571 _on_issue_err "$_post_hook"
10afcaca 3572 return 1
3573 fi
3574 fi
3575
02140ce7 3576 if ! _createcsr "$_main_domain" "$_alt_domains" "$CERT_KEY_PATH" "$CSR_PATH" "$DOMAIN_SSL_CONF"; then
10afcaca 3577 _err "Create CSR error."
5ef501c5 3578 _clearup
85e1f4ea 3579 _on_issue_err "$_post_hook"
41e3eafa 3580 return 1
3581 fi
4c3b3608 3582 fi
10afcaca 3583
d9c9114b 3584 _savedomainconf "Le_Keylength" "$_key_length"
4c2a3841 3585
4c3b3608 3586 vlist="$Le_Vlist"
cae203be 3587
3588 _info "Getting domain auth token for each domain"
4c3b3608 3589 sep='#'
9d725af6 3590 dvsep=','
4c2a3841 3591 if [ -z "$vlist" ]; then
d2cde379 3592 if [ "$ACME_VERSION" = "2" ]; then
c1151b0d 3593 #make new order request
3594 _identifiers="{\"type\":\"dns\",\"value\":\"$_main_domain\"}"
38f1b4d2 3595 _w_index=1
674b5088 3596 while true; do
dd17124e 3597 d="$(echo "$_alt_domains," | cut -d , -f "$_w_index")"
674b5088 3598 _w_index="$(_math "$_w_index" + 1)"
3599 _debug d "$d"
3600 if [ -z "$d" ]; then
3601 break
c1151b0d 3602 fi
674b5088 3603 _identifiers="$_identifiers,{\"type\":\"dns\",\"value\":\"$d\"}"
c1151b0d 3604 done
3605 _debug2 _identifiers "$_identifiers"
3606 if ! _send_signed_request "$ACME_NEW_ORDER" "{\"identifiers\": [$_identifiers]}"; then
3607 _err "Create new order error."
3608 _clearup
3609 _on_issue_err "$_post_hook"
3610 return 1
3611 fi
3612
d2cde379 3613 Le_OrderFinalize="$(echo "$response" | tr -d '\r\n' | _egrep_o '"finalize" *: *"[^"]*"' | cut -d '"' -f 4)"
3614 _debug Le_OrderFinalize "$Le_OrderFinalize"
3615 if [ -z "$Le_OrderFinalize" ]; then
78915896 3616 _err "Create new order error. Le_OrderFinalize not found. $response"
c1151b0d 3617 _clearup
3618 _on_issue_err "$_post_hook"
3619 return 1
3620 fi
3621
3622 #for dns manual mode
d2cde379 3623 _savedomainconf "Le_OrderFinalize" "$Le_OrderFinalize"
c1151b0d 3624
f8d22c48 3625 _authorizations_seg="$(echo "$response" | tr -d '\r\n' | _egrep_o '"authorizations" *: *\[[^\]*\]' | cut -d '[' -f 2 | tr -d ']' | tr -d '"')"
c1151b0d 3626 _debug2 _authorizations_seg "$_authorizations_seg"
3627 if [ -z "$_authorizations_seg" ]; then
3628 _err "_authorizations_seg not found."
3629 _clearup
3630 _on_issue_err "$_post_hook"
3631 return 1
3632 fi
3633
3634 #domain and authz map
3635 _authorizations_map=""
f8d22c48 3636 for _authz_url in $(echo "$_authorizations_seg" | tr ',' ' '); do
c1151b0d 3637 _debug2 "_authz_url" "$_authz_url"
3638 if ! response="$(_get "$_authz_url")"; then
3639 _err "get to authz error."
263c38ca 3640 _err "_authorizations_seg" "$_authorizations_seg"
3641 _err "_authz_url" "$_authz_url"
c1151b0d 3642 _clearup
3643 _on_issue_err "$_post_hook"
3644 return 1
3645 fi
3646
3647 response="$(echo "$response" | _normalizeJson)"
3648 _debug2 response "$response"
3649 _d="$(echo "$response" | _egrep_o '"value" *: *"[^"]*"' | cut -d : -f 2 | tr -d ' "')"
72f54ca6 3650 if _contains "$response" "\"wildcard\" *: *true"; then
3651 _d="*.$_d"
3652 fi
c1151b0d 3653 _debug2 _d "$_d"
3654 _authorizations_map="$_d,$response
3655$_authorizations_map"
3656 done
3657 _debug2 _authorizations_map "$_authorizations_map"
3658 fi
3659
c1151b0d 3660 _index=0
a63b05a9 3661 _currentRoot=""
38f1b4d2 3662 _w_index=1
88bbe55b 3663 while true; do
88bbe55b 3664 d="$(echo "$_main_domain,$_alt_domains," | cut -d , -f "$_w_index")"
3665 _w_index="$(_math "$_w_index" + 1)"
3666 _debug d "$d"
3667 if [ -z "$d" ]; then
3668 break
3669 fi
ca7202eb 3670 _info "Getting webroot for domain" "$d"
c1151b0d 3671 _index=$(_math $_index + 1)
af1cc3b3 3672 _w="$(echo $_web_roots | cut -d , -f $_index)"
9d725af6 3673 _debug _w "$_w"
4c2a3841 3674 if [ "$_w" ]; then
a63b05a9 3675 _currentRoot="$_w"
3676 fi
3677 _debug "_currentRoot" "$_currentRoot"
4c2a3841 3678
a63b05a9 3679 vtype="$VTYPE_HTTP"
c1151b0d 3680 #todo, v2 wildcard force to use dns
3881f221 3681 if _startswith "$_currentRoot" "$W_DNS"; then
a63b05a9 3682 vtype="$VTYPE_DNS"
3683 fi
4c2a3841 3684
3685 if [ "$_currentRoot" = "$W_TLS" ]; then
c1151b0d 3686 if [ "$ACME_VERSION" = "2" ]; then
3687 vtype="$VTYPE_TLS2"
3688 else
3689 vtype="$VTYPE_TLS"
3690 fi
e22bcf7c 3691 fi
c4d8fd83 3692
c1151b0d 3693 if [ "$ACME_VERSION" = "2" ]; then
3694 response="$(echo "$_authorizations_map" | grep "^$d," | sed "s/$d,//")"
3695 _debug2 "response" "$response"
3696 if [ -z "$response" ]; then
3697 _err "get to authz error."
263c38ca 3698 _err "_authorizations_map" "$_authorizations_map"
c1151b0d 3699 _clearup
3700 _on_issue_err "$_post_hook"
3701 return 1
3702 fi
3703 else
3704 if ! __get_domain_new_authz "$d"; then
3705 _clearup
3706 _on_issue_err "$_post_hook"
3707 return 1
3708 fi
c4d8fd83 3709 fi
3710
4c2a3841 3711 if [ -z "$thumbprint" ]; then
339a8ad6 3712 thumbprint="$(__calc_account_thumbprint)"
4c3b3608 3713 fi
3714
4c2a3841 3715 entry="$(printf "%s\n" "$response" | _egrep_o '[^\{]*"type":"'$vtype'"[^\}]*')"
4c3b3608 3716 _debug entry "$entry"
4c2a3841 3717 if [ -z "$entry" ]; then
584fb290 3718 _err "Error, can not get domain token entry $d"
a63766a0 3719 _supported_vtypes="$(echo "$response" | _egrep_o "\"challenges\":\[[^]]*]" | tr '{' "\n" | grep type | cut -d '"' -f 4 | tr "\n" ' ')"
b51ed9bb 3720 if [ "$_supported_vtypes" ]; then
3721 _err "The supported validation types are: $_supported_vtypes, but you specified: $vtype"
3722 fi
19539575 3723 _clearup
85e1f4ea 3724 _on_issue_err "$_post_hook"
19539575 3725 return 1
3726 fi
22ea4004 3727 token="$(printf "%s\n" "$entry" | _egrep_o '"token":"[^"]*' | cut -d : -f 2 | tr -d '"')"
ca7202eb 3728 _debug token "$token"
4c2a3841 3729
584fb290 3730 if [ -z "$token" ]; then
3731 _err "Error, can not get domain token $entry"
3732 _clearup
3733 _on_issue_err "$_post_hook"
3734 return 1
3735 fi
c1151b0d 3736 if [ "$ACME_VERSION" = "2" ]; then
3737 uri="$(printf "%s\n" "$entry" | _egrep_o '"url":"[^"]*' | cut -d '"' -f 4 | _head_n 1)"
3738 else
3739 uri="$(printf "%s\n" "$entry" | _egrep_o '"uri":"[^"]*' | cut -d '"' -f 4)"
3740 fi
ca7202eb 3741 _debug uri "$uri"
cae203be 3742
584fb290 3743 if [ -z "$uri" ]; then
3744 _err "Error, can not get domain uri. $entry"
3745 _clearup
3746 _on_issue_err "$_post_hook"
3747 return 1
3748 fi
4c3b3608 3749 keyauthorization="$token.$thumbprint"
3750 _debug keyauthorization "$keyauthorization"
3751
95e06de5 3752 if printf "%s" "$response" | grep '"status":"valid"' >/dev/null 2>&1; then
c1151b0d 3753 _debug "$d is already verified."
ca7202eb 3754 keyauthorization="$STATE_VERIFIED"
d35bf517 3755 _debug keyauthorization "$keyauthorization"
ec603bee 3756 fi
3757
a63b05a9 3758 dvlist="$d$sep$keyauthorization$sep$uri$sep$vtype$sep$_currentRoot"
4c3b3608 3759 _debug dvlist "$dvlist"
4c2a3841 3760
9d725af6 3761 vlist="$vlist$dvlist$dvsep"
4c3b3608 3762
3763 done
9d725af6 3764 _debug vlist "$vlist"
4c3b3608 3765 #add entry
3766 dnsadded=""
9d725af6 3767 ventries=$(echo "$vlist" | tr "$dvsep" ' ')
1f7df33e 3768 _alias_index=1
4c2a3841 3769 for ventry in $ventries; do
ca7202eb 3770 d=$(echo "$ventry" | cut -d "$sep" -f 1)
3771 keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2)
3772 vtype=$(echo "$ventry" | cut -d "$sep" -f 4)
3773 _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5)
72f54ca6 3774 _debug d "$d"
4c2a3841 3775 if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
69212114 3776 _debug "$d is already verified, skip $vtype."
ec603bee 3777 continue
3778 fi
3779
4c2a3841 3780 if [ "$vtype" = "$VTYPE_DNS" ]; then
4c3b3608 3781 dnsadded='0'
72f54ca6 3782 _dns_root_d="$d"
3783 if _startswith "$_dns_root_d" "*."; then
3784 _dns_root_d="$(echo "$_dns_root_d" | sed 's/*.//')"
3785 fi
875625b1 3786 _d_alias="$(_getfield "$_challenge_alias" "$_alias_index")"
3787 _alias_index="$(_math "$_alias_index" + 1)"
3788 _debug "_d_alias" "$_d_alias"
3789 if [ "$_d_alias" ]; then
64821ad4 3790 if _startswith "$_d_alias" "$DNS_ALIAS_PREFIX"; then
3791 txtdomain="$(echo "$_d_alias" | sed "s/$DNS_ALIAS_PREFIX//")"
3792 else
3793 txtdomain="_acme-challenge.$_d_alias"
3794 fi
875625b1 3795 else
3796 txtdomain="_acme-challenge.$_dns_root_d"
3797 fi
4c3b3608 3798 _debug txtdomain "$txtdomain"
11927a76 3799 txt="$(printf "%s" "$keyauthorization" | _digest "sha256" | _url_replace)"
4c3b3608 3800 _debug txt "$txt"
a61fe418 3801
72f54ca6 3802 d_api="$(_findHook "$_dns_root_d" dnsapi "$_currentRoot")"
a61fe418 3803
4c3b3608 3804 _debug d_api "$d_api"
4c2a3841 3805
3806 if [ "$d_api" ]; then
4c3b3608 3807 _info "Found domain api file: $d_api"
3808 else
3881f221 3809 if [ "$_currentRoot" != "$W_DNS" ]; then
3810 _err "Can not find dns api hook for: $_currentRoot"
3811 _info "You need to add the txt record manually."
3812 fi
5f8b60a0 3813 _info "$(__red "Add the following TXT record:")"
81772fb7 3814 _info "$(__red "Domain: '$(__green "$txtdomain")'")"
3815 _info "$(__red "TXT value: '$(__green "$txt")'")"
3816 _info "$(__red "Please be aware that you prepend _acme-challenge. before your domain")"
3817 _info "$(__red "so the resulting subdomain will be: $txtdomain")"
4c3b3608 3818 continue
3819 fi
4c2a3841 3820
73b8b120 3821 (
ca7202eb 3822 if ! . "$d_api"; then
73b8b120 3823 _err "Load file $d_api error. Please check your api file and try again."
3824 return 1
3825 fi
4c2a3841 3826
158f22f7 3827 addcommand="${_currentRoot}_add"
ca7202eb 3828 if ! _exists "$addcommand"; then
73b8b120 3829 _err "It seems that your api file is not correct, it must have a function named: $addcommand"
3830 return 1
3831 fi
4c2a3841 3832
ca7202eb 3833 if ! $addcommand "$txtdomain" "$txt"; then
73b8b120 3834 _err "Error add txt for domain:$txtdomain"
3835 return 1
3836 fi
3837 )
4c2a3841 3838
3839 if [ "$?" != "0" ]; then
5ef501c5 3840 _clearup
ea722da3 3841 _on_issue_err "$_post_hook" "$vlist"
4c3b3608 3842 return 1
3843 fi
3844 dnsadded='1'
3845 fi
3846 done
3847
4c2a3841 3848 if [ "$dnsadded" = '0' ]; then
3849 _savedomainconf "Le_Vlist" "$vlist"
4c3b3608 3850 _debug "Dns record not added yet, so, save to $DOMAIN_CONF and exit."
a8b62261 3851 _err "Please add the TXT records to the domains, and re-run with --renew."
5ef501c5 3852 _clearup
5f8b60a0 3853 _on_issue_err "$_post_hook"
4c3b3608 3854 return 1
3855 fi
4c2a3841 3856
4c3b3608 3857 fi
4c2a3841 3858
3859 if [ "$dnsadded" = '1' ]; then
3860 if [ -z "$Le_DNSSleep" ]; then
ca7202eb 3861 Le_DNSSleep="$DEFAULT_DNS_SLEEP"
0e38c60d 3862 else
4c2a3841 3863 _savedomainconf "Le_DNSSleep" "$Le_DNSSleep"
0e38c60d 3864 fi
3865
5fbc47eb 3866 _info "Sleep $(__green $Le_DNSSleep) seconds for the txt records to take effect"
ca7202eb 3867 _sleep "$Le_DNSSleep"
4c3b3608 3868 fi
4c2a3841 3869
5d943a35 3870 NGINX_RESTORE_VLIST=""
4c3b3608 3871 _debug "ok, let's start to verify"
a63b05a9 3872
0463b5d6 3873 _ncIndex=1
9d725af6 3874 ventries=$(echo "$vlist" | tr "$dvsep" ' ')
4c2a3841 3875 for ventry in $ventries; do
ca7202eb 3876 d=$(echo "$ventry" | cut -d "$sep" -f 1)
3877 keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2)
3878 uri=$(echo "$ventry" | cut -d "$sep" -f 3)
3879 vtype=$(echo "$ventry" | cut -d "$sep" -f 4)
3880 _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5)
ec603bee 3881
4c2a3841 3882 if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
ec603bee 3883 _info "$d is already verified, skip $vtype."
3884 continue
3885 fi
3886
4c3b3608 3887 _info "Verifying:$d"
3888 _debug "d" "$d"
3889 _debug "keyauthorization" "$keyauthorization"
3890 _debug "uri" "$uri"
3891 removelevel=""
e22bcf7c 3892 token="$(printf "%s" "$keyauthorization" | cut -d '.' -f 1)"
a63b05a9 3893
3894 _debug "_currentRoot" "$_currentRoot"
3895
4c2a3841 3896 if [ "$vtype" = "$VTYPE_HTTP" ]; then
3897 if [ "$_currentRoot" = "$NO_VALUE" ]; then
4c3b3608 3898 _info "Standalone mode server"
85e1f4ea 3899 _ncaddr="$(_getfield "$_local_addr" "$_ncIndex")"
0463b5d6 3900 _ncIndex="$(_math $_ncIndex + 1)"
3794b5cb 3901 _startserver "$keyauthorization" "$_ncaddr"
4c2a3841 3902 if [ "$?" != "0" ]; then
5ef501c5 3903 _clearup
58e4d337 3904 _on_issue_err "$_post_hook" "$vlist"
6fc1447f 3905 return 1
3906 fi
5dbf664a 3907 sleep 1
ca7202eb 3908 _debug serverproc "$serverproc"
0e44f587 3909 elif [ "$_currentRoot" = "$MODE_STATELESS" ]; then
3910 _info "Stateless mode for domain:$d"
3911 _sleep 1
9d725af6 3912 elif _startswith "$_currentRoot" "$NGINX"; then
3913 _info "Nginx mode for domain:$d"
3914 #set up nginx server
3915 FOUND_REAL_NGINX_CONF=""
3916 BACKUP_NGINX_CONF=""
3917 if ! _setNginx "$d" "$_currentRoot" "$thumbprint"; then
3918 _clearup
58e4d337 3919 _on_issue_err "$_post_hook" "$vlist"
9d725af6 3920 return 1
03f8d6e9 3921 fi
302c41ed 3922
03f8d6e9 3923 if [ "$FOUND_REAL_NGINX_CONF" ]; then
9d725af6 3924 _realConf="$FOUND_REAL_NGINX_CONF"
3925 _backup="$BACKUP_NGINX_CONF"
3926 _debug _realConf "$_realConf"
5d943a35 3927 NGINX_RESTORE_VLIST="$d$sep$_realConf$sep$_backup$dvsep$NGINX_RESTORE_VLIST"
9d725af6 3928 fi
3929 _sleep 1
4c3b3608 3930 else
4c2a3841 3931 if [ "$_currentRoot" = "apache" ]; then
6f930641 3932 wellknown_path="$ACME_DIR"
3933 else
a63b05a9 3934 wellknown_path="$_currentRoot/.well-known/acme-challenge"
4c2a3841 3935 if [ ! -d "$_currentRoot/.well-known" ]; then
6f930641 3936 removelevel='1'
4c2a3841 3937 elif [ ! -d "$_currentRoot/.well-known/acme-challenge" ]; then
6f930641 3938 removelevel='2'
3939 else
3940 removelevel='3'
3941 fi
4c3b3608 3942 fi
6f930641 3943
4c3b3608 3944 _debug wellknown_path "$wellknown_path"
6f930641 3945
4c3b3608 3946 _debug "writing token:$token to $wellknown_path/$token"
3947
3948 mkdir -p "$wellknown_path"
93fc48a2 3949
4c2a3841 3950 if ! printf "%s" "$keyauthorization" >"$wellknown_path/$token"; then
93fc48a2 3951 _err "$d:Can not write token to file : $wellknown_path/$token"
3952 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
3953 _clearup
58e4d337 3954 _on_issue_err "$_post_hook" "$vlist"
93fc48a2 3955 return 1
3956 fi
3957
4c2a3841 3958 if [ ! "$usingApache" ]; then
44edb2bd 3959 if webroot_owner=$(_stat "$_currentRoot"); then
32fdc196 3960 _debug "Changing owner/group of .well-known to $webroot_owner"
c87cd0de 3961 if ! _exec "chown -R \"$webroot_owner\" \"$_currentRoot/.well-known\""; then
3962 _debug "$(cat "$_EXEC_TEMP_ERR")"
3963 _exec_err >/dev/null 2>&1
3964 fi
32fdc196 3965 else
b54ce310 3966 _debug "not changing owner/group of webroot"
32fdc196 3967 fi
df886ffa 3968 fi
4c2a3841 3969
4c3b3608 3970 fi
4c2a3841 3971
3972 elif [ "$vtype" = "$VTYPE_TLS" ]; then
e22bcf7c 3973 #create A
3974 #_hash_A="$(printf "%s" $token | _digest "sha256" "hex" )"
3975 #_debug2 _hash_A "$_hash_A"
3976 #_x="$(echo $_hash_A | cut -c 1-32)"
3977 #_debug2 _x "$_x"
3978 #_y="$(echo $_hash_A | cut -c 33-64)"
3979 #_debug2 _y "$_y"
3980 #_SAN_A="$_x.$_y.token.acme.invalid"
3981 #_debug2 _SAN_A "$_SAN_A"
4c2a3841 3982
e22bcf7c 3983 #create B
0c538f75 3984 _hash_B="$(printf "%s" "$keyauthorization" | _digest "sha256" "hex")"
e22bcf7c 3985 _debug2 _hash_B "$_hash_B"
0c538f75 3986 _x="$(echo "$_hash_B" | cut -c 1-32)"
e22bcf7c 3987 _debug2 _x "$_x"
0c538f75 3988 _y="$(echo "$_hash_B" | cut -c 33-64)"
e22bcf7c 3989 _debug2 _y "$_y"
4c2a3841 3990
e22bcf7c 3991 #_SAN_B="$_x.$_y.ka.acme.invalid"
4c2a3841 3992
e22bcf7c 3993 _SAN_B="$_x.$_y.acme.invalid"
3994 _debug2 _SAN_B "$_SAN_B"
4c2a3841 3995
85e1f4ea 3996 _ncaddr="$(_getfield "$_local_addr" "$_ncIndex")"
0c538f75 3997 _ncIndex="$(_math "$_ncIndex" + 1)"
0463b5d6 3998 if ! _starttlsserver "$_SAN_B" "$_SAN_A" "$Le_TLSPort" "$keyauthorization" "$_ncaddr"; then
e22bcf7c 3999 _err "Start tls server error."
4000 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4001 _clearup
58e4d337 4002 _on_issue_err "$_post_hook" "$vlist"
e22bcf7c 4003 return 1
4004 fi
4c3b3608 4005 fi
4c2a3841 4006
f94433e5 4007 if ! __trigger_validation "$uri" "$keyauthorization"; then
c4d8fd83 4008 _err "$d:Can not get challenge: $response"
4009 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4010 _clearup
58e4d337 4011 _on_issue_err "$_post_hook" "$vlist"
c4d8fd83 4012 return 1
4013 fi
4c2a3841 4014
c1151b0d 4015 if [ "$code" ] && [ "$code" != '202' ]; then
4016 if [ "$ACME_VERSION" = "2" ] && [ "$code" = '200' ]; then
4017 _debug "trigger validation code: $code"
4018 else
4019 _err "$d:Challenge error: $response"
4020 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4021 _clearup
4022 _on_issue_err "$_post_hook" "$vlist"
4023 return 1
4024 fi
4c3b3608 4025 fi
4c2a3841 4026
6fc1447f 4027 waittimes=0
4c2a3841 4028 if [ -z "$MAX_RETRY_TIMES" ]; then
6fc1447f 4029 MAX_RETRY_TIMES=30
4030 fi
4c2a3841 4031
4032 while true; do
0c538f75 4033 waittimes=$(_math "$waittimes" + 1)
4c2a3841 4034 if [ "$waittimes" -ge "$MAX_RETRY_TIMES" ]; then
6fc1447f 4035 _err "$d:Timeout"
4036 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4037 _clearup
58e4d337 4038 _on_issue_err "$_post_hook" "$vlist"
6fc1447f 4039 return 1
4040 fi
4c2a3841 4041
5dbf664a 4042 _debug "sleep 2 secs to verify"
4043 sleep 2
4c3b3608 4044 _debug "checking"
44edb2bd 4045 response="$(_get "$uri")"
4c2a3841 4046 if [ "$?" != "0" ]; then
c60883ef 4047 _err "$d:Verify error:$response"
a63b05a9 4048 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4c3b3608 4049 _clearup
58e4d337 4050 _on_issue_err "$_post_hook" "$vlist"
4c3b3608 4051 return 1
4052 fi
9aaf36cd 4053 _debug2 original "$response"
4c2a3841 4054
4055 response="$(echo "$response" | _normalizeJson)"
7012b91f 4056 _debug2 response "$response"
4c2a3841 4057
4058 status=$(echo "$response" | _egrep_o '"status":"[^"]*' | cut -d : -f 2 | tr -d '"')
4059 if [ "$status" = "valid" ]; then
93f3098a 4060 _info "$(__green Success)"
ca7202eb 4061 _stopserver "$serverproc"
4c3b3608 4062 serverproc=""
a63b05a9 4063 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4c2a3841 4064 break
4c3b3608 4065 fi
4c2a3841 4066
4067 if [ "$status" = "invalid" ]; then
4068 error="$(echo "$response" | tr -d "\r\n" | _egrep_o '"error":\{[^\}]*')"
4069 _debug2 error "$error"
4070 errordetail="$(echo "$error" | _egrep_o '"detail": *"[^"]*' | cut -d '"' -f 4)"
4071 _debug2 errordetail "$errordetail"
4072 if [ "$errordetail" ]; then
4073 _err "$d:Verify error:$errordetail"
4074 else
4075 _err "$d:Verify error:$error"
4076 fi
4077 if [ "$DEBUG" ]; then
4078 if [ "$vtype" = "$VTYPE_HTTP" ]; then
4079 _debug "Debug: get token url."
4080 _get "http://$d/.well-known/acme-challenge/$token" "" 1
4081 fi
4082 fi
a63b05a9 4083 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4c3b3608 4084 _clearup
58e4d337 4085 _on_issue_err "$_post_hook" "$vlist"
4c2a3841 4086 return 1
4c3b3608 4087 fi
4c2a3841 4088
4089 if [ "$status" = "pending" ]; then
4c3b3608 4090 _info "Pending"
4091 else
4c2a3841 4092 _err "$d:Verify error:$response"
a63b05a9 4093 _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
4c3b3608 4094 _clearup
58e4d337 4095 _on_issue_err "$_post_hook" "$vlist"
4c3b3608 4096 return 1
4097 fi
4c2a3841 4098
4c3b3608 4099 done
4c2a3841 4100
4c3b3608 4101 done
4102
4103 _clearup
4104 _info "Verify finished, start to sign."
11927a76 4105 der="$(_getfile "${CSR_PATH}" "${BEGIN_CSR}" "${END_CSR}" | tr -d "\r\n" | _url_replace)"
4c2a3841 4106
c1151b0d 4107 if [ "$ACME_VERSION" = "2" ]; then
d2cde379 4108 if ! _send_signed_request "${Le_OrderFinalize}" "{\"csr\": \"$der\"}"; then
c1151b0d 4109 _err "Sign failed."
4110 _on_issue_err "$_post_hook"
4111 return 1
4112 fi
4113 if [ "$code" != "200" ]; then
4114 _err "Sign failed, code is not 200."
668c43ab 4115 _err "$response"
c1151b0d 4116 _on_issue_err "$_post_hook"
4117 return 1
4118 fi
f8d22c48 4119 Le_LinkCert="$(echo "$response" | tr -d '\r\n' | _egrep_o '"certificate" *: *"[^"]*"' | cut -d '"' -f 4)"
4c2a3841 4120
f8d22c48 4121 if ! _get "$Le_LinkCert" >"$CERT_PATH"; then
668c43ab 4122 _err "Sign failed, can not download cert:$Le_LinkCert."
4123 _err "$response"
c1151b0d 4124 _on_issue_err "$_post_hook"
4125 return 1
4126 fi
4c3b3608 4127
1c35f46b 4128 if [ "$(grep -- "$BEGIN_CERT" "$CERT_PATH" | wc -l)" -gt "1" ]; then
4129 _debug "Found cert chain"
120cde16 4130 cat "$CERT_PATH" >"$CERT_FULLCHAIN_PATH"
1c35f46b 4131 _end_n="$(grep -n -- "$END_CERT" "$CERT_FULLCHAIN_PATH" | _head_n 1 | cut -d : -f 1)"
4132 _debug _end_n "$_end_n"
120cde16 4133 sed -n "1,${_end_n}p" "$CERT_FULLCHAIN_PATH" >"$CERT_PATH"
1c35f46b 4134 _end_n="$(_math $_end_n + 1)"
120cde16 4135 sed -n "${_end_n},9999p" "$CERT_FULLCHAIN_PATH" >"$CA_CERT_PATH"
1c35f46b 4136 fi
c1151b0d 4137 else
4138 if ! _send_signed_request "${ACME_NEW_ORDER}" "{\"resource\": \"$ACME_NEW_ORDER_RES\", \"csr\": \"$der\"}" "needbase64"; then
323febe8 4139 _err "Sign failed. $response"
c1151b0d 4140 _on_issue_err "$_post_hook"
4141 return 1
4142 fi
4143 _rcert="$response"
36a7a840 4144 Le_LinkCert="$(grep -i '^Location.*$' "$HTTP_HEADER" | _tail_n 1 | tr -d "\r\n" | cut -d " " -f 2)"
4c2a3841 4145 echo "$BEGIN_CERT" >"$CERT_PATH"
4c3b3608 4146
72518d48 4147 #if ! _get "$Le_LinkCert" | _base64 "multiline" >> "$CERT_PATH" ; then
4148 # _debug "Get cert failed. Let's try last response."
3c07f57a 4149 # printf -- "%s" "$_rcert" | _dbase64 "multiline" | _base64 "multiline" >> "$CERT_PATH"
72518d48 4150 #fi
4c2a3841 4151
4152 if ! printf -- "%s" "$_rcert" | _dbase64 "multiline" | _base64 "multiline" >>"$CERT_PATH"; then
72518d48 4153 _debug "Try cert link."
4c2a3841 4154 _get "$Le_LinkCert" | _base64 "multiline" >>"$CERT_PATH"
d404e92d 4155 fi
4156
4c2a3841 4157 echo "$END_CERT" >>"$CERT_PATH"
c1151b0d 4158 fi
4159
4160 _debug "Le_LinkCert" "$Le_LinkCert"
4161 _savedomainconf "Le_LinkCert" "$Le_LinkCert"
4162
183063a2 4163 if [ -z "$Le_LinkCert" ] || ! _checkcert "$CERT_PATH"; then
716f7277 4164 response="$(echo "$response" | _dbase64 "multiline" | tr -d '\0' | _normalizeJson)"
183063a2 4165 _err "Sign failed: $(echo "$response" | _egrep_o '"detail":"[^"]*"')"
4166 _on_issue_err "$_post_hook"
4167 return 1
4168 fi
4169
c1151b0d 4170 if [ "$Le_LinkCert" ]; then
43822d37 4171 _info "$(__green "Cert success.")"
4c3b3608 4172 cat "$CERT_PATH"
5980ebc7 4173
4c2a3841 4174 _info "Your cert is in $(__green " $CERT_PATH ")"
4175
4176 if [ -f "$CERT_KEY_PATH" ]; then
4177 _info "Your cert key is in $(__green " $CERT_KEY_PATH ")"
5980ebc7 4178 fi
4179
4c2a3841 4180 if [ ! "$USER_PATH" ] || [ ! "$IN_CRON" ]; then
281aa349 4181 USER_PATH="$PATH"
4182 _saveaccountconf "USER_PATH" "$USER_PATH"
4183 fi
4c3b3608 4184 fi
4c3b3608 4185
4c2a3841 4186 _cleardomainconf "Le_Vlist"
4187
1c35f46b 4188 if [ "$ACME_VERSION" = "2" ]; then
4189 _debug "v2 chain."
4190 else
183063a2 4191 cp "$CERT_PATH" "$CERT_FULLCHAIN_PATH"
1c35f46b 4192 Le_LinkIssuer=$(grep -i '^Link' "$HTTP_HEADER" | _head_n 1 | cut -d " " -f 2 | cut -d ';' -f 1 | tr -d '<>')
d8ba26e6 4193
1c35f46b 4194 if [ "$Le_LinkIssuer" ]; then
4195 if ! _contains "$Le_LinkIssuer" ":"; then
4196 _info "$(__red "Relative issuer link found.")"
4197 Le_LinkIssuer="$_ACME_SERVER_HOST$Le_LinkIssuer"
d8ba26e6 4198 fi
1c35f46b 4199 _debug Le_LinkIssuer "$Le_LinkIssuer"
4200 _savedomainconf "Le_LinkIssuer" "$Le_LinkIssuer"
d8ba26e6 4201
1c35f46b 4202 _link_issuer_retry=0
4203 _MAX_ISSUER_RETRY=5
4204 while [ "$_link_issuer_retry" -lt "$_MAX_ISSUER_RETRY" ]; do
4205 _debug _link_issuer_retry "$_link_issuer_retry"
4206 if [ "$ACME_VERSION" = "2" ]; then
4207 if _get "$Le_LinkIssuer" >"$CA_CERT_PATH"; then
4208 break
4209 fi
4210 else
4211 if _get "$Le_LinkIssuer" >"$CA_CERT_PATH.der"; then
4212 echo "$BEGIN_CERT" >"$CA_CERT_PATH"
4213 _base64 "multiline" <"$CA_CERT_PATH.der" >>"$CA_CERT_PATH"
4214 echo "$END_CERT" >>"$CA_CERT_PATH"
0f120c41 4215 if ! _checkcert "$CA_CERT_PATH"; then
183063a2 4216 _err "Can not get the ca cert."
4217 break
4218 fi
1c35f46b 4219 cat "$CA_CERT_PATH" >>"$CERT_FULLCHAIN_PATH"
4220 rm -f "$CA_CERT_PATH.der"
4221 break
4222 fi
c1151b0d 4223 fi
1c35f46b 4224 _link_issuer_retry=$(_math $_link_issuer_retry + 1)
4225 _sleep "$_link_issuer_retry"
4226 done
4227 if [ "$_link_issuer_retry" = "$_MAX_ISSUER_RETRY" ]; then
4228 _err "Max retry for issuer ca cert is reached."
d8ba26e6 4229 fi
1c35f46b 4230 else
4231 _debug "No Le_LinkIssuer header found."
d8ba26e6 4232 fi
4c3b3608 4233 fi
1c35f46b 4234 [ -f "$CA_CERT_PATH" ] && _info "The intermediate CA cert is in $(__green " $CA_CERT_PATH ")"
4235 [ -f "$CERT_FULLCHAIN_PATH" ] && _info "And the full chain certs is there: $(__green " $CERT_FULLCHAIN_PATH ")"
4c2a3841 4236
3aae1ae3 4237 Le_CertCreateTime=$(_time)
4c2a3841 4238 _savedomainconf "Le_CertCreateTime" "$Le_CertCreateTime"
4239
4240 Le_CertCreateTimeStr=$(date -u)
4241 _savedomainconf "Le_CertCreateTimeStr" "$Le_CertCreateTimeStr"
4242
4243 if [ -z "$Le_RenewalDays" ] || [ "$Le_RenewalDays" -lt "0" ] || [ "$Le_RenewalDays" -gt "$MAX_RENEW" ]; then
ca7202eb 4244 Le_RenewalDays="$MAX_RENEW"
054cb72e 4245 else
4c2a3841 4246 _savedomainconf "Le_RenewalDays" "$Le_RenewalDays"
13d7cae9 4247 fi
4c2a3841 4248
4249 if [ "$CA_BUNDLE" ]; then
78009539
PS
4250 _saveaccountconf CA_BUNDLE "$CA_BUNDLE"
4251 else
4252 _clearaccountconf "CA_BUNDLE"
4253 fi
4254
2aa75f03 4255 if [ "$CA_PATH" ]; then
4256 _saveaccountconf CA_PATH "$CA_PATH"
4257 else
4258 _clearaccountconf "CA_PATH"
4259 fi
78009539 4260
4c2a3841 4261 if [ "$HTTPS_INSECURE" ]; then
fac1e367 4262 _saveaccountconf HTTPS_INSECURE "$HTTPS_INSECURE"
4263 else
4c2a3841 4264 _clearaccountconf "HTTPS_INSECURE"
13d7cae9 4265 fi
00a50605 4266
4c2a3841 4267 if [ "$Le_Listen_V4" ]; then
4268 _savedomainconf "Le_Listen_V4" "$Le_Listen_V4"
50827188 4269 _cleardomainconf Le_Listen_V6
4c2a3841 4270 elif [ "$Le_Listen_V6" ]; then
4271 _savedomainconf "Le_Listen_V6" "$Le_Listen_V6"
50827188 4272 _cleardomainconf Le_Listen_V4
4273 fi
f6dcd989 4274
c4b2e582 4275 if [ "$Le_ForceNewDomainKey" = "1" ]; then
4276 _savedomainconf "Le_ForceNewDomainKey" "$Le_ForceNewDomainKey"
4277 else
4278 _cleardomainconf Le_ForceNewDomainKey
4279 fi
4280
ca7202eb 4281 Le_NextRenewTime=$(_math "$Le_CertCreateTime" + "$Le_RenewalDays" \* 24 \* 60 \* 60)
4c2a3841 4282
ca7202eb 4283 Le_NextRenewTimeStr=$(_time2str "$Le_NextRenewTime")
4c2a3841 4284 _savedomainconf "Le_NextRenewTimeStr" "$Le_NextRenewTimeStr"
4285
ca7202eb 4286 Le_NextRenewTime=$(_math "$Le_NextRenewTime" - 86400)
4c2a3841 4287 _savedomainconf "Le_NextRenewTime" "$Le_NextRenewTime"
f6dcd989 4288
85e1f4ea 4289 if [ "$_real_cert$_real_key$_real_ca$_reload_cmd$_real_fullchain" ]; then
4290 _savedomainconf "Le_RealCertPath" "$_real_cert"
4291 _savedomainconf "Le_RealCACertPath" "$_real_ca"
4292 _savedomainconf "Le_RealKeyPath" "$_real_key"
4293 _savedomainconf "Le_ReloadCmd" "$_reload_cmd"
4294 _savedomainconf "Le_RealFullChainPath" "$_real_fullchain"
ce8dca7a 4295 if ! _installcert "$_main_domain" "$_real_cert" "$_real_key" "$_real_ca" "$_real_fullchain" "$_reload_cmd"; then
4296 return 1
4297 fi
01f54558 4298 fi
4c0d3f1b 4299
ce8dca7a 4300 if ! _on_issue_success "$_post_hook" "$_renew_hook"; then
4301 _err "Call hook error."
4302 return 1
4303 fi
4c3b3608 4304}
4305
43822d37 4306#domain [isEcc]
4c3b3608 4307renew() {
4308 Le_Domain="$1"
4c2a3841 4309 if [ -z "$Le_Domain" ]; then
43822d37 4310 _usage "Usage: $PROJECT_ENTRY --renew -d domain.com [--ecc]"
4c3b3608 4311 return 1
4312 fi
4313
43822d37 4314 _isEcc="$2"
4315
e799ef29 4316 _initpath "$Le_Domain" "$_isEcc"
43822d37 4317
e2053b22 4318 _info "$(__green "Renew: '$Le_Domain'")"
4c2a3841 4319 if [ ! -f "$DOMAIN_CONF" ]; then
43822d37 4320 _info "'$Le_Domain' is not a issued domain, skip."
4c2a3841 4321 return 0
4c3b3608 4322 fi
4c2a3841 4323
4324 if [ "$Le_RenewalDays" ]; then
1e6b68f5 4325 _savedomainconf Le_RenewalDays "$Le_RenewalDays"
4326 fi
4327
8663fb7e 4328 . "$DOMAIN_CONF"
c5f1cca3 4329 _debug Le_API "$Le_API"
4c2a3841 4330 if [ "$Le_API" ]; then
48d9a8c1 4331 if [ "$_OLD_CA_HOST" = "$Le_API" ]; then
4332 export Le_API="$DEFAULT_CA"
4333 _savedomainconf Le_API "$Le_API"
4334 fi
4a2ac7bd 4335 if [ "$_OLD_STAGE_CA_HOST" = "$Le_API" ]; then
c1151b0d 4336 export Le_API="$DEFAULT_STAGING_CA"
4a2ac7bd 4337 _savedomainconf Le_API "$Le_API"
4338 fi
48d9a8c1 4339 export ACME_DIRECTORY="$Le_API"
c4236e58 4340 #reload ca configs
4341 ACCOUNT_KEY_PATH=""
4342 ACCOUNT_JSON_PATH=""
4343 CA_CONF=""
4344 _debug3 "initpath again."
4345 _initpath "$Le_Domain" "$_isEcc"
5c48e139 4346 fi
4c2a3841 4347
4348 if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(_time)" -lt "$Le_NextRenewTime" ]; then
e2053b22 4349 _info "Skip, Next renewal time is: $(__green "$Le_NextRenewTimeStr")"
4350 _info "Add '$(__red '--force')' to force to renew."
e799ef29 4351 return "$RENEW_SKIP"
4c3b3608 4352 fi
4c2a3841 4353
c4d0aec5 4354 if [ "$IN_CRON" = "1" ] && [ -z "$Le_CertCreateTime" ]; then
4355 _info "Skip invalid cert for: $Le_Domain"
4356 return 0
4357 fi
4358
4c3b3608 4359 IS_RENEW="1"
875625b1 4360 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 4361 res="$?"
4c2a3841 4362 if [ "$res" != "0" ]; then
e799ef29 4363 return "$res"
a61fe418 4364 fi
4c2a3841 4365
4366 if [ "$Le_DeployHook" ]; then
93bce1b2 4367 _deploy "$Le_Domain" "$Le_DeployHook"
e799ef29 4368 res="$?"
a61fe418 4369 fi
4c2a3841 4370
4c3b3608 4371 IS_RENEW=""
4372
e799ef29 4373 return "$res"
4c3b3608 4374}
4375
cc179731 4376#renewAll [stopRenewOnError]
4c3b3608 4377renewAll() {
4378 _initpath
cc179731 4379 _stopRenewOnError="$1"
4380 _debug "_stopRenewOnError" "$_stopRenewOnError"
4381 _ret="0"
43822d37 4382
e591d5cf 4383 for di in "${CERT_HOME}"/*.*/; do
4384 _debug di "$di"
44483dba 4385 if ! [ -d "$di" ]; then
3498a585 4386 _debug "Not directory, skip: $di"
4387 continue
4388 fi
e591d5cf 4389 d=$(basename "$di")
201aa244 4390 _debug d "$d"
43822d37 4391 (
201aa244 4392 if _endswith "$d" "$ECC_SUFFIX"; then
4393 _isEcc=$(echo "$d" | cut -d "$ECC_SEP" -f 2)
4394 d=$(echo "$d" | cut -d "$ECC_SEP" -f 1)
43822d37 4395 fi
4396 renew "$d" "$_isEcc"
4d2f38b0 4397 )
cc179731 4398 rc="$?"
4399 _debug "Return code: $rc"
4c2a3841 4400 if [ "$rc" != "0" ]; then
4401 if [ "$rc" = "$RENEW_SKIP" ]; then
cc179731 4402 _info "Skipped $d"
4c2a3841 4403 elif [ "$_stopRenewOnError" ]; then
cc179731 4404 _err "Error renew $d, stop now."
201aa244 4405 return "$rc"
cc179731 4406 else
4407 _ret="$rc"
482cb737 4408 _err "Error renew $d."
cc179731 4409 fi
4410 fi
4c3b3608 4411 done
201aa244 4412 return "$_ret"
4c3b3608 4413}
4414
10afcaca 4415#csr webroot
4c2a3841 4416signcsr() {
10afcaca 4417 _csrfile="$1"
4418 _csrW="$2"
4419 if [ -z "$_csrfile" ] || [ -z "$_csrW" ]; then
4420 _usage "Usage: $PROJECT_ENTRY --signcsr --csr mycsr.csr -w /path/to/webroot/a.com/ "
4421 return 1
4422 fi
4423
875625b1 4424 _real_cert="$3"
4425 _real_key="$4"
4426 _real_ca="$5"
4427 _reload_cmd="$6"
4428 _real_fullchain="$7"
4429 _pre_hook="${8}"
4430 _post_hook="${9}"
4431 _renew_hook="${10}"
4432 _local_addr="${11}"
4433 _challenge_alias="${12}"
4434
10afcaca 4435 _csrsubj=$(_readSubjectFromCSR "$_csrfile")
4c2a3841 4436 if [ "$?" != "0" ]; then
10afcaca 4437 _err "Can not read subject from csr: $_csrfile"
4438 return 1
4439 fi
ad752b31 4440 _debug _csrsubj "$_csrsubj"
2c9ed4c5 4441 if _contains "$_csrsubj" ' ' || ! _contains "$_csrsubj" '.'; then
4442 _info "It seems that the subject: $_csrsubj is not a valid domain name. Drop it."
4443 _csrsubj=""
4444 fi
10afcaca 4445
4446 _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile")
4c2a3841 4447 if [ "$?" != "0" ]; then
10afcaca 4448 _err "Can not read domain list from csr: $_csrfile"
4449 return 1
4450 fi
4451 _debug "_csrdomainlist" "$_csrdomainlist"
4c2a3841 4452
4453 if [ -z "$_csrsubj" ]; then
ad752b31 4454 _csrsubj="$(_getfield "$_csrdomainlist" 1)"
4455 _debug _csrsubj "$_csrsubj"
4456 _csrdomainlist="$(echo "$_csrdomainlist" | cut -d , -f 2-)"
4457 _debug "_csrdomainlist" "$_csrdomainlist"
4458 fi
4c2a3841 4459
4460 if [ -z "$_csrsubj" ]; then
ad752b31 4461 _err "Can not read subject from csr: $_csrfile"
4462 return 1
4463 fi
4c2a3841 4464
10afcaca 4465 _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile")
4c2a3841 4466 if [ "$?" != "0" ] || [ -z "$_csrkeylength" ]; then
10afcaca 4467 _err "Can not read key length from csr: $_csrfile"
4468 return 1
4469 fi
4c2a3841 4470
cd9fb3b6 4471 if [ -z "$ACME_VERSION" ] && _contains "$_csrsubj,$_csrdomainlist" "*."; then
4472 export ACME_VERSION=2
4473 fi
10afcaca 4474 _initpath "$_csrsubj" "$_csrkeylength"
4475 mkdir -p "$DOMAIN_PATH"
4c2a3841 4476
10afcaca 4477 _info "Copy csr to: $CSR_PATH"
4478 cp "$_csrfile" "$CSR_PATH"
4c2a3841 4479
875625b1 4480 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 4481
10afcaca 4482}
4483
4484showcsr() {
4c2a3841 4485 _csrfile="$1"
10afcaca 4486 _csrd="$2"
4487 if [ -z "$_csrfile" ] && [ -z "$_csrd" ]; then
4488 _usage "Usage: $PROJECT_ENTRY --showcsr --csr mycsr.csr"
4489 return 1
4490 fi
4491
4492 _initpath
4c2a3841 4493
10afcaca 4494 _csrsubj=$(_readSubjectFromCSR "$_csrfile")
4c2a3841 4495 if [ "$?" != "0" ] || [ -z "$_csrsubj" ]; then
10afcaca 4496 _err "Can not read subject from csr: $_csrfile"
4497 return 1
4498 fi
4c2a3841 4499
10afcaca 4500 _info "Subject=$_csrsubj"
4501
4502 _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile")
4c2a3841 4503 if [ "$?" != "0" ]; then
10afcaca 4504 _err "Can not read domain list from csr: $_csrfile"
4505 return 1
4506 fi
4507 _debug "_csrdomainlist" "$_csrdomainlist"
4508
4509 _info "SubjectAltNames=$_csrdomainlist"
4510
10afcaca 4511 _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile")
4c2a3841 4512 if [ "$?" != "0" ] || [ -z "$_csrkeylength" ]; then
10afcaca 4513 _err "Can not read key length from csr: $_csrfile"
4514 return 1
4515 fi
4516 _info "KeyLength=$_csrkeylength"
4517}
4518
6d7eda3e 4519list() {
22ea4004 4520 _raw="$1"
6d7eda3e 4521 _initpath
4c2a3841 4522
dcf4f8f6 4523 _sep="|"
4c2a3841 4524 if [ "$_raw" ]; then
d5ec5f80 4525 printf "%s\n" "Main_Domain${_sep}KeyLength${_sep}SAN_Domains${_sep}Created${_sep}Renew"
e591d5cf 4526 for di in "${CERT_HOME}"/*.*/; do
44483dba 4527 if ! [ -d "$di" ]; then
3498a585 4528 _debug "Not directory, skip: $di"
4529 continue
4530 fi
e591d5cf 4531 d=$(basename "$di")
201aa244 4532 _debug d "$d"
dcf4f8f6 4533 (
201aa244 4534 if _endswith "$d" "$ECC_SUFFIX"; then
4535 _isEcc=$(echo "$d" | cut -d "$ECC_SEP" -f 2)
4536 d=$(echo "$d" | cut -d "$ECC_SEP" -f 1)
43822d37 4537 fi
e591d5cf 4538 _initpath "$d" "$_isEcc"
4c2a3841 4539 if [ -f "$DOMAIN_CONF" ]; then
dcf4f8f6 4540 . "$DOMAIN_CONF"
d5ec5f80 4541 printf "%s\n" "$Le_Domain${_sep}\"$Le_Keylength\"${_sep}$Le_Alt${_sep}$Le_CertCreateTimeStr${_sep}$Le_NextRenewTimeStr"
dcf4f8f6 4542 fi
4543 )
4544 done
4545 else
4c2a3841 4546 if _exists column; then
22ea4004 4547 list "raw" | column -t -s "$_sep"
4548 else
43822d37 4549 list "raw" | tr "$_sep" '\t'
22ea4004 4550 fi
dcf4f8f6 4551 fi
6d7eda3e 4552
6d7eda3e 4553}
4554
93bce1b2 4555_deploy() {
4556 _d="$1"
4557 _hooks="$2"
4558
4559 for _d_api in $(echo "$_hooks" | tr ',' " "); do
4560 _deployApi="$(_findHook "$_d" deploy "$_d_api")"
4561 if [ -z "$_deployApi" ]; then
4562 _err "The deploy hook $_d_api is not found."
4563 return 1
4564 fi
4565 _debug _deployApi "$_deployApi"
4566
4567 if ! (
4568 if ! . "$_deployApi"; then
4569 _err "Load file $_deployApi error. Please check your api file and try again."
4570 return 1
4571 fi
4572
4573 d_command="${_d_api}_deploy"
4574 if ! _exists "$d_command"; then
4575 _err "It seems that your api file is not correct, it must have a function named: $d_command"
4576 return 1
4577 fi
4578
4579 if ! $d_command "$_d" "$CERT_KEY_PATH" "$CERT_PATH" "$CA_CERT_PATH" "$CERT_FULLCHAIN_PATH"; then
4580 _err "Error deploy for domain:$_d"
4581 return 1
4582 fi
4583 ); then
4584 _err "Deploy error."
4585 return 1
4586 else
4587 _info "$(__green Success)"
4588 fi
4589 done
4590}
4591
4592#domain hooks
a61fe418 4593deploy() {
93bce1b2 4594 _d="$1"
4595 _hooks="$2"
a61fe418 4596 _isEcc="$3"
93bce1b2 4597 if [ -z "$_hooks" ]; then
a61fe418 4598 _usage "Usage: $PROJECT_ENTRY --deploy -d domain.com --deploy-hook cpanel [--ecc] "
4599 return 1
4600 fi
4601
93bce1b2 4602 _initpath "$_d" "$_isEcc"
4c2a3841 4603 if [ ! -d "$DOMAIN_PATH" ]; then
93bce1b2 4604 _err "Domain is not valid:'$_d'"
a61fe418 4605 return 1
4606 fi
4c2a3841 4607
93bce1b2 4608 . "$DOMAIN_CONF"
4c2a3841 4609
93bce1b2 4610 _savedomainconf Le_DeployHook "$_hooks"
4c2a3841 4611
93bce1b2 4612 _deploy "$_d" "$_hooks"
a61fe418 4613}
4614
4c3b3608 4615installcert() {
85e1f4ea 4616 _main_domain="$1"
4617 if [ -z "$_main_domain" ]; then
5c539af7 4618 _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 4619 return 1
4620 fi
4621
85e1f4ea 4622 _real_cert="$2"
4623 _real_key="$3"
4624 _real_ca="$4"
4625 _reload_cmd="$5"
4626 _real_fullchain="$6"
43822d37 4627 _isEcc="$7"
4628
85e1f4ea 4629 _initpath "$_main_domain" "$_isEcc"
4c2a3841 4630 if [ ! -d "$DOMAIN_PATH" ]; then
85e1f4ea 4631 _err "Domain is not valid:'$_main_domain'"
43822d37 4632 return 1
4633 fi
4634
85e1f4ea 4635 _savedomainconf "Le_RealCertPath" "$_real_cert"
4636 _savedomainconf "Le_RealCACertPath" "$_real_ca"
4637 _savedomainconf "Le_RealKeyPath" "$_real_key"
4638 _savedomainconf "Le_ReloadCmd" "$_reload_cmd"
4639 _savedomainconf "Le_RealFullChainPath" "$_real_fullchain"
4640
044da37c 4641 _installcert "$_main_domain" "$_real_cert" "$_real_key" "$_real_ca" "$_real_fullchain" "$_reload_cmd"
43822d37 4642}
4c3b3608 4643
044da37c 4644#domain cert key ca fullchain reloadcmd backup-prefix
43822d37 4645_installcert() {
85e1f4ea 4646 _main_domain="$1"
4647 _real_cert="$2"
4648 _real_key="$3"
4649 _real_ca="$4"
044da37c 4650 _real_fullchain="$5"
4651 _reload_cmd="$6"
4652 _backup_prefix="$7"
4c3b3608 4653
85e1f4ea 4654 if [ "$_real_cert" = "$NO_VALUE" ]; then
4655 _real_cert=""
4d2f38b0 4656 fi
85e1f4ea 4657 if [ "$_real_key" = "$NO_VALUE" ]; then
4658 _real_key=""
4d2f38b0 4659 fi
85e1f4ea 4660 if [ "$_real_ca" = "$NO_VALUE" ]; then
4661 _real_ca=""
4d2f38b0 4662 fi
85e1f4ea 4663 if [ "$_reload_cmd" = "$NO_VALUE" ]; then
4664 _reload_cmd=""
4d2f38b0 4665 fi
85e1f4ea 4666 if [ "$_real_fullchain" = "$NO_VALUE" ]; then
4667 _real_fullchain=""
4d2f38b0 4668 fi
4c2a3841 4669
044da37c 4670 _backup_path="$DOMAIN_BACKUP_PATH/$_backup_prefix"
4671 mkdir -p "$_backup_path"
4672
85e1f4ea 4673 if [ "$_real_cert" ]; then
4674 _info "Installing cert to:$_real_cert"
4675 if [ -f "$_real_cert" ] && [ ! "$IS_RENEW" ]; then
044da37c 4676 cp "$_real_cert" "$_backup_path/cert.bak"
4c3b3608 4677 fi
206be3c1 4678 cat "$CERT_PATH" >"$_real_cert" || return 1
4c3b3608 4679 fi
4c2a3841 4680
85e1f4ea 4681 if [ "$_real_ca" ]; then
4682 _info "Installing CA to:$_real_ca"
4683 if [ "$_real_ca" = "$_real_cert" ]; then
4684 echo "" >>"$_real_ca"
206be3c1 4685 cat "$CA_CERT_PATH" >>"$_real_ca" || return 1
4c3b3608 4686 else
85e1f4ea 4687 if [ -f "$_real_ca" ] && [ ! "$IS_RENEW" ]; then
044da37c 4688 cp "$_real_ca" "$_backup_path/ca.bak"
78552b18 4689 fi
206be3c1 4690 cat "$CA_CERT_PATH" >"$_real_ca" || return 1
4c3b3608 4691 fi
4692 fi
4693
85e1f4ea 4694 if [ "$_real_key" ]; then
4695 _info "Installing key to:$_real_key"
4696 if [ -f "$_real_key" ] && [ ! "$IS_RENEW" ]; then
044da37c 4697 cp "$_real_key" "$_backup_path/key.bak"
4c3b3608 4698 fi
82014583 4699 if [ -f "$_real_key" ]; then
206be3c1 4700 cat "$CERT_KEY_PATH" >"$_real_key" || return 1
82014583 4701 else
206be3c1 4702 cat "$CERT_KEY_PATH" >"$_real_key" || return 1
7b92371a 4703 chmod 600 "$_real_key"
82014583 4704 fi
4c3b3608 4705 fi
4c2a3841 4706
85e1f4ea 4707 if [ "$_real_fullchain" ]; then
4708 _info "Installing full chain to:$_real_fullchain"
4709 if [ -f "$_real_fullchain" ] && [ ! "$IS_RENEW" ]; then
044da37c 4710 cp "$_real_fullchain" "$_backup_path/fullchain.bak"
a63b05a9 4711 fi
206be3c1 4712 cat "$CERT_FULLCHAIN_PATH" >"$_real_fullchain" || return 1
4c2a3841 4713 fi
4c3b3608 4714
85e1f4ea 4715 if [ "$_reload_cmd" ]; then
4716 _info "Run reload cmd: $_reload_cmd"
25555b8c 4717 if (
839bf0e2 4718 export CERT_PATH
4719 export CERT_KEY_PATH
4720 export CA_CERT_PATH
4721 export CERT_FULLCHAIN_PATH
58d4c74b 4722 export Le_Domain
85e1f4ea 4723 cd "$DOMAIN_PATH" && eval "$_reload_cmd"
839bf0e2 4724 ); then
43822d37 4725 _info "$(__green "Reload success")"
4d2f38b0 4726 else
4727 _err "Reload error for :$Le_Domain"
4728 fi
4729 fi
4730
4c3b3608 4731}
4732
27dbe77f 4733#confighome
4c3b3608 4734installcronjob() {
27dbe77f 4735 _c_home="$1"
4c3b3608 4736 _initpath
415f375c 4737 _CRONTAB="crontab"
4738 if ! _exists "$_CRONTAB" && _exists "fcrontab"; then
4739 _CRONTAB="fcrontab"
4740 fi
4741 if ! _exists "$_CRONTAB"; then
4742 _err "crontab/fcrontab doesn't exist, so, we can not install cron jobs."
77546ea5 4743 _err "All your certs will not be renewed automatically."
a7b7355d 4744 _err "You must add your own cron job to call '$PROJECT_ENTRY --cron' everyday."
77546ea5 4745 return 1
4746 fi
4747
4c3b3608 4748 _info "Installing cron job"
415f375c 4749 if ! $_CRONTAB -l | grep "$PROJECT_ENTRY --cron"; then
4c2a3841 4750 if [ -f "$LE_WORKING_DIR/$PROJECT_ENTRY" ]; then
a7b7355d 4751 lesh="\"$LE_WORKING_DIR\"/$PROJECT_ENTRY"
4c3b3608 4752 else
a7b7355d 4753 _err "Can not install cronjob, $PROJECT_ENTRY not found."
4c3b3608 4754 return 1
4755 fi
27dbe77f 4756
4757 if [ "$_c_home" ]; then
80941f84 4758 _c_entry="--config-home \"$_c_home\" "
27dbe77f 4759 fi
32b3717c 4760 _t=$(_time)
4761 random_minute=$(_math $_t % 60)
e2c939fb 4762 if _exists uname && uname -a | grep SunOS >/dev/null; then
415f375c 4763 $_CRONTAB -l | {
4c2a3841 4764 cat
0533bde9 4765 echo "$random_minute 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" $_c_entry> /dev/null"
415f375c 4766 } | $_CRONTAB --
22ea4004 4767 else
415f375c 4768 $_CRONTAB -l | {
4c2a3841 4769 cat
0533bde9 4770 echo "$random_minute 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" $_c_entry> /dev/null"
415f375c 4771 } | $_CRONTAB -
22ea4004 4772 fi
4c3b3608 4773 fi
4c2a3841 4774 if [ "$?" != "0" ]; then
4c3b3608 4775 _err "Install cron job failed. You need to manually renew your certs."
4776 _err "Or you can add cronjob by yourself:"
a7b7355d 4777 _err "$lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null"
4c3b3608 4778 return 1
4779 fi
4780}
4781
4782uninstallcronjob() {
415f375c 4783 _CRONTAB="crontab"
4784 if ! _exists "$_CRONTAB" && _exists "fcrontab"; then
4785 _CRONTAB="fcrontab"
4786 fi
4787
4788 if ! _exists "$_CRONTAB"; then
37db5b81 4789 return
4790 fi
4c3b3608 4791 _info "Removing cron job"
415f375c 4792 cr="$($_CRONTAB -l | grep "$PROJECT_ENTRY --cron")"
4c2a3841 4793 if [ "$cr" ]; then
4794 if _exists uname && uname -a | grep solaris >/dev/null; then
415f375c 4795 $_CRONTAB -l | sed "/$PROJECT_ENTRY --cron/d" | $_CRONTAB --
22ea4004 4796 else
415f375c 4797 $_CRONTAB -l | sed "/$PROJECT_ENTRY --cron/d" | $_CRONTAB -
22ea4004 4798 fi
a7b7355d 4799 LE_WORKING_DIR="$(echo "$cr" | cut -d ' ' -f 9 | tr -d '"')"
4c3b3608 4800 _info LE_WORKING_DIR "$LE_WORKING_DIR"
27dbe77f 4801 if _contains "$cr" "--config-home"; then
f5b546b3 4802 LE_CONFIG_HOME="$(echo "$cr" | cut -d ' ' -f 11 | tr -d '"')"
4803 _debug LE_CONFIG_HOME "$LE_CONFIG_HOME"
27dbe77f 4804 fi
4c2a3841 4805 fi
4c3b3608 4806 _initpath
a7b7355d 4807
4c3b3608 4808}
4809
6cb415f5 4810revoke() {
4811 Le_Domain="$1"
4c2a3841 4812 if [ -z "$Le_Domain" ]; then
78f0201d 4813 _usage "Usage: $PROJECT_ENTRY --revoke -d domain.com [--ecc]"
6cb415f5 4814 return 1
4815 fi
4c2a3841 4816
43822d37 4817 _isEcc="$2"
4818
c4a375b3 4819 _initpath "$Le_Domain" "$_isEcc"
4c2a3841 4820 if [ ! -f "$DOMAIN_CONF" ]; then
6cb415f5 4821 _err "$Le_Domain is not a issued domain, skip."
4c2a3841 4822 return 1
6cb415f5 4823 fi
4c2a3841 4824
4825 if [ ! -f "$CERT_PATH" ]; then
6cb415f5 4826 _err "Cert for $Le_Domain $CERT_PATH is not found, skip."
4827 return 1
4828 fi
6cb415f5 4829
11927a76 4830 cert="$(_getfile "${CERT_PATH}" "${BEGIN_CERT}" "${END_CERT}" | tr -d "\r\n" | _url_replace)"
4c2a3841 4831
4832 if [ -z "$cert" ]; then
6cb415f5 4833 _err "Cert for $Le_Domain is empty found, skip."
4834 return 1
4835 fi
4c2a3841 4836
48d9a8c1 4837 _initAPI
4838
d2cde379 4839 if [ "$ACME_VERSION" = "2" ]; then
4840 data="{\"certificate\": \"$cert\"}"
4841 else
4842 data="{\"resource\": \"revoke-cert\", \"certificate\": \"$cert\"}"
4843 fi
48d9a8c1 4844 uri="${ACME_REVOKE_CERT}"
6cb415f5 4845
4c2a3841 4846 if [ -f "$CERT_KEY_PATH" ]; then
1befee5a 4847 _info "Try domain key first."
c4a375b3 4848 if _send_signed_request "$uri" "$data" "" "$CERT_KEY_PATH"; then
4c2a3841 4849 if [ -z "$response" ]; then
1befee5a 4850 _info "Revoke success."
c4a375b3 4851 rm -f "$CERT_PATH"
1befee5a 4852 return 0
4c2a3841 4853 else
1befee5a 4854 _err "Revoke error by domain key."
4855 _err "$response"
4856 fi
6cb415f5 4857 fi
4c2a3841 4858 else
1befee5a 4859 _info "Domain key file doesn't exists."
6cb415f5 4860 fi
6cb415f5 4861
1befee5a 4862 _info "Try account key."
6cb415f5 4863
c4a375b3 4864 if _send_signed_request "$uri" "$data" "" "$ACCOUNT_KEY_PATH"; then
4c2a3841 4865 if [ -z "$response" ]; then
6cb415f5 4866 _info "Revoke success."
c4a375b3 4867 rm -f "$CERT_PATH"
6cb415f5 4868 return 0
4c2a3841 4869 else
6cb415f5 4870 _err "Revoke error."
c9c31c04 4871 _debug "$response"
6cb415f5 4872 fi
4873 fi
4874 return 1
4875}
4c3b3608 4876
78f0201d 4877#domain ecc
4878remove() {
4879 Le_Domain="$1"
4880 if [ -z "$Le_Domain" ]; then
4881 _usage "Usage: $PROJECT_ENTRY --remove -d domain.com [--ecc]"
4882 return 1
4883 fi
4884
4885 _isEcc="$2"
4886
4887 _initpath "$Le_Domain" "$_isEcc"
4888 _removed_conf="$DOMAIN_CONF.removed"
4889 if [ ! -f "$DOMAIN_CONF" ]; then
4890 if [ -f "$_removed_conf" ]; then
4891 _err "$Le_Domain is already removed, You can remove the folder by yourself: $DOMAIN_PATH"
4892 else
4893 _err "$Le_Domain is not a issued domain, skip."
4894 fi
4895 return 1
4896 fi
4897
4898 if mv "$DOMAIN_CONF" "$_removed_conf"; then
68aea3af 4899 _info "$Le_Domain is removed, the key and cert files are in $(__green $DOMAIN_PATH)"
78f0201d 4900 _info "You can remove them by yourself."
4901 return 0
4902 else
4903 _err "Remove $Le_Domain failed."
4904 return 1
4905 fi
4906}
4907
0c00e870 4908#domain vtype
4909_deactivate() {
4910 _d_domain="$1"
4911 _d_type="$2"
4912 _initpath
4c2a3841 4913
d2cde379 4914 if [ "$ACME_VERSION" = "2" ]; then
4915 _identifiers="{\"type\":\"dns\",\"value\":\"$_d_domain\"}"
4916 if ! _send_signed_request "$ACME_NEW_ORDER" "{\"identifiers\": [$_identifiers]}"; then
4917 _err "Can not get domain new order."
4918 return 1
4919 fi
4920 _authorizations_seg="$(echo "$response" | tr -d '\r\n' | _egrep_o '"authorizations" *: *\[[^\]*\]' | cut -d '[' -f 2 | tr -d ']' | tr -d '"')"
4921 _debug2 _authorizations_seg "$_authorizations_seg"
4922 if [ -z "$_authorizations_seg" ]; then
4923 _err "_authorizations_seg not found."
4924 _clearup
4925 _on_issue_err "$_post_hook"
4926 return 1
4927 fi
4c2a3841 4928
d2cde379 4929 authzUri="$_authorizations_seg"
4930 _debug2 "authzUri" "$authzUri"
4931 if ! response="$(_get "$authzUri")"; then
4932 _err "get to authz error."
263c38ca 4933 _err "_authorizations_seg" "$_authorizations_seg"
4934 _err "authzUri" "$authzUri"
d2cde379 4935 _clearup
4936 _on_issue_err "$_post_hook"
4937 return 1
4938 fi
4c2a3841 4939
d2cde379 4940 response="$(echo "$response" | _normalizeJson)"
4941 _debug2 response "$response"
4942 _URL_NAME="url"
4943 else
4944 if ! __get_domain_new_authz "$_d_domain"; then
4945 _err "Can not get domain new authz token."
4946 return 1
4947 fi
4948
4949 authzUri="$(echo "$responseHeaders" | grep "^Location:" | _head_n 1 | cut -d ' ' -f 2 | tr -d "\r\n")"
4950 _debug "authzUri" "$authzUri"
4951 if [ "$code" ] && [ ! "$code" = '201' ]; then
4952 _err "new-authz error: $response"
4953 return 1
4954 fi
4955 _URL_NAME="uri"
14d7bfda 4956 fi
0c00e870 4957
d2cde379 4958 entries="$(echo "$response" | _egrep_o "{ *\"type\":\"[^\"]*\", *\"status\": *\"valid\", *\"$_URL_NAME\"[^}]*")"
14d7bfda 4959 if [ -z "$entries" ]; then
4960 _info "No valid entries found."
4961 if [ -z "$thumbprint" ]; then
4962 thumbprint="$(__calc_account_thumbprint)"
4963 fi
4964 _debug "Trigger validation."
d2cde379 4965 vtype="$VTYPE_DNS"
14d7bfda 4966 entry="$(printf "%s\n" "$response" | _egrep_o '[^\{]*"type":"'$vtype'"[^\}]*')"
4967 _debug entry "$entry"
4968 if [ -z "$entry" ]; then
4969 _err "Error, can not get domain token $d"
0c00e870 4970 return 1
4971 fi
14d7bfda 4972 token="$(printf "%s\n" "$entry" | _egrep_o '"token":"[^"]*' | cut -d : -f 2 | tr -d '"')"
4973 _debug token "$token"
4c2a3841 4974
d2cde379 4975 uri="$(printf "%s\n" "$entry" | _egrep_o "\"$_URL_NAME\":\"[^\"]*" | cut -d : -f 2,3 | tr -d '"')"
14d7bfda 4976 _debug uri "$uri"
4977
4978 keyauthorization="$token.$thumbprint"
4979 _debug keyauthorization "$keyauthorization"
4980 __trigger_validation "$uri" "$keyauthorization"
4981
4982 fi
4983
4984 _d_i=0
4985 _d_max_retry=$(echo "$entries" | wc -l)
4986 while [ "$_d_i" -lt "$_d_max_retry" ]; do
4987 _info "Deactivate: $_d_domain"
4988 _d_i="$(_math $_d_i + 1)"
4989 entry="$(echo "$entries" | sed -n "${_d_i}p")"
0c00e870 4990 _debug entry "$entry"
4c2a3841 4991
4992 if [ -z "$entry" ]; then
fb2029e7 4993 _info "No more valid entry found."
0c00e870 4994 break
4995 fi
4c2a3841 4996
0c00e870 4997 _vtype="$(printf "%s\n" "$entry" | _egrep_o '"type": *"[^"]*"' | cut -d : -f 2 | tr -d '"')"
c4a375b3 4998 _debug _vtype "$_vtype"
0c00e870 4999 _info "Found $_vtype"
5000
d2cde379 5001 uri="$(printf "%s\n" "$entry" | _egrep_o "\"$_URL_NAME\":\"[^\"]*" | cut -d : -f 2,3 | tr -d '"')"
c4a375b3 5002 _debug uri "$uri"
4c2a3841 5003
5004 if [ "$_d_type" ] && [ "$_d_type" != "$_vtype" ]; then
0c00e870 5005 _info "Skip $_vtype"
5006 continue
5007 fi
4c2a3841 5008
0c00e870 5009 _info "Deactivate: $_vtype"
4c2a3841 5010
d2cde379 5011 if [ "$ACME_VERSION" = "2" ]; then
5012 _djson="{\"status\":\"deactivated\"}"
5013 else
5014 _djson="{\"resource\": \"authz\", \"status\":\"deactivated\"}"
5015 fi
5016
5017 if _send_signed_request "$authzUri" "$_djson" && _contains "$response" '"deactivated"'; then
14d7bfda 5018 _info "Deactivate: $_vtype success."
5019 else
0c00e870 5020 _err "Can not deactivate $_vtype."
14d7bfda 5021 break
0c00e870 5022 fi
4c2a3841 5023
0c00e870 5024 done
5025 _debug "$_d_i"
14d7bfda 5026 if [ "$_d_i" -eq "$_d_max_retry" ]; then
0c00e870 5027 _info "Deactivated success!"
5028 else
5029 _err "Deactivate failed."
5030 fi
5031
5032}
5033
5034deactivate() {
3f4513b3 5035 _d_domain_list="$1"
0c00e870 5036 _d_type="$2"
5037 _initpath
a3bdaa85 5038 _initAPI
3f4513b3 5039 _debug _d_domain_list "$_d_domain_list"
4c2a3841 5040 if [ -z "$(echo $_d_domain_list | cut -d , -f 1)" ]; then
3f4513b3 5041 _usage "Usage: $PROJECT_ENTRY --deactivate -d domain.com [-d domain.com]"
0c00e870 5042 return 1
5043 fi
4c2a3841 5044 for _d_dm in $(echo "$_d_domain_list" | tr ',' ' '); do
5045 if [ -z "$_d_dm" ] || [ "$_d_dm" = "$NO_VALUE" ]; then
3f4513b3 5046 continue
5047 fi
c4a375b3 5048 if ! _deactivate "$_d_dm" "$_d_type"; then
86c017ec 5049 return 1
5050 fi
3f4513b3 5051 done
0c00e870 5052}
5053
4c3b3608 5054# Detect profile file if not specified as environment variable
5055_detect_profile() {
4c2a3841 5056 if [ -n "$PROFILE" -a -f "$PROFILE" ]; then
4c3b3608 5057 echo "$PROFILE"
5058 return
5059 fi
5060
4c3b3608 5061 DETECTED_PROFILE=''
4c3b3608 5062 SHELLTYPE="$(basename "/$SHELL")"
5063
4c2a3841 5064 if [ "$SHELLTYPE" = "bash" ]; then
5065 if [ -f "$HOME/.bashrc" ]; then
4c3b3608 5066 DETECTED_PROFILE="$HOME/.bashrc"
4c2a3841 5067 elif [ -f "$HOME/.bash_profile" ]; then
4c3b3608 5068 DETECTED_PROFILE="$HOME/.bash_profile"
5069 fi
4c2a3841 5070 elif [ "$SHELLTYPE" = "zsh" ]; then
4c3b3608 5071 DETECTED_PROFILE="$HOME/.zshrc"
5072 fi
5073
4c2a3841 5074 if [ -z "$DETECTED_PROFILE" ]; then
5075 if [ -f "$HOME/.profile" ]; then
4c3b3608 5076 DETECTED_PROFILE="$HOME/.profile"
4c2a3841 5077 elif [ -f "$HOME/.bashrc" ]; then
4c3b3608 5078 DETECTED_PROFILE="$HOME/.bashrc"
4c2a3841 5079 elif [ -f "$HOME/.bash_profile" ]; then
4c3b3608 5080 DETECTED_PROFILE="$HOME/.bash_profile"
4c2a3841 5081 elif [ -f "$HOME/.zshrc" ]; then
4c3b3608 5082 DETECTED_PROFILE="$HOME/.zshrc"
5083 fi
5084 fi
5085
1be222f6 5086 echo "$DETECTED_PROFILE"
4c3b3608 5087}
5088
5089_initconf() {
5090 _initpath
4c2a3841 5091 if [ ! -f "$ACCOUNT_CONF_PATH" ]; then
0ca5b799 5092 echo "
d404e92d 5093
d0871bda 5094#LOG_FILE=\"$DEFAULT_LOG_FILE\"
6b500036 5095#LOG_LEVEL=1
5ea6e9c9 5096
251d1c5c 5097#AUTO_UPGRADE=\"1\"
89002ed2 5098
569d6c55 5099#NO_TIMESTAMP=1
5b771039 5100
d5ec5f80 5101 " >"$ACCOUNT_CONF_PATH"
4c3b3608 5102 fi
5103}
5104
c8e9a31e 5105# nocron
c60883ef 5106_precheck() {
c8e9a31e 5107 _nocron="$1"
4c2a3841 5108
5109 if ! _exists "curl" && ! _exists "wget"; then
c60883ef 5110 _err "Please install curl or wget first, we need to access http resources."
4c3b3608 5111 return 1
5112 fi
4c2a3841 5113
5114 if [ -z "$_nocron" ]; then
415f375c 5115 if ! _exists "crontab" && ! _exists "fcrontab"; then
c8e9a31e 5116 _err "It is recommended to install crontab first. try to install 'cron, crontab, crontabs or vixie-cron'."
5117 _err "We need to set cron job to renew the certs automatically."
5118 _err "Otherwise, your certs will not be able to be renewed automatically."
4c2a3841 5119 if [ -z "$FORCE" ]; then
c8e9a31e 5120 _err "Please add '--force' and try install again to go without crontab."
5121 _err "./$PROJECT_ENTRY --install --force"
5122 return 1
5123 fi
77546ea5 5124 fi
4c3b3608 5125 fi
4c2a3841 5126
d8ba26e6 5127 if ! _exists "${ACME_OPENSSL_BIN:-openssl}"; then
851fedf7 5128 _err "Please install openssl first. ACME_OPENSSL_BIN=$ACME_OPENSSL_BIN"
c60883ef 5129 _err "We need openssl to generate keys."
4c3b3608 5130 return 1
5131 fi
4c2a3841 5132
3794b5cb 5133 if ! _exists "socat"; then
5134 _err "It is recommended to install socat first."
5135 _err "We use socat for standalone server if you use standalone mode."
c60883ef 5136 _err "If you don't use standalone mode, just ignore this warning."
5137 fi
4c2a3841 5138
c60883ef 5139 return 0
5140}
5141
0a7c9364 5142_setShebang() {
5143 _file="$1"
5144 _shebang="$2"
4c2a3841 5145 if [ -z "$_shebang" ]; then
43822d37 5146 _usage "Usage: file shebang"
0a7c9364 5147 return 1
5148 fi
5149 cp "$_file" "$_file.tmp"
4c2a3841 5150 echo "$_shebang" >"$_file"
5151 sed -n 2,99999p "$_file.tmp" >>"$_file"
5152 rm -f "$_file.tmp"
0a7c9364 5153}
5154
27dbe77f 5155#confighome
94dc5f33 5156_installalias() {
27dbe77f 5157 _c_home="$1"
94dc5f33 5158 _initpath
5159
5160 _envfile="$LE_WORKING_DIR/$PROJECT_ENTRY.env"
4c2a3841 5161 if [ "$_upgrading" ] && [ "$_upgrading" = "1" ]; then
44edb2bd 5162 echo "$(cat "$_envfile")" | sed "s|^LE_WORKING_DIR.*$||" >"$_envfile"
5163 echo "$(cat "$_envfile")" | sed "s|^alias le.*$||" >"$_envfile"
5164 echo "$(cat "$_envfile")" | sed "s|^alias le.sh.*$||" >"$_envfile"
94dc5f33 5165 fi
5166
27dbe77f 5167 if [ "$_c_home" ]; then
be83a6a3 5168 _c_entry=" --config-home '$_c_home'"
27dbe77f 5169 fi
5170
1786a5e5 5171 _setopt "$_envfile" "export LE_WORKING_DIR" "=" "\"$LE_WORKING_DIR\""
f5b546b3 5172 if [ "$_c_home" ]; then
5173 _setopt "$_envfile" "export LE_CONFIG_HOME" "=" "\"$LE_CONFIG_HOME\""
d04434e3 5174 else
5175 _sed_i "/^export LE_CONFIG_HOME/d" "$_envfile"
f5b546b3 5176 fi
be83a6a3 5177 _setopt "$_envfile" "alias $PROJECT_ENTRY" "=" "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\""
94dc5f33 5178
5179 _profile="$(_detect_profile)"
4c2a3841 5180 if [ "$_profile" ]; then
94dc5f33 5181 _debug "Found profile: $_profile"
aba5c634 5182 _info "Installing alias to '$_profile'"
94dc5f33 5183 _setopt "$_profile" ". \"$_envfile\""
5184 _info "OK, Close and reopen your terminal to start using $PROJECT_NAME"
5185 else
5186 _info "No profile is found, you will need to go into $LE_WORKING_DIR to use $PROJECT_NAME"
5187 fi
94dc5f33 5188
5189 #for csh
5190 _cshfile="$LE_WORKING_DIR/$PROJECT_ENTRY.csh"
94dc5f33 5191 _csh_profile="$HOME/.cshrc"
4c2a3841 5192 if [ -f "$_csh_profile" ]; then
aba5c634 5193 _info "Installing alias to '$_csh_profile'"
6626371d 5194 _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\""
f5b546b3 5195 if [ "$_c_home" ]; then
5196 _setopt "$_cshfile" "setenv LE_CONFIG_HOME" " " "\"$LE_CONFIG_HOME\""
d04434e3 5197 else
5198 _sed_i "/^setenv LE_CONFIG_HOME/d" "$_cshfile"
f5b546b3 5199 fi
be83a6a3 5200 _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\""
4c2a3841 5201 _setopt "$_csh_profile" "source \"$_cshfile\""
94dc5f33 5202 fi
4c2a3841 5203
acafa585 5204 #for tcsh
5205 _tcsh_profile="$HOME/.tcshrc"
4c2a3841 5206 if [ -f "$_tcsh_profile" ]; then
aba5c634 5207 _info "Installing alias to '$_tcsh_profile'"
acafa585 5208 _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\""
f5b546b3 5209 if [ "$_c_home" ]; then
5210 _setopt "$_cshfile" "setenv LE_CONFIG_HOME" " " "\"$LE_CONFIG_HOME\""
5211 fi
be83a6a3 5212 _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\""
4c2a3841 5213 _setopt "$_tcsh_profile" "source \"$_cshfile\""
acafa585 5214 fi
94dc5f33 5215
5216}
5217
86ef0a26 5218# nocron confighome noprofile
c60883ef 5219install() {
f3e4cea3 5220
4c2a3841 5221 if [ -z "$LE_WORKING_DIR" ]; then
f3e4cea3 5222 LE_WORKING_DIR="$DEFAULT_INSTALL_HOME"
5223 fi
4c2a3841 5224
c8e9a31e 5225 _nocron="$1"
27dbe77f 5226 _c_home="$2"
86ef0a26 5227 _noprofile="$3"
4c2a3841 5228 if ! _initpath; then
c60883ef 5229 _err "Install failed."
4c3b3608 5230 return 1
5231 fi
4c2a3841 5232 if [ "$_nocron" ]; then
52677b0a 5233 _debug "Skip install cron job"
5234 fi
4c2a3841 5235
4356eefb 5236 if [ "$IN_CRON" != "1" ]; then
5237 if ! _precheck "$_nocron"; then
5238 _err "Pre-check failed, can not install."
5239 return 1
5240 fi
4c3b3608 5241 fi
4c2a3841 5242
8e845d9f 5243 if [ -z "$_c_home" ] && [ "$LE_CONFIG_HOME" != "$LE_WORKING_DIR" ]; then
5244 _info "Using config home: $LE_CONFIG_HOME"
5245 _c_home="$LE_CONFIG_HOME"
5246 fi
5247
6cc11ffb 5248 #convert from le
4c2a3841 5249 if [ -d "$HOME/.le" ]; then
5250 for envfile in "le.env" "le.sh.env"; do
5251 if [ -f "$HOME/.le/$envfile" ]; then
5252 if grep "le.sh" "$HOME/.le/$envfile" >/dev/null; then
5253 _upgrading="1"
5254 _info "You are upgrading from le.sh"
5255 _info "Renaming \"$HOME/.le\" to $LE_WORKING_DIR"
5256 mv "$HOME/.le" "$LE_WORKING_DIR"
5257 mv "$LE_WORKING_DIR/$envfile" "$LE_WORKING_DIR/$PROJECT_ENTRY.env"
5258 break
6cc11ffb 5259 fi
5260 fi
5261 done
5262 fi
5263
4c3b3608 5264 _info "Installing to $LE_WORKING_DIR"
635695ec 5265
d04434e3 5266 if [ ! -d "$LE_WORKING_DIR" ]; then
5267 if ! mkdir -p "$LE_WORKING_DIR"; then
5268 _err "Can not create working dir: $LE_WORKING_DIR"
5269 return 1
5270 fi
5271
5272 chmod 700 "$LE_WORKING_DIR"
4a0f23e2 5273 fi
4c2a3841 5274
d04434e3 5275 if [ ! -d "$LE_CONFIG_HOME" ]; then
5276 if ! mkdir -p "$LE_CONFIG_HOME"; then
5277 _err "Can not create config dir: $LE_CONFIG_HOME"
5278 return 1
5279 fi
762978f8 5280
d04434e3 5281 chmod 700 "$LE_CONFIG_HOME"
27dbe77f 5282 fi
5283
d5ec5f80 5284 cp "$PROJECT_ENTRY" "$LE_WORKING_DIR/" && chmod +x "$LE_WORKING_DIR/$PROJECT_ENTRY"
4c3b3608 5285
4c2a3841 5286 if [ "$?" != "0" ]; then
a7b7355d 5287 _err "Install failed, can not copy $PROJECT_ENTRY"
4c3b3608 5288 return 1
5289 fi
5290
a7b7355d 5291 _info "Installed to $LE_WORKING_DIR/$PROJECT_ENTRY"
4c3b3608 5292
86ef0a26 5293 if [ "$IN_CRON" != "1" ] && [ -z "$_noprofile" ]; then
4356eefb 5294 _installalias "$_c_home"
5295 fi
4c3b3608 5296
4c2a3841 5297 for subf in $_SUB_FOLDERS; do
5298 if [ -d "$subf" ]; then
d5ec5f80 5299 mkdir -p "$LE_WORKING_DIR/$subf"
5300 cp "$subf"/* "$LE_WORKING_DIR"/"$subf"/
a61fe418 5301 fi
5302 done
5303
4c2a3841 5304 if [ ! -f "$ACCOUNT_CONF_PATH" ]; then
4c3b3608 5305 _initconf
5306 fi
6cc11ffb 5307
4c2a3841 5308 if [ "$_DEFAULT_ACCOUNT_CONF_PATH" != "$ACCOUNT_CONF_PATH" ]; then
635695ec 5309 _setopt "$_DEFAULT_ACCOUNT_CONF_PATH" "ACCOUNT_CONF_PATH" "=" "\"$ACCOUNT_CONF_PATH\""
6cc11ffb 5310 fi
5311
4c2a3841 5312 if [ "$_DEFAULT_CERT_HOME" != "$CERT_HOME" ]; then
b2817897 5313 _saveaccountconf "CERT_HOME" "$CERT_HOME"
5314 fi
5315
4c2a3841 5316 if [ "$_DEFAULT_ACCOUNT_KEY_PATH" != "$ACCOUNT_KEY_PATH" ]; then
b2817897 5317 _saveaccountconf "ACCOUNT_KEY_PATH" "$ACCOUNT_KEY_PATH"
5318 fi
4c2a3841 5319
5320 if [ -z "$_nocron" ]; then
27dbe77f 5321 installcronjob "$_c_home"
c8e9a31e 5322 fi
0a7c9364 5323
4c2a3841 5324 if [ -z "$NO_DETECT_SH" ]; then
641989fd 5325 #Modify shebang
4c2a3841 5326 if _exists bash; then
694af4ae 5327 _bash_path="$(bash -c "command -v bash 2>/dev/null")"
5328 if [ -z "$_bash_path" ]; then
5329 _bash_path="$(bash -c 'echo $SHELL')"
5330 fi
5331 fi
5332 if [ "$_bash_path" ]; then
329174b6 5333 _info "Good, bash is found, so change the shebang to use bash as preferred."
694af4ae 5334 _shebang='#!'"$_bash_path"
641989fd 5335 _setShebang "$LE_WORKING_DIR/$PROJECT_ENTRY" "$_shebang"
4c2a3841 5336 for subf in $_SUB_FOLDERS; do
5337 if [ -d "$LE_WORKING_DIR/$subf" ]; then
5338 for _apifile in "$LE_WORKING_DIR/$subf/"*.sh; do
a61fe418 5339 _setShebang "$_apifile" "$_shebang"
5340 done
5341 fi
5342 done
0a7c9364 5343 fi
5344 fi
5345
4c3b3608 5346 _info OK
5347}
5348
52677b0a 5349# nocron
4c3b3608 5350uninstall() {
52677b0a 5351 _nocron="$1"
4c2a3841 5352 if [ -z "$_nocron" ]; then
52677b0a 5353 uninstallcronjob
5354 fi
4c3b3608 5355 _initpath
5356
9aa3be7f 5357 _uninstallalias
4c2a3841 5358
d5ec5f80 5359 rm -f "$LE_WORKING_DIR/$PROJECT_ENTRY"
f5b546b3 5360 _info "The keys and certs are in \"$(__green "$LE_CONFIG_HOME")\", you can remove them by yourself."
9aa3be7f 5361
5362}
5363
5364_uninstallalias() {
5365 _initpath
5366
4c3b3608 5367 _profile="$(_detect_profile)"
4c2a3841 5368 if [ "$_profile" ]; then
9aa3be7f 5369 _info "Uninstalling alias from: '$_profile'"
d5ec5f80 5370 text="$(cat "$_profile")"
4c2a3841 5371 echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.env\"$||" >"$_profile"
4c3b3608 5372 fi
5373
94dc5f33 5374 _csh_profile="$HOME/.cshrc"
4c2a3841 5375 if [ -f "$_csh_profile" ]; then
9aa3be7f 5376 _info "Uninstalling alias from: '$_csh_profile'"
d5ec5f80 5377 text="$(cat "$_csh_profile")"
4c2a3841 5378 echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" >"$_csh_profile"
94dc5f33 5379 fi
4c2a3841 5380
acafa585 5381 _tcsh_profile="$HOME/.tcshrc"
4c2a3841 5382 if [ -f "$_tcsh_profile" ]; then
9aa3be7f 5383 _info "Uninstalling alias from: '$_csh_profile'"
d5ec5f80 5384 text="$(cat "$_tcsh_profile")"
4c2a3841 5385 echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" >"$_tcsh_profile"
acafa585 5386 fi
4c3b3608 5387
5388}
5389
5390cron() {
c73fdd40 5391 export IN_CRON=1
89002ed2 5392 _initpath
d8ba26e6 5393 _info "$(__green "===Starting cron===")"
4c2a3841 5394 if [ "$AUTO_UPGRADE" = "1" ]; then
89002ed2 5395 export LE_WORKING_DIR
5396 (
4c2a3841 5397 if ! upgrade; then
5398 _err "Cron:Upgrade failed!"
5399 return 1
5400 fi
89002ed2 5401 )
d5ec5f80 5402 . "$LE_WORKING_DIR/$PROJECT_ENTRY" >/dev/null
1ab63043 5403
4c2a3841 5404 if [ -t 1 ]; then
1ab63043 5405 __INTERACTIVE="1"
5406 fi
4c2a3841 5407
89002ed2 5408 _info "Auto upgraded to: $VER"
5409 fi
4c3b3608 5410 renewAll
cc179731 5411 _ret="$?"
281aa349 5412 IN_CRON=""
d8ba26e6 5413 _info "$(__green "===End cron===")"
0ba95a3d 5414 exit $_ret
4c3b3608 5415}
5416
5417version() {
a63b05a9 5418 echo "$PROJECT"
5419 echo "v$VER"
4c3b3608 5420}
5421
5422showhelp() {
d0871bda 5423 _initpath
4c3b3608 5424 version
a7b7355d 5425 echo "Usage: $PROJECT_ENTRY command ...[parameters]....
a63b05a9 5426Commands:
5427 --help, -h Show this help message.
5428 --version, -v Show version info.
a7b7355d 5429 --install Install $PROJECT_NAME to your system.
5430 --uninstall Uninstall $PROJECT_NAME, and uninstall the cron job.
d8beaf72 5431 --upgrade Upgrade $PROJECT_NAME to the latest code from $PROJECT.
a63b05a9 5432 --issue Issue a cert.
10afcaca 5433 --signcsr Issue a cert from an existing csr.
a61fe418 5434 --deploy Deploy the cert to your server.
27dbe77f 5435 --install-cert Install the issued cert to apache/nginx or any other server.
a63b05a9 5436 --renew, -r Renew a cert.
27dbe77f 5437 --renew-all Renew all the certs.
a63b05a9 5438 --revoke Revoke a cert.
47b49f1b 5439 --remove Remove the cert from list of certs known to $PROJECT_NAME.
10afcaca 5440 --list List all the certs.
5441 --showcsr Show the content of a csr.
27dbe77f 5442 --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.
5443 --uninstall-cronjob Uninstall the cron job. The 'uninstall' command can do this automatically.
a63b05a9 5444 --cron Run cron job to renew all the certs.
5445 --toPkcs Export the certificate and key to a pfx file.
4410226d 5446 --toPkcs8 Convert to pkcs8 format.
27dbe77f 5447 --update-account Update account info.
5448 --register-account Register account key.
422dd1fa 5449 --deactivate-account Deactivate the account.
0984585d 5450 --create-account-key Create an account private key, professional use.
5451 --create-domain-key Create an domain private key, professional use.
a63b05a9 5452 --createCSR, -ccsr Create CSR , professional use.
0c00e870 5453 --deactivate Deactivate the domain authz, professional use.
3c07f57a 5454
a63b05a9 5455Parameters:
5456 --domain, -d domain.tld Specifies a domain, used to issue, renew or revoke etc.
64821ad4 5457 --challenge-alias domain.tld The challenge domain alias for DNS alias mode: $_DNS_ALIAS_WIKI
5458 --domain-alias domain.tld The domain alias for DNS alias mode: $_DNS_ALIAS_WIKI
a63b05a9 5459 --force, -f Used to force to install or force to renew a cert immediately.
5460 --staging, --test Use staging server, just for test.
5461 --debug Output debug info.
e6e85b0c 5462 --output-insecure Output all the sensitive messages. By default all the credentials/sensitive messages are hidden from the output/debug/log for secure.
a63b05a9 5463 --webroot, -w /path/to/webroot Specifies the web root folder for web root mode.
5464 --standalone Use standalone mode.
0e44f587 5465 --stateless Use stateless mode, see: $_STATELESS_WIKI
a63b05a9 5466 --apache Use apache mode.
eccec5f6 5467 --dns [dns_cf|dns_dp|dns_cx|/path/to/api/file] Use dns mode or dns api.
4a4dacb5 5468 --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 5469
a63b05a9 5470 --keylength, -k [2048] Specifies the domain key length: 2048, 3072, 4096, 8192 or ec-256, ec-384.
5471 --accountkeylength, -ak [2048] Specifies the account key length.
d0871bda 5472 --log [/path/to/logfile] Specifies the log file. The default is: \"$DEFAULT_LOG_FILE\" if you don't give a file path here.
a73c5b33 5473 --log-level 1|2 Specifies the log level, default is 1.
52765466 5474 --syslog [0|3|6|7] Syslog level, 0: disable syslog, 3: error, 6: info, 7: debug.
3c07f57a 5475
a63b05a9 5476 These parameters are to install the cert to nginx/apache or anyother server after issue/renew a cert:
3c07f57a 5477
13fe54c9 5478 --cert-file After issue/renew, the cert will be copied to this path.
5479 --key-file After issue/renew, the key will be copied to this path.
5480 --ca-file After issue/renew, the intermediate cert will be copied to this path.
5481 --fullchain-file After issue/renew, the fullchain cert will be copied to this path.
3c07f57a 5482
a63b05a9 5483 --reloadcmd \"service nginx reload\" After issue/renew, it's used to reload the server.
5484
48d9a8c1 5485 --server SERVER ACME Directory Resource URI. (default: https://acme-v01.api.letsencrypt.org/directory)
a63b05a9 5486 --accountconf Specifies a customized account config file.
635695ec 5487 --home Specifies the home dir for $PROJECT_NAME .
27dbe77f 5488 --cert-home Specifies the home dir to save all the certs, only valid for '--install' command.
5489 --config-home Specifies the home dir to save all the configurations.
635695ec 5490 --useragent Specifies the user agent string. it will be saved for future use too.
9082862b
C
5491 --accountemail Specifies the account email, only valid for the '--install' and '--update-account' command.
5492 --accountkey Specifies the account key path, only valid for the '--install' command.
523c7682 5493 --days Specifies the days to renew the cert when using '--issue' command. The max value is $MAX_RENEW days.
39c8f79f 5494 --httpport Specifies the standalone listening port. Only valid if the server is behind a reverse proxy or load balancer.
6ae0f7f5 5495 --local-address Specifies the standalone/tls server listening address, in case you have multiple ip addresses.
dcf4f8f6 5496 --listraw Only used for '--list' command, list the certs in raw format.
27dbe77f 5497 --stopRenewOnError, -se Only valid for '--renew-all' command. Stop if one cert has error in renewal.
13d7cae9 5498 --insecure Do not check the server certificate, in some devices, the api server's certificate may not be trusted.
8f73e241 5499 --ca-bundle Specifies the path to the CA certificate bundle to verify api server's certificate.
13fe54c9 5500 --ca-path Specifies directory containing CA certificates in PEM format, used by wget or curl.
bc96082f 5501 --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 5502 --no-color Do not output color text.
e32b3aac 5503 --force-color Force output of color text. Useful for non-interactive use with the aha tool for HTML E-Mails.
27dbe77f 5504 --ecc Specifies to use the ECC cert. Valid for '--install-cert', '--renew', '--revoke', '--toPkcs' and '--createCSR'
10afcaca 5505 --csr Specifies the input csr.
b0070f03 5506 --pre-hook Command to be run before obtaining any certificates.
84a6730b 5507 --post-hook Command to be run after attempting to obtain/renew certificates. No matter the obtain/renew is success or failed.
b0070f03 5508 --renew-hook Command to be run once for each successfully renewed certificate.
a61fe418 5509 --deploy-hook The hook file to deploy cert
0c9546cc 5510 --ocsp-must-staple, --ocsp Generate ocsp must Staple extension.
c4b2e582 5511 --always-force-new-domain-key Generate new domain key when renewal. Otherwise, the domain key is not changed by default.
6bf281f9 5512 --auto-upgrade [0|1] Valid for '--upgrade' command, indicating whether to upgrade automatically in future.
6ae0f7f5 5513 --listen-v4 Force standalone/tls server to listen at ipv4.
5514 --listen-v6 Force standalone/tls server to listen at ipv6.
a746139c 5515 --openssl-bin Specifies a custom openssl bin location.
9b124070 5516 --use-wget Force to use wget, if you have both curl and wget installed.
a0923622 5517 --yes-I-know-dns-manual-mode-enough-go-ahead-please Force to use dns manual mode: $_DNS_MANUAL_WIKI
66686de4 5518 --branch, -b Only valid for '--upgrade' command, specifies the branch name to upgrade to.
4c3b3608 5519 "
5520}
5521
86ef0a26 5522# nocron noprofile
4a0f23e2 5523_installOnline() {
5524 _info "Installing from online archive."
52677b0a 5525 _nocron="$1"
86ef0a26 5526 _noprofile="$2"
4c2a3841 5527 if [ ! "$BRANCH" ]; then
4a0f23e2 5528 BRANCH="master"
5529 fi
a8df88ab 5530
4a0f23e2 5531 target="$PROJECT/archive/$BRANCH.tar.gz"
5532 _info "Downloading $target"
5533 localname="$BRANCH.tar.gz"
4c2a3841 5534 if ! _get "$target" >$localname; then
df9547ae 5535 _err "Download error."
4a0f23e2 5536 return 1
5537 fi
0bbe6eef 5538 (
4c2a3841 5539 _info "Extracting $localname"
3a3b0dd5 5540 if ! (tar xzf $localname || gtar xzf $localname); then
5541 _err "Extraction error."
5542 exit 1
5543 fi
4c2a3841 5544
5545 cd "$PROJECT_NAME-$BRANCH"
5546 chmod +x $PROJECT_ENTRY
86ef0a26 5547 if ./$PROJECT_ENTRY install "$_nocron" "" "$_noprofile"; then
4c2a3841 5548 _info "Install success!"
5549 fi
5550
5551 cd ..
5552
5553 rm -rf "$PROJECT_NAME-$BRANCH"
5554 rm -f "$localname"
0bbe6eef 5555 )
4a0f23e2 5556}
5557
52677b0a 5558upgrade() {
5559 if (
267f283a 5560 _initpath
5561 export LE_WORKING_DIR
d0b748a4 5562 cd "$LE_WORKING_DIR"
86ef0a26 5563 _installOnline "nocron" "noprofile"
4c2a3841 5564 ); then
52677b0a 5565 _info "Upgrade success!"
096d8992 5566 exit 0
52677b0a 5567 else
5568 _err "Upgrade failed!"
096d8992 5569 exit 1
52677b0a 5570 fi
5571}
a63b05a9 5572
5ea6e9c9 5573_processAccountConf() {
4c2a3841 5574 if [ "$_useragent" ]; then
5ea6e9c9 5575 _saveaccountconf "USER_AGENT" "$_useragent"
4c2a3841 5576 elif [ "$USER_AGENT" ] && [ "$USER_AGENT" != "$DEFAULT_USER_AGENT" ]; then
d0871bda 5577 _saveaccountconf "USER_AGENT" "$USER_AGENT"
5ea6e9c9 5578 fi
4c2a3841 5579
5580 if [ "$_accountemail" ]; then
5ea6e9c9 5581 _saveaccountconf "ACCOUNT_EMAIL" "$_accountemail"
4c2a3841 5582 elif [ "$ACCOUNT_EMAIL" ] && [ "$ACCOUNT_EMAIL" != "$DEFAULT_ACCOUNT_EMAIL" ]; then
d0871bda 5583 _saveaccountconf "ACCOUNT_EMAIL" "$ACCOUNT_EMAIL"
5ea6e9c9 5584 fi
4c2a3841 5585
a746139c 5586 if [ "$_openssl_bin" ]; then
851fedf7 5587 _saveaccountconf "ACME_OPENSSL_BIN" "$_openssl_bin"
5588 elif [ "$ACME_OPENSSL_BIN" ] && [ "$ACME_OPENSSL_BIN" != "$DEFAULT_OPENSSL_BIN" ]; then
5589 _saveaccountconf "ACME_OPENSSL_BIN" "$ACME_OPENSSL_BIN"
a746139c 5590 fi
5591
4c2a3841 5592 if [ "$_auto_upgrade" ]; then
6bf281f9 5593 _saveaccountconf "AUTO_UPGRADE" "$_auto_upgrade"
4c2a3841 5594 elif [ "$AUTO_UPGRADE" ]; then
6bf281f9 5595 _saveaccountconf "AUTO_UPGRADE" "$AUTO_UPGRADE"
5596 fi
4c2a3841 5597
9b124070 5598 if [ "$_use_wget" ]; then
5599 _saveaccountconf "ACME_USE_WGET" "$_use_wget"
5600 elif [ "$ACME_USE_WGET" ]; then
5601 _saveaccountconf "ACME_USE_WGET" "$ACME_USE_WGET"
5602 fi
5603
5ea6e9c9 5604}
5605
a63b05a9 5606_process() {
5607 _CMD=""
5608 _domain=""
3f4513b3 5609 _altdomains="$NO_VALUE"
a63b05a9 5610 _webroot=""
875625b1 5611 _challenge_alias=""
bdbf323f 5612 _keylength=""
5613 _accountkeylength=""
5c539af7 5614 _cert_file=""
5615 _key_file=""
5616 _ca_file=""
5617 _fullchain_file=""
4d2f38b0 5618 _reloadcmd=""
a63b05a9 5619 _password=""
635695ec 5620 _accountconf=""
5621 _useragent=""
b5eb4b90 5622 _accountemail=""
5623 _accountkey=""
b2817897 5624 _certhome=""
27dbe77f 5625 _confighome=""
39c8f79f 5626 _httpport=""
e22bcf7c 5627 _tlsport=""
0e38c60d 5628 _dnssleep=""
dcf4f8f6 5629 _listraw=""
cc179731 5630 _stopRenewOnError=""
e3698edd 5631 #_insecure=""
78009539 5632 _ca_bundle=""
2aa75f03 5633 _ca_path=""
c8e9a31e 5634 _nocron=""
43822d37 5635 _ecc=""
10afcaca 5636 _csr=""
b0070f03 5637 _pre_hook=""
5638 _post_hook=""
5639 _renew_hook=""
a61fe418 5640 _deploy_hook=""
5ea6e9c9 5641 _logfile=""
d0871bda 5642 _log=""
0463b5d6 5643 _local_address=""
a73c5b33 5644 _log_level=""
6bf281f9 5645 _auto_upgrade=""
6ae0f7f5 5646 _listen_v4=""
5647 _listen_v6=""
a746139c 5648 _openssl_bin=""
e2edf208 5649 _syslog=""
9b124070 5650 _use_wget=""
98394f99 5651 _server=""
4c2a3841 5652 while [ ${#} -gt 0 ]; do
a63b05a9 5653 case "${1}" in
4c2a3841 5654
5655 --help | -h)
a63b05a9 5656 showhelp
5657 return
5658 ;;
4c2a3841 5659 --version | -v)
a63b05a9 5660 version
5661 return
5662 ;;
4c2a3841 5663 --install)
a63b05a9 5664 _CMD="install"
5665 ;;
4c2a3841 5666 --uninstall)
a63b05a9 5667 _CMD="uninstall"
5668 ;;
4c2a3841 5669 --upgrade)
52677b0a 5670 _CMD="upgrade"
5671 ;;
4c2a3841 5672 --issue)
a63b05a9 5673 _CMD="issue"
5674 ;;
4c2a3841 5675 --deploy)
a61fe418 5676 _CMD="deploy"
5677 ;;
4c2a3841 5678 --signcsr)
10afcaca 5679 _CMD="signcsr"
5680 ;;
4c2a3841 5681 --showcsr)
10afcaca 5682 _CMD="showcsr"
5683 ;;
db7e4bf9 5684 --installcert | -i | --install-cert)
a63b05a9 5685 _CMD="installcert"
5686 ;;
4c2a3841 5687 --renew | -r)
a63b05a9 5688 _CMD="renew"
5689 ;;
db7e4bf9 5690 --renewAll | --renewall | --renew-all)
a63b05a9 5691 _CMD="renewAll"
5692 ;;
4c2a3841 5693 --revoke)
a63b05a9 5694 _CMD="revoke"
5695 ;;
78f0201d 5696 --remove)
5697 _CMD="remove"
5698 ;;
4c2a3841 5699 --list)
6d7eda3e 5700 _CMD="list"
5701 ;;
ee20015d 5702 --installcronjob | --install-cronjob)
a63b05a9 5703 _CMD="installcronjob"
5704 ;;
db7e4bf9 5705 --uninstallcronjob | --uninstall-cronjob)
a63b05a9 5706 _CMD="uninstallcronjob"
5707 ;;
4c2a3841 5708 --cron)
a63b05a9 5709 _CMD="cron"
5710 ;;
4c2a3841 5711 --toPkcs)
a63b05a9 5712 _CMD="toPkcs"
4c2a3841 5713 ;;
4410226d 5714 --toPkcs8)
5715 _CMD="toPkcs8"
342128a4 5716 ;;
0984585d 5717 --createAccountKey | --createaccountkey | -cak | --create-account-key)
a63b05a9 5718 _CMD="createAccountKey"
5719 ;;
0984585d 5720 --createDomainKey | --createdomainkey | -cdk | --create-domain-key)
a63b05a9 5721 _CMD="createDomainKey"
5722 ;;
4c2a3841 5723 --createCSR | --createcsr | -ccr)
a63b05a9 5724 _CMD="createCSR"
5725 ;;
4c2a3841 5726 --deactivate)
0c00e870 5727 _CMD="deactivate"
5728 ;;
ee20015d 5729 --updateaccount | --update-account)
eb59817e 5730 _CMD="updateaccount"
5731 ;;
ee20015d 5732 --registeraccount | --register-account)
eb59817e 5733 _CMD="registeraccount"
5734 ;;
422dd1fa 5735 --deactivate-account)
5736 _CMD="deactivateaccount"
5737 ;;
4c2a3841 5738 --domain | -d)
a63b05a9 5739 _dvalue="$2"
4c2a3841 5740
5741 if [ "$_dvalue" ]; then
5742 if _startswith "$_dvalue" "-"; then
ee1737a5 5743 _err "'$_dvalue' is not a valid domain for parameter '$1'"
5744 return 1
5745 fi
4c2a3841 5746 if _is_idn "$_dvalue" && ! _exists idn; then
9774b01b 5747 _err "It seems that $_dvalue is an IDN( Internationalized Domain Names), please install 'idn' command first."
5748 return 1
5749 fi
4c2a3841 5750
9e9f839d 5751 if _startswith "$_dvalue" "*."; then
5752 _debug "Wildcard domain"
5753 export ACME_VERSION=2
5754 fi
4c2a3841 5755 if [ -z "$_domain" ]; then
ee1737a5 5756 _domain="$_dvalue"
a63b05a9 5757 else
4c2a3841 5758 if [ "$_altdomains" = "$NO_VALUE" ]; then
ee1737a5 5759 _altdomains="$_dvalue"
5760 else
5761 _altdomains="$_altdomains,$_dvalue"
5762 fi
a63b05a9 5763 fi
5764 fi
4c2a3841 5765
a63b05a9 5766 shift
5767 ;;
5768
4c2a3841 5769 --force | -f)
a63b05a9 5770 FORCE="1"
5771 ;;
4c2a3841 5772 --staging | --test)
a63b05a9 5773 STAGE="1"
5774 ;;
48d9a8c1 5775 --server)
5776 ACME_DIRECTORY="$2"
98394f99 5777 _server="$ACME_DIRECTORY"
48d9a8c1 5778 export ACME_DIRECTORY
5779 shift
5780 ;;
4c2a3841 5781 --debug)
5782 if [ -z "$2" ] || _startswith "$2" "-"; then
fc6cf4d9 5783 DEBUG="$DEBUG_LEVEL_DEFAULT"
a63b05a9 5784 else
5785 DEBUG="$2"
5786 shift
4c2a3841 5787 fi
a63b05a9 5788 ;;
e6e85b0c 5789 --output-insecure)
5790 export OUTPUT_INSECURE=1
5791 ;;
4c2a3841 5792 --webroot | -w)
a63b05a9 5793 wvalue="$2"
4c2a3841 5794 if [ -z "$_webroot" ]; then
a63b05a9 5795 _webroot="$wvalue"
5796 else
5797 _webroot="$_webroot,$wvalue"
5798 fi
5799 shift
4c2a3841 5800 ;;
875625b1 5801 --challenge-alias)
5802 cvalue="$2"
5803 _challenge_alias="$_challenge_alias$cvalue,"
5804 shift
5805 ;;
64821ad4 5806 --domain-alias)
5807 cvalue="$DNS_ALIAS_PREFIX$2"
5808 _challenge_alias="$_challenge_alias$cvalue,"
5809 shift
1f7df33e 5810 ;;
4c2a3841 5811 --standalone)
3f4513b3 5812 wvalue="$NO_VALUE"
4c2a3841 5813 if [ -z "$_webroot" ]; then
a63b05a9 5814 _webroot="$wvalue"
5815 else
5816 _webroot="$_webroot,$wvalue"
5817 fi
5818 ;;
0e44f587 5819 --stateless)
5820 wvalue="$MODE_STATELESS"
5821 if [ -z "$_webroot" ]; then
5822 _webroot="$wvalue"
5823 else
5824 _webroot="$_webroot,$wvalue"
5825 fi
5826 ;;
4c2a3841 5827 --local-address)
0463b5d6 5828 lvalue="$2"
5829 _local_address="$_local_address$lvalue,"
5830 shift
5831 ;;
4c2a3841 5832 --apache)
a63b05a9 5833 wvalue="apache"
4c2a3841 5834 if [ -z "$_webroot" ]; then
a63b05a9 5835 _webroot="$wvalue"
5836 else
5837 _webroot="$_webroot,$wvalue"
5838 fi
5839 ;;
9d725af6 5840 --nginx)
5841 wvalue="$NGINX"
5842 if [ -z "$_webroot" ]; then
5843 _webroot="$wvalue"
5844 else
5845 _webroot="$_webroot,$wvalue"
5846 fi
5847 ;;
4c2a3841 5848 --dns)
3881f221 5849 wvalue="$W_DNS"
a5c56c54 5850 if [ "$2" ] && ! _startswith "$2" "-"; then
a63b05a9 5851 wvalue="$2"
5852 shift
5853 fi
4c2a3841 5854 if [ -z "$_webroot" ]; then
a63b05a9 5855 _webroot="$wvalue"
5856 else
5857 _webroot="$_webroot,$wvalue"
5858 fi
5859 ;;
4c2a3841 5860 --dnssleep)
0e38c60d 5861 _dnssleep="$2"
5862 Le_DNSSleep="$_dnssleep"
5863 shift
5864 ;;
4c2a3841 5865
5866 --keylength | -k)
a63b05a9 5867 _keylength="$2"
a63b05a9 5868 shift
5869 ;;
4c2a3841 5870 --accountkeylength | -ak)
2ce87fe2 5871 _accountkeylength="$2"
a63b05a9 5872 shift
5873 ;;
5874
5c539af7 5875 --cert-file | --certpath)
5876 _cert_file="$2"
a63b05a9 5877 shift
5878 ;;
5c539af7 5879 --key-file | --keypath)
5880 _key_file="$2"
a63b05a9 5881 shift
5882 ;;
5c539af7 5883 --ca-file | --capath)
5884 _ca_file="$2"
a63b05a9 5885 shift
5886 ;;
5c539af7 5887 --fullchain-file | --fullchainpath)
5888 _fullchain_file="$2"
a63b05a9 5889 shift
5890 ;;
4c2a3841 5891 --reloadcmd | --reloadCmd)
a63b05a9 5892 _reloadcmd="$2"
5893 shift
5894 ;;
4c2a3841 5895 --password)
a63b05a9 5896 _password="$2"
5897 shift
5898 ;;
4c2a3841 5899 --accountconf)
635695ec 5900 _accountconf="$2"
5901 ACCOUNT_CONF_PATH="$_accountconf"
a7b7355d 5902 shift
a63b05a9 5903 ;;
4c2a3841 5904 --home)
a63b05a9 5905 LE_WORKING_DIR="$2"
a7b7355d 5906 shift
a63b05a9 5907 ;;
ee20015d 5908 --certhome | --cert-home)
b2817897 5909 _certhome="$2"
5910 CERT_HOME="$_certhome"
5911 shift
4c2a3841 5912 ;;
27dbe77f 5913 --config-home)
5914 _confighome="$2"
f5b546b3 5915 LE_CONFIG_HOME="$_confighome"
27dbe77f 5916 shift
5917 ;;
4c2a3841 5918 --useragent)
635695ec 5919 _useragent="$2"
5920 USER_AGENT="$_useragent"
5921 shift
5922 ;;
4c2a3841 5923 --accountemail)
b5eb4b90 5924 _accountemail="$2"
5925 ACCOUNT_EMAIL="$_accountemail"
5926 shift
5927 ;;
4c2a3841 5928 --accountkey)
b5eb4b90 5929 _accountkey="$2"
5930 ACCOUNT_KEY_PATH="$_accountkey"
5931 shift
5932 ;;
4c2a3841 5933 --days)
06625071 5934 _days="$2"
5935 Le_RenewalDays="$_days"
5936 shift
5937 ;;
4c2a3841 5938 --httpport)
39c8f79f 5939 _httpport="$2"
5940 Le_HTTPPort="$_httpport"
5941 shift
5942 ;;
4c2a3841 5943 --listraw)
dcf4f8f6 5944 _listraw="raw"
4c2a3841 5945 ;;
5946 --stopRenewOnError | --stoprenewonerror | -se)
cc179731 5947 _stopRenewOnError="1"
5948 ;;
4c2a3841 5949 --insecure)
e3698edd 5950 #_insecure="1"
fac1e367 5951 HTTPS_INSECURE="1"
13d7cae9 5952 ;;
4c2a3841 5953 --ca-bundle)
78d1cfb4 5954 _ca_bundle="$(_readlink "$2")"
78009539
PS
5955 CA_BUNDLE="$_ca_bundle"
5956 shift
5957 ;;
2aa75f03 5958 --ca-path)
5959 _ca_path="$2"
5960 CA_PATH="$_ca_path"
5961 shift
5962 ;;
4c2a3841 5963 --nocron)
c8e9a31e 5964 _nocron="1"
5965 ;;
08b4e1a7 5966 --no-color)
5967 export ACME_NO_COLOR=1
5968 ;;
e32b3aac
SB
5969 --force-color)
5970 export ACME_FORCE_COLOR=1
5971 ;;
4c2a3841 5972 --ecc)
43822d37 5973 _ecc="isEcc"
5974 ;;
4c2a3841 5975 --csr)
10afcaca 5976 _csr="$2"
5977 shift
5978 ;;
4c2a3841 5979 --pre-hook)
b0070f03 5980 _pre_hook="$2"
5981 shift
5982 ;;
4c2a3841 5983 --post-hook)
b0070f03 5984 _post_hook="$2"
5985 shift
5986 ;;
4c2a3841 5987 --renew-hook)
b0070f03 5988 _renew_hook="$2"
5989 shift
5990 ;;
4c2a3841 5991 --deploy-hook)
93bce1b2 5992 if [ -z "$2" ] || _startswith "$2" "-"; then
5993 _usage "Please specify a value for '--deploy-hook'"
5994 return 1
5995 fi
5996 _deploy_hook="$_deploy_hook$2,"
a61fe418 5997 shift
5998 ;;
4c2a3841 5999 --ocsp-must-staple | --ocsp)
96db9362 6000 Le_OCSP_Staple="1"
0c9546cc 6001 ;;
c4b2e582 6002 --always-force-new-domain-key)
6003 if [ -z "$2" ] || _startswith "$2" "-"; then
6004 Le_ForceNewDomainKey=1
6005 else
6006 Le_ForceNewDomainKey="$2"
6007 shift
6008 fi
6009 ;;
a0923622 6010 --yes-I-know-dns-manual-mode-enough-go-ahead-please)
6011 export FORCE_DNS_MANUAL=1
6012 ;;
4c2a3841 6013 --log | --logfile)
d0871bda 6014 _log="1"
5ea6e9c9 6015 _logfile="$2"
4c2a3841 6016 if _startswith "$_logfile" '-'; then
d0871bda 6017 _logfile=""
6018 else
6019 shift
6020 fi
5ea6e9c9 6021 LOG_FILE="$_logfile"
4c2a3841 6022 if [ -z "$LOG_LEVEL" ]; then
a73c5b33 6023 LOG_LEVEL="$DEFAULT_LOG_LEVEL"
6024 fi
6025 ;;
4c2a3841 6026 --log-level)
30bfc2ce 6027 _log_level="$2"
a73c5b33 6028 LOG_LEVEL="$_log_level"
6029 shift
5ea6e9c9 6030 ;;
e2edf208 6031 --syslog)
6032 if ! _startswith "$2" '-'; then
6033 _syslog="$2"
6034 shift
6035 fi
6036 if [ -z "$_syslog" ]; then
fc6cf4d9 6037 _syslog="$SYSLOG_LEVEL_DEFAULT"
e2edf208 6038 fi
6039 ;;
4c2a3841 6040 --auto-upgrade)
6bf281f9 6041 _auto_upgrade="$2"
4c2a3841 6042 if [ -z "$_auto_upgrade" ] || _startswith "$_auto_upgrade" '-'; then
6bf281f9 6043 _auto_upgrade="1"
6044 else
6045 shift
6046 fi
6047 AUTO_UPGRADE="$_auto_upgrade"
6048 ;;
4c2a3841 6049 --listen-v4)
6ae0f7f5 6050 _listen_v4="1"
6051 Le_Listen_V4="$_listen_v4"
6052 ;;
4c2a3841 6053 --listen-v6)
6ae0f7f5 6054 _listen_v6="1"
6055 Le_Listen_V6="$_listen_v6"
6056 ;;
a746139c 6057 --openssl-bin)
6058 _openssl_bin="$2"
851fedf7 6059 ACME_OPENSSL_BIN="$_openssl_bin"
7c2e8754 6060 shift
a746139c 6061 ;;
9b124070 6062 --use-wget)
6063 _use_wget="1"
6064 ACME_USE_WGET="1"
6065 ;;
66686de4 6066 --branch | -b)
6067 export BRANCH="$2"
6068 shift
6069 ;;
4c2a3841 6070 *)
a63b05a9 6071 _err "Unknown parameter : $1"
6072 return 1
6073 ;;
6074 esac
6075
6076 shift 1
6077 done
6078
4c2a3841 6079 if [ "${_CMD}" != "install" ]; then
5ea6e9c9 6080 __initHome
661f0583 6081 if [ "$_log" ]; then
4c2a3841 6082 if [ -z "$_logfile" ]; then
661f0583 6083 _logfile="$DEFAULT_LOG_FILE"
6084 fi
d0871bda 6085 fi
4c2a3841 6086 if [ "$_logfile" ]; then
5ea6e9c9 6087 _saveaccountconf "LOG_FILE" "$_logfile"
661f0583 6088 LOG_FILE="$_logfile"
5ea6e9c9 6089 fi
a73c5b33 6090
4c2a3841 6091 if [ "$_log_level" ]; then
a73c5b33 6092 _saveaccountconf "LOG_LEVEL" "$_log_level"
6093 LOG_LEVEL="$_log_level"
6094 fi
4c2a3841 6095
e2edf208 6096 if [ "$_syslog" ]; then
6097 if _exists logger; then
6098 if [ "$_syslog" = "0" ]; then
6099 _clearaccountconf "SYS_LOG"
6100 else
6101 _saveaccountconf "SYS_LOG" "$_syslog"
6102 fi
6103 SYS_LOG="$_syslog"
6104 else
6105 _err "The 'logger' command is not found, can not enable syslog."
6106 _clearaccountconf "SYS_LOG"
6107 SYS_LOG=""
6108 fi
6109 fi
6110
5ea6e9c9 6111 _processAccountConf
6112 fi
4c2a3841 6113
9d548d81 6114 _debug2 LE_WORKING_DIR "$LE_WORKING_DIR"
4c2a3841 6115
6116 if [ "$DEBUG" ]; then
dcf9cb58 6117 version
98394f99 6118 if [ "$_server" ]; then
6119 _debug "Using server: $_server"
6120 fi
dcf9cb58 6121 fi
a63b05a9 6122
6123 case "${_CMD}" in
27dbe77f 6124 install) install "$_nocron" "$_confighome" ;;
bc96082f 6125 uninstall) uninstall "$_nocron" ;;
52677b0a 6126 upgrade) upgrade ;;
a63b05a9 6127 issue)
875625b1 6128 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 6129 ;;
a61fe418 6130 deploy)
6131 deploy "$_domain" "$_deploy_hook" "$_ecc"
6132 ;;
10afcaca 6133 signcsr)
875625b1 6134 signcsr "$_csr" "$_webroot" "$_cert_file" "$_key_file" "$_ca_file" "$_reloadcmd" "$_fullchain_file" "$_pre_hook" "$_post_hook" "$_renew_hook" "$_local_address" "$_challenge_alias"
10afcaca 6135 ;;
6136 showcsr)
6137 showcsr "$_csr" "$_domain"
6138 ;;
a63b05a9 6139 installcert)
5c539af7 6140 installcert "$_domain" "$_cert_file" "$_key_file" "$_ca_file" "$_reloadcmd" "$_fullchain_file" "$_ecc"
a63b05a9 6141 ;;
4c2a3841 6142 renew)
43822d37 6143 renew "$_domain" "$_ecc"
a63b05a9 6144 ;;
4c2a3841 6145 renewAll)
cc179731 6146 renewAll "$_stopRenewOnError"
a63b05a9 6147 ;;
4c2a3841 6148 revoke)
43822d37 6149 revoke "$_domain" "$_ecc"
a63b05a9 6150 ;;
78f0201d 6151 remove)
6152 remove "$_domain" "$_ecc"
6153 ;;
4c2a3841 6154 deactivate)
3f4513b3 6155 deactivate "$_domain,$_altdomains"
eb59817e 6156 ;;
4c2a3841 6157 registeraccount)
57e58ce7 6158 registeraccount "$_accountkeylength"
eb59817e 6159 ;;
4c2a3841 6160 updateaccount)
eb59817e 6161 updateaccount
6162 ;;
422dd1fa 6163 deactivateaccount)
6164 deactivateaccount
6165 ;;
4c2a3841 6166 list)
dcf4f8f6 6167 list "$_listraw"
6d7eda3e 6168 ;;
27dbe77f 6169 installcronjob) installcronjob "$_confighome" ;;
a63b05a9 6170 uninstallcronjob) uninstallcronjob ;;
6171 cron) cron ;;
4c2a3841 6172 toPkcs)
43822d37 6173 toPkcs "$_domain" "$_password" "$_ecc"
a63b05a9 6174 ;;
4410226d 6175 toPkcs8)
6176 toPkcs8 "$_domain" "$_ecc"
6177 ;;
4c2a3841 6178 createAccountKey)
5fbc47eb 6179 createAccountKey "$_accountkeylength"
a63b05a9 6180 ;;
4c2a3841 6181 createDomainKey)
a63b05a9 6182 createDomainKey "$_domain" "$_keylength"
6183 ;;
4c2a3841 6184 createCSR)
43822d37 6185 createCSR "$_domain" "$_altdomains" "$_ecc"
a63b05a9 6186 ;;
6187
6188 *)
27dbe77f 6189 if [ "$_CMD" ]; then
6190 _err "Invalid command: $_CMD"
6191 fi
4c2a3841 6192 showhelp
a63b05a9 6193 return 1
4c2a3841 6194 ;;
a63b05a9 6195 esac
d3595686 6196 _ret="$?"
4c2a3841 6197 if [ "$_ret" != "0" ]; then
d3595686 6198 return $_ret
6199 fi
4c2a3841 6200
6201 if [ "${_CMD}" = "install" ]; then
6202 if [ "$_log" ]; then
6203 if [ -z "$LOG_FILE" ]; then
d0871bda 6204 LOG_FILE="$DEFAULT_LOG_FILE"
6205 fi
6206 _saveaccountconf "LOG_FILE" "$LOG_FILE"
5ea6e9c9 6207 fi
4c2a3841 6208
6209 if [ "$_log_level" ]; then
a73c5b33 6210 _saveaccountconf "LOG_LEVEL" "$_log_level"
6211 fi
e2edf208 6212
6213 if [ "$_syslog" ]; then
6214 if _exists logger; then
6215 if [ "$_syslog" = "0" ]; then
6216 _clearaccountconf "SYS_LOG"
6217 else
6218 _saveaccountconf "SYS_LOG" "$_syslog"
6219 fi
6220 else
6221 _err "The 'logger' command is not found, can not enable syslog."
6222 _clearaccountconf "SYS_LOG"
6223 SYS_LOG=""
6224 fi
6225 fi
6226
5ea6e9c9 6227 _processAccountConf
b5eb4b90 6228 fi
635695ec 6229
a63b05a9 6230}
6231
4c2a3841 6232if [ "$INSTALLONLINE" ]; then
d1f97fc8 6233 INSTALLONLINE=""
2fbf3991 6234 _installOnline
4a0f23e2 6235 exit
6236fi
4c3b3608 6237
319e0ae3 6238main() {
6239 [ -z "$1" ] && showhelp && return
4c2a3841 6240 if _startswith "$1" '-'; then _process "$@"; else "$@"; fi
319e0ae3 6241}
e69a7c38 6242
aa7b82de 6243main "$@"