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