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