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