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